fix: jitter_ms now measured in Rust at check start, excludes latency and return trip

This commit is contained in:
M1 2026-03-17 10:52:08 +04:00
parent e7ec457d0e
commit 27be1fa8bf
4 changed files with 13 additions and 1 deletions

View File

@ -18,3 +18,4 @@ rustls-native-certs = "0.8"
webpki-roots = "0.26" webpki-roots = "0.26"
x509-parser = "0.16" x509-parser = "0.16"
tokio-rustls = "0.26" tokio-rustls = "0.26"
chrono = { version = "0.4", features = ["serde"] }

View File

@ -57,6 +57,13 @@ pub async fn fetch_and_run(
} }
async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Option<String>) -> PingResult { async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Option<String>) -> PingResult {
// 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 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(); let start = Instant::now();
// Check cert expiry for HTTPS URLs // 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 { Err(e) => PingResult {
monitor_id: monitor.id.clone(), monitor_id: monitor.id.clone(),
scheduled_at, scheduled_at,
jitter_ms,
status_code: None, status_code: None,
latency_ms: Some(latency_ms), latency_ms: Some(latency_ms),
up: false, up: false,
@ -144,6 +152,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(),
scheduled_at, scheduled_at,
jitter_ms,
status_code: Some(status), status_code: Some(status),
latency_ms: Some(latency_ms), latency_ms: Some(latency_ms),
up, up,

View File

@ -19,6 +19,7 @@ pub struct Monitor {
pub struct PingResult { pub struct PingResult {
pub monitor_id: String, pub monitor_id: String,
pub scheduled_at: Option<String>, pub scheduled_at: Option<String>,
pub jitter_ms: Option<i64>,
pub status_code: Option<u16>, pub status_code: Option<u16>,
pub latency_ms: Option<u64>, pub latency_ms: Option<u64>,
pub up: bool, pub up: bool,

View File

@ -57,7 +57,7 @@ export const ingest = new Elysia()
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 scheduledAt = body.scheduled_at ? new Date(body.scheduled_at) : null; 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` const [ping] = await sql`
INSERT INTO pings (monitor_id, scheduled_at, jitter_ms, status_code, latency_ms, up, error, meta) 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({ body: t.Object({
monitor_id: t.String(), monitor_id: 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())),
status_code: t.Optional(t.Number()), status_code: t.Optional(t.Number()),
latency_ms: t.Optional(t.Number()), latency_ms: t.Optional(t.Number()),
up: t.Boolean(), up: t.Boolean(),