diff options
Diffstat (limited to 'src/ops.rs')
| -rw-r--r-- | src/ops.rs | 152 |
1 files changed, 75 insertions, 77 deletions
@@ -65,89 +65,90 @@ impl Metrics { } pub fn to_prometheus(&self) -> String { + use std::fmt::Write as _; let mut o = String::new(); - o.push_str("# HELP aurbuild_up Always 1: the process answered the scrape.\n# TYPE aurbuild_up gauge\naurbuild_up 1\n"); - o.push_str(&header("aurbuild_queue_depth", "Builds waiting to run.")); - o.push_str(&format!("aurbuild_queue_depth {}\n", self.queue_depth)); - o.push_str(&header("aurbuild_oldest_queued_age_seconds", "How long the oldest queued build has waited.")); - o.push_str(&format!("aurbuild_oldest_queued_age_seconds {}\n", self.oldest_queued_age_secs)); + gauge(&mut o, "aurbuild_up", "Always 1: the process answered the scrape.", 1); + gauge(&mut o, "aurbuild_queue_depth", "Builds waiting to run.", self.queue_depth); + gauge( + &mut o, + "aurbuild_oldest_queued_age_seconds", + "How long the oldest queued build has waited.", + self.oldest_queued_age_secs, + ); o.push_str("# HELP aurbuild_builds_total Builds by state.\n# TYPE aurbuild_builds_total gauge\n"); for (state, n) in &self.builds { - o.push_str(&format!("aurbuild_builds_total{{state=\"{state}\"}} {n}\n")); + let _ = writeln!(o, "aurbuild_builds_total{{state=\"{state}\"}} {n}"); } - o.push_str(&header("aurbuild_pending_review_candidates", "Reviews waiting on a human — the review debt.")); - o.push_str(&format!("aurbuild_pending_review_candidates {}\n", self.pending_review)); + gauge( + &mut o, + "aurbuild_pending_review_candidates", + "Reviews waiting on a human — the review debt.", + self.pending_review, + ); o.push_str("# HELP aurbuild_pending_review_age_seconds Histogram of how long pending reviews have waited.\n# TYPE aurbuild_pending_review_age_seconds histogram\n"); for le in [3600i64, 21600, 86400, 259200, 604800] { let n = self.review_ages.iter().filter(|a| **a <= le).count(); - o.push_str(&format!("aurbuild_pending_review_age_seconds_bucket{{le=\"{le}\"}} {n}\n")); + let _ = writeln!(o, "aurbuild_pending_review_age_seconds_bucket{{le=\"{le}\"}} {n}"); } - o.push_str(&format!( - "aurbuild_pending_review_age_seconds_bucket{{le=\"+Inf\"}} {}\n", - self.review_ages.len() - )); let sum: i64 = self.review_ages.iter().sum(); - o.push_str(&format!("aurbuild_pending_review_age_seconds_sum {sum}\n")); - o.push_str(&format!("aurbuild_pending_review_age_seconds_count {}\n", self.review_ages.len())); - - o.push_str(&header("aurbuild_builds_succeeded_total", "Builds that published.")); - o.push_str(&format!("aurbuild_builds_succeeded_total {}\n", self.succeeded)); - o.push_str(&header("aurbuild_builds_failed_total", "Builds that failed.")); - o.push_str(&format!("aurbuild_builds_failed_total {}\n", self.failed)); - let rate = self.build_success_rate(); - o.push_str(&header("aurbuild_build_success_rate", "Share of recorded builds that succeeded.")); - o.push_str(&format!("aurbuild_build_success_rate {rate:.4}\n")); - - o.push_str(&header("aurbuild_blocked_missing_dep", "Builds blocked on a package we have not published.")); - o.push_str(&format!("aurbuild_blocked_missing_dep {}\n", self.blocked_missing_dep)); - o.push_str(&header("aurbuild_index_packages", "Packages in the served index.")); - o.push_str(&format!("aurbuild_index_packages {}\n", self.index_packages)); - o.push_str(&header("aurbuild_releases", "Published releases on record.")); - o.push_str(&format!("aurbuild_releases {}\n", self.releases)); - o.push_str(&header("aurbuild_subscriptions", "Subscribed pkgbases.")); - o.push_str(&format!("aurbuild_subscriptions {}\n", self.subscriptions)); - o.push_str(&header("aurbuild_notices_unseen", "Notices no human has looked at.")); - o.push_str(&format!("aurbuild_notices_unseen {}\n", self.notices_unseen)); + let _ = writeln!(o, "aurbuild_pending_review_age_seconds_bucket{{le=\"+Inf\"}} {}", self.review_ages.len()); + let _ = writeln!(o, "aurbuild_pending_review_age_seconds_sum {sum}"); + let _ = writeln!(o, "aurbuild_pending_review_age_seconds_count {}", self.review_ages.len()); + + gauge(&mut o, "aurbuild_builds_succeeded_total", "Builds that published.", self.succeeded); + gauge(&mut o, "aurbuild_builds_failed_total", "Builds that failed.", self.failed); + gauge( + &mut o, + "aurbuild_build_success_rate", + "Share of recorded builds that succeeded.", + format_args!("{:.4}", self.build_success_rate()), + ); + + gauge( + &mut o, + "aurbuild_blocked_missing_dep", + "Builds blocked on a package we have not published.", + self.blocked_missing_dep, + ); + gauge(&mut o, "aurbuild_index_packages", "Packages in the served index.", self.index_packages); + gauge(&mut o, "aurbuild_releases", "Published releases on record.", self.releases); + gauge(&mut o, "aurbuild_subscriptions", "Subscribed pkgbases.", self.subscriptions); + gauge(&mut o, "aurbuild_notices_unseen", "Notices no human has looked at.", self.notices_unseen); o.push_str("# HELP aurbuild_stale_releases Releases whose recorded world no longer matches the index, by reason.\n# TYPE aurbuild_stale_releases gauge\n"); - o.push_str(&format!("aurbuild_stale_releases{{reason=\"outdated\"}} {}\n", self.stale_outdated)); - o.push_str(&format!("aurbuild_stale_releases{{reason=\"missing\"}} {}\n", self.stale_missing)); - o.push_str(&header("aurbuild_downgrades", "Subscriptions where AUR offers a lower version than we serve.")); - o.push_str(&format!("aurbuild_downgrades {}\n", self.downgrades)); - - o.push_str(&header("aurbuild_sqlite_size_bytes", "Main database file size.")); - o.push_str(&format!("aurbuild_sqlite_size_bytes {}\n", self.db_bytes)); - o.push_str(&header("aurbuild_sqlite_wal_size_bytes", "Write-ahead log size — large means nobody is checkpointing.")); - o.push_str(&format!("aurbuild_sqlite_wal_size_bytes {}\n", self.wal_bytes)); - o.push_str(&header("aurbuild_artifact_bytes", "Bytes held in the content-addressed store.")); - o.push_str(&format!("aurbuild_artifact_bytes {}\n", self.artifact_bytes)); - o.push_str(&header("aurbuild_staging_bytes", "Bytes in disposable staging directories.")); - o.push_str(&format!("aurbuild_staging_bytes {}\n", self.staging_bytes)); - o.push_str(&format!("# TYPE aurbuild_staging_dirs gauge\naurbuild_staging_dirs {}\n", self.staging_dirs)); - + let _ = writeln!(o, "aurbuild_stale_releases{{reason=\"outdated\"}} {}", self.stale_outdated); + let _ = writeln!(o, "aurbuild_stale_releases{{reason=\"missing\"}} {}", self.stale_missing); + gauge( + &mut o, + "aurbuild_downgrades", + "Subscriptions where AUR offers a lower version than we serve.", + self.downgrades, + ); + + gauge(&mut o, "aurbuild_sqlite_size_bytes", "Main database file size.", self.db_bytes); + gauge( + &mut o, + "aurbuild_sqlite_wal_size_bytes", + "Write-ahead log size — large means nobody is checkpointing.", + self.wal_bytes, + ); + gauge(&mut o, "aurbuild_artifact_bytes", "Bytes held in the content-addressed store.", self.artifact_bytes); + gauge(&mut o, "aurbuild_staging_bytes", "Bytes in disposable staging directories.", self.staging_bytes); + let _ = writeln!(o, "# TYPE aurbuild_staging_dirs gauge\naurbuild_staging_dirs {}", self.staging_dirs); + + let help = "Seconds since the last successful poll of any source."; match self.poll_lag_age_secs { - Some(a) => { - o.push_str(&header("aurbuild_poll_lag_seconds", "Seconds since the last successful poll of any source.")); - o.push_str(&format!("aurbuild_poll_lag_seconds {a}\n")); - } - None => { - o.push_str(&header("aurbuild_poll_lag_seconds", "Seconds since the last successful poll of any source.")); - o.push_str("aurbuild_poll_lag_seconds nan\n"); - } + Some(a) => gauge(&mut o, "aurbuild_poll_lag_seconds", help, a), + None => gauge(&mut o, "aurbuild_poll_lag_seconds", help, "nan"), } + let help = "Seconds since the last retention run."; match self.last_gc_age_secs { - Some(a) => { - o.push_str(&header("aurbuild_last_gc_age_seconds", "Seconds since the last retention run.")); - o.push_str(&format!("aurbuild_last_gc_age_seconds {a}\n")); - } - None => { - o.push_str(&header("aurbuild_last_gc_age_seconds", "Seconds since the last retention run.")); - o.push_str("aurbuild_last_gc_age_seconds nan\n"); - } + Some(a) => gauge(&mut o, "aurbuild_last_gc_age_seconds", help, a), + None => gauge(&mut o, "aurbuild_last_gc_age_seconds", help, "nan"), } o } @@ -158,9 +159,7 @@ pub fn metrics(cfg: &Config, db: &Db) -> Result<Metrics> { let now = time::now(); m.builds = db::build_state_counts(db)?; - let count = |state: &str| -> i64 { - m.builds.iter().find(|(s, _)| s == state).map(|(_, n)| *n).unwrap_or(0) - }; + let count = |state: &str| -> i64 { m.builds.iter().find(|(s, _)| s == state).map_or(0, |(_, n)| *n) }; m.queue_depth = count("queued"); m.succeeded = count("succeeded"); m.failed = count("failed"); @@ -170,7 +169,7 @@ pub fn metrics(cfg: &Config, db: &Db) -> Result<Metrics> { m.pending_review = db::count_pending_reviews(db)?; m.review_ages = db::pending_review_ages_secs(db, &now)?; - m.review_ages.sort(); + m.review_ages.sort_unstable(); m.index_packages = db::count_index_packages(db)?; m.releases = db::count_published_releases(db)?; @@ -195,12 +194,14 @@ pub fn metrics(cfg: &Config, db: &Db) -> Result<Metrics> { Ok(m) } -fn header(name: &str, help: &str) -> String { - format!("# HELP {name} {help}\n# TYPE {name} gauge\n") +/// One Prometheus gauge: its `# HELP`, its `# TYPE`, and its value. +fn gauge(o: &mut String, name: &str, help: &str, value: impl std::fmt::Display) { + use std::fmt::Write as _; + let _ = write!(o, "# HELP {name} {help}\n# TYPE {name} gauge\n{name} {value}\n"); } fn file_len(p: &Path) -> u64 { - std::fs::metadata(p).map(|m| m.len()).unwrap_or(0) + std::fs::metadata(p).map_or(0, |m| m.len()) } fn staging_usage(root: &Path) -> (usize, u64) { @@ -342,9 +343,7 @@ pub fn release_files(cfg: &Config, db: &Db, release_id: i64) -> Result<Vec<PathB out.push(PathBuf::from(p)); } } - let digests = db::release_by_id(db, release_id)? - .map(|r| r.source_digests_json) - .unwrap_or_else(|| "[]".into()); + let digests = db::release_by_id(db, release_id)?.map_or_else(|| "[]".into(), |r| r.source_digests_json); if let Ok(list) = serde_json::from_str::<Vec<db::SourceDigest>>(&digests) { for d in list { if !d.digest.is_empty() { @@ -438,8 +437,7 @@ pub fn backup(db: &Db, dest: &Path) -> Result<PathBuf> { } } db::backup_to(&db.writer, &dest)?; - let version = db::schema_version(&dest) - .map_err(|e| crate::error::Error::db(format!("backup unreadable: {e}")))?; + let version = db::schema_version(&dest).map_err(|e| crate::error::Error::db(format!("backup unreadable: {e}")))?; if version <= 0 { return Err(crate::error::Error::db("backup does not contain a migrated database")); } |
