From 27be1fa8bf971886e68380dd54f6940470be3588 Mon Sep 17 00:00:00 2001 From: M1 Date: Tue, 17 Mar 2026 10:52:08 +0400 Subject: [PATCH] fix: jitter_ms now measured in Rust at check start, excludes latency and return trip --- apps/monitor/Cargo.toml | 1 + apps/monitor/src/runner.rs | 9 +++++++++ apps/monitor/src/types.rs | 1 + apps/web/src/routes/pings.ts | 3 ++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/monitor/Cargo.toml b/apps/monitor/Cargo.toml index 8e2b7db..3d8581f 100644 --- a/apps/monitor/Cargo.toml +++ b/apps/monitor/Cargo.toml @@ -18,3 +18,4 @@ rustls-native-certs = "0.8" webpki-roots = "0.26" x509-parser = "0.16" tokio-rustls = "0.26" +chrono = { version = "0.4", features = ["serde"] } diff --git a/apps/monitor/src/runner.rs b/apps/monitor/src/runner.rs index 85d8aab..40cc023 100644 --- a/apps/monitor/src/runner.rs +++ b/apps/monitor/src/runner.rs @@ -57,6 +57,13 @@ pub async fn fetch_and_run( } async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Option) -> PingResult { + // Compute jitter: how late we actually started vs when we were scheduled + let jitter_ms: Option = scheduled_at.as_deref().and_then(|s| { + let scheduled = chrono::DateTime::parse_from_rfc3339(s).ok()?; + let now = chrono::Utc::now(); + Some((now - scheduled.with_timezone(&chrono::Utc)).num_milliseconds()) + }); + let start = Instant::now(); // Check cert expiry for HTTPS URLs @@ -97,6 +104,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op Err(e) => PingResult { monitor_id: monitor.id.clone(), scheduled_at, + jitter_ms, status_code: None, latency_ms: Some(latency_ms), up: false, @@ -144,6 +152,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op PingResult { monitor_id: monitor.id.clone(), scheduled_at, + jitter_ms, status_code: Some(status), latency_ms: Some(latency_ms), up, diff --git a/apps/monitor/src/types.rs b/apps/monitor/src/types.rs index 22c4bcb..95d3eb5 100644 --- a/apps/monitor/src/types.rs +++ b/apps/monitor/src/types.rs @@ -19,6 +19,7 @@ pub struct Monitor { pub struct PingResult { pub monitor_id: String, pub scheduled_at: Option, + pub jitter_ms: Option, pub status_code: Option, pub latency_ms: Option, pub up: bool, diff --git a/apps/web/src/routes/pings.ts b/apps/web/src/routes/pings.ts index dc48114..c4ba9b6 100644 --- a/apps/web/src/routes/pings.ts +++ b/apps/web/src/routes/pings.ts @@ -57,7 +57,7 @@ export const ingest = new Elysia() if (body.cert_expiry_days != null) meta.cert_expiry_days = body.cert_expiry_days; const scheduledAt = body.scheduled_at ? new Date(body.scheduled_at) : null; - const jitterMs = scheduledAt ? Math.max(0, Date.now() - scheduledAt.getTime()) : null; + const jitterMs = body.jitter_ms ?? null; const [ping] = await sql` INSERT INTO pings (monitor_id, scheduled_at, jitter_ms, status_code, latency_ms, up, error, meta) @@ -83,6 +83,7 @@ export const ingest = new Elysia() body: t.Object({ monitor_id: t.String(), scheduled_at: t.Optional(t.Nullable(t.String())), + jitter_ms: t.Optional(t.Nullable(t.Number())), status_code: t.Optional(t.Number()), latency_ms: t.Optional(t.Number()), up: t.Boolean(),