fix: use monitor-side checked_at timestamp instead of coordinator arrival time

This commit is contained in:
nate 2026-03-18 18:50:16 +04:00
parent 425bfbfc39
commit 42024a9fc3
3 changed files with 11 additions and 1 deletions

View File

@ -65,13 +65,15 @@ export const ingest = new Elysia()
const meta = body.meta ? { ...body.meta } : {}; const meta = body.meta ? { ...body.meta } : {};
if (body.cert_expiry_days != null) meta.cert_expiry_days = body.cert_expiry_days; if (body.cert_expiry_days != null) meta.cert_expiry_days = body.cert_expiry_days;
const checkedAt = body.checked_at ? new Date(body.checked_at) : null;
const scheduledAt = body.scheduled_at ? new Date(body.scheduled_at) : null; const scheduledAt = body.scheduled_at ? new Date(body.scheduled_at) : null;
const jitterMs = body.jitter_ms ?? null; const jitterMs = body.jitter_ms ?? null;
const [ping] = await sql` const [ping] = await sql`
INSERT INTO pings (monitor_id, scheduled_at, jitter_ms, status_code, latency_ms, up, error, meta, region, run_id) INSERT INTO pings (monitor_id, checked_at, scheduled_at, jitter_ms, status_code, latency_ms, up, error, meta, region, run_id)
VALUES ( VALUES (
${body.monitor_id}, ${body.monitor_id},
${checkedAt ?? sql`now()`},
${scheduledAt}, ${scheduledAt},
${jitterMs}, ${jitterMs},
${body.status_code ?? null}, ${body.status_code ?? null},
@ -93,6 +95,7 @@ export const ingest = new Elysia()
}, { }, {
body: t.Object({ body: t.Object({
monitor_id: t.String(), monitor_id: t.String(),
checked_at: t.Optional(t.Nullable(t.String())),
scheduled_at: t.Optional(t.Nullable(t.String())), scheduled_at: t.Optional(t.Nullable(t.String())),
jitter_ms: t.Optional(t.Nullable(t.Number())), jitter_ms: t.Optional(t.Nullable(t.Number())),
status_code: t.Optional(t.Nullable(t.Number())), status_code: t.Optional(t.Nullable(t.Number())),

View File

@ -100,6 +100,7 @@ pub async fn fetch_and_run(
Ok(r) => r, Ok(r) => r,
Err(_) => PingResult { Err(_) => PingResult {
monitor_id: monitor.id.clone(), monitor_id: monitor.id.clone(),
checked_at: Some(chrono::Utc::now().to_rfc3339()),
scheduled_at: scheduled_at_iso.clone(), scheduled_at: scheduled_at_iso.clone(),
jitter_ms: None, jitter_ms: None,
status_code: None, status_code: None,
@ -125,6 +126,9 @@ pub async fn fetch_and_run(
} }
async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Option<String>, region: &str, run_id: &str) -> PingResult { async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Option<String>, region: &str, run_id: &str) -> PingResult {
// Record when the check actually started (used as checked_at in the ping)
let checked_at = chrono::Utc::now().to_rfc3339();
// Compute jitter: how late we actually started vs when we were scheduled // Compute jitter: how late we actually started vs when we were scheduled
let jitter_ms: Option<i64> = scheduled_at.as_deref().and_then(|s| { let jitter_ms: Option<i64> = scheduled_at.as_deref().and_then(|s| {
let scheduled = chrono::DateTime::parse_from_rfc3339(s).ok()?; let scheduled = chrono::DateTime::parse_from_rfc3339(s).ok()?;
@ -175,6 +179,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
debug!("{} check error: {e}", monitor.url); debug!("{} check error: {e}", monitor.url);
PingResult { PingResult {
monitor_id: monitor.id.clone(), monitor_id: monitor.id.clone(),
checked_at: Some(checked_at.clone()),
scheduled_at, scheduled_at,
jitter_ms, jitter_ms,
status_code: None, status_code: None,
@ -236,6 +241,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
PingResult { PingResult {
monitor_id: monitor.id.clone(), monitor_id: monitor.id.clone(),
checked_at: Some(checked_at),
scheduled_at, scheduled_at,
jitter_ms, jitter_ms,
status_code: Some(status), status_code: Some(status),

View File

@ -33,6 +33,7 @@ pub struct Monitor {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct PingResult { pub struct PingResult {
pub monitor_id: String, pub monitor_id: String,
pub checked_at: Option<String>,
pub scheduled_at: Option<String>, pub scheduled_at: Option<String>,
pub jitter_ms: Option<i64>, pub jitter_ms: Option<i64>,
pub status_code: Option<u16>, pub status_code: Option<u16>,