diff options
| author | Jesper Jensen <jesper@jnsn.dev> | 2026-02-06 20:02:57 +0100 |
|---|---|---|
| committer | Jesper Jensen <jesper@jnsn.dev> | 2026-02-06 20:02:57 +0100 |
| commit | 82221f3d4f663720abbc2fff27db47da65ce7425 (patch) | |
| tree | fba63d5f2facf156130a4b7d4c31bc8bfb076533 | |
| parent | c2687b44edb4da3b258f4e28c6add8713c50bcec (diff) | |
Add database metrics
| -rw-r--r-- | src/main.rs | 18 | ||||
| -rw-r--r-- | src/metrics.rs | 13 |
2 files changed, 25 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index ff3e596..3811e4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -251,16 +251,24 @@ fn update_images(db: &Connection, file: &str, auth: &mut Auth, img: DockerRef, e metrics::get().images_checked.with_label_values(&[registry]).inc(); - let _last_checked = match db.query_one(" + let query_result = { + let _timer = metrics::get().db_query_duration.start_timer(); + db.query_one(" SELECT last_checked FROM images WHERE registry = ?1 AND image = ?2 - ", (registry, image), |row| row.get::<_, chrono::DateTime<chrono::Utc>>(0)).optional() { + ", (registry, image), |row| row.get::<_, chrono::DateTime<chrono::Utc>>(0)).optional() + }; + + let _last_checked = match query_result { Ok(Some(x)) => x, Ok(None) => { let time = chrono::offset::Utc::now(); - db.execute(" - INSERT INTO images(registry, image, last_checked) VALUES (?1, ?2, ?3) - ", (registry, &image, time)).unwrap(); + { + let _timer = metrics::get().db_query_duration.start_timer(); + db.execute(" + INSERT INTO images(registry, image, last_checked) VALUES (?1, ?2, ?3) + ", (registry, &image, time)).unwrap(); + } time }, Err(_x) => return Err("Database Failure".to_string()), diff --git a/src/metrics.rs b/src/metrics.rs index 09617c4..a6c8286 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -1,5 +1,5 @@ use prometheus::{ - CounterVec, Gauge, HistogramVec, + CounterVec, Gauge, Histogram, HistogramVec, Opts, Registry, TextEncoder, Encoder, histogram_opts, }; @@ -26,6 +26,7 @@ pub struct Metrics { pub auth_attempts: CounterVec, pub run_duration: Gauge, pub git_operations: CounterVec, + pub db_query_duration: Histogram, registry: Registry, } @@ -89,6 +90,15 @@ impl Metrics { ).unwrap(); registry.register(Box::new(git_operations.clone())).unwrap(); + let db_query_duration = Histogram::with_opts( + histogram_opts!( + "vbump_query_duration_seconds", + "Database query latency distribution", + vec![0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5] + ), + ).unwrap(); + registry.register(Box::new(db_query_duration.clone())).unwrap(); + Self { state, images_checked, @@ -99,6 +109,7 @@ impl Metrics { auth_attempts, run_duration, git_operations, + db_query_duration, registry, } } |
