Keep the last valid CPU reading during RAM-only heartbeats
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -147,10 +147,12 @@ pub async fn ingest(
|
||||
match &e.payload {
|
||||
Some(Payload::Heartbeat(h)) => {
|
||||
Uuid::parse_str(&h.instance_id)?;
|
||||
let usage = h.system_usage.as_ref().and_then(system_usage);
|
||||
let usage = h.system_usage.as_ref().and_then(|sample| system_usage(sample, observed));
|
||||
sqlx::query("UPDATE agent_installations SET last_contact_at=now(),
|
||||
system_usage=COALESCE($2,system_usage),
|
||||
system_usage_observed_at=CASE WHEN $2::jsonb IS NULL THEN system_usage_observed_at ELSE $3 END
|
||||
system_usage=CASE WHEN $2::jsonb IS NULL THEN system_usage
|
||||
ELSE COALESCE(system_usage,'{}'::jsonb) || $2 END,
|
||||
system_usage_observed_at=CASE WHEN $2::jsonb ? 'ram_available_bytes'
|
||||
THEN $3 ELSE system_usage_observed_at END
|
||||
WHERE agent_id=$1 AND revoked_at IS NULL")
|
||||
.bind(agent).bind(usage).bind(observed).execute(&mut *tx).await?;
|
||||
}
|
||||
@@ -204,7 +206,7 @@ pub async fn ingest(
|
||||
))
|
||||
}
|
||||
|
||||
fn system_usage(sample: &SystemUsage) -> Option<Value> {
|
||||
fn system_usage(sample: &SystemUsage, observed: DateTime<Utc>) -> Option<Value> {
|
||||
let cpu = sample
|
||||
.cpu_usage_basis_points
|
||||
.filter(|&value| value <= 10_000);
|
||||
@@ -212,14 +214,16 @@ fn system_usage(sample: &SystemUsage) -> Option<Value> {
|
||||
.ram_available_bytes
|
||||
.zip(sample.ram_total_bytes)
|
||||
.filter(|&(available, total)| total > 0 && available <= total);
|
||||
if cpu.is_none() && memory.is_none() {
|
||||
return None;
|
||||
let mut usage = serde_json::Map::new();
|
||||
if let Some(cpu) = cpu {
|
||||
usage.insert("cpu_usage_percent".into(), json!(f64::from(cpu) / 100.0));
|
||||
usage.insert("cpu_usage_observed_at".into(), json!(observed));
|
||||
}
|
||||
Some(json!({
|
||||
"cpu_usage_percent": cpu.map(|value| f64::from(value) / 100.0),
|
||||
"ram_available_bytes": memory.map(|(available, _)| available),
|
||||
"ram_total_bytes": memory.map(|(_, total)| total),
|
||||
}))
|
||||
if let Some((available, total)) = memory {
|
||||
usage.insert("ram_available_bytes".into(), json!(available));
|
||||
usage.insert("ram_total_bytes".into(), json!(total));
|
||||
}
|
||||
(!usage.is_empty()).then_some(Value::Object(usage))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -233,13 +237,17 @@ mod tests {
|
||||
ram_available_bytes: Some(0),
|
||||
ram_total_bytes: Some(1),
|
||||
};
|
||||
assert_eq!(
|
||||
system_usage(&sample),
|
||||
Some(json!({"cpu_usage_percent": 0.0, "ram_available_bytes": 0, "ram_total_bytes": 1}))
|
||||
);
|
||||
let now = Utc::now();
|
||||
let measured = system_usage(&sample, now).unwrap();
|
||||
assert_eq!(measured["cpu_usage_percent"], json!(0.0));
|
||||
assert_eq!(measured["cpu_usage_observed_at"], json!(now));
|
||||
assert_eq!(measured["ram_available_bytes"], json!(0));
|
||||
assert_eq!(measured["ram_total_bytes"], json!(1));
|
||||
sample.cpu_usage_basis_points = Some(10_001);
|
||||
assert!(system_usage(&sample).unwrap()["cpu_usage_percent"].is_null());
|
||||
let partial = system_usage(&sample, now).unwrap();
|
||||
assert!(partial.get("cpu_usage_percent").is_none());
|
||||
assert!(partial.get("cpu_usage_observed_at").is_none());
|
||||
sample.ram_available_bytes = Some(2);
|
||||
assert_eq!(system_usage(&sample), None);
|
||||
assert_eq!(system_usage(&sample, now), None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user