fix: jitter_ms now measured in Rust at check start, excludes latency and return trip
This commit is contained in:
parent
e7ec457d0e
commit
27be1fa8bf
|
|
@ -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"] }
|
||||
|
|
|
|||
|
|
@ -57,6 +57,13 @@ pub async fn fetch_and_run(
|
|||
}
|
||||
|
||||
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();
|
||||
|
||||
// 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,
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ pub struct Monitor {
|
|||
pub struct PingResult {
|
||||
pub monitor_id: String,
|
||||
pub scheduled_at: Option<String>,
|
||||
pub jitter_ms: Option<i64>,
|
||||
pub status_code: Option<u16>,
|
||||
pub latency_ms: Option<u64>,
|
||||
pub up: bool,
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
Loading…
Reference in New Issue