From bae9a7ecd5ab8010e4f4e5823d48326a0e00dd46 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 14 Mar 2021 15:37:03 +1300 Subject: [PATCH] Expose binary data in metrics This enables slicing and aggregating metrics based on zebrad version: https://www.robustperception.io/exposing-the-software-version-to-prometheus --- zebrad/src/components/metrics.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/zebrad/src/components/metrics.rs b/zebrad/src/components/metrics.rs index 69bb2eeb1..c4d5137f5 100644 --- a/zebrad/src/components/metrics.rs +++ b/zebrad/src/components/metrics.rs @@ -19,6 +19,28 @@ impl MetricsEndpoint { match endpoint_result { Ok(()) => { info!("Opened metrics endpoint at {}", addr); + + // Expose binary metadata to metrics, using a single time series with + // value 1: + // https://www.robustperception.io/exposing-the-software-version-to-prometheus + // + // We manually expand the metrics::increment!() macro because it only + // supports string literals for metrics names, preventing us from + // using concat!() to build the name. + static METRIC_NAME: [metrics::SharedString; 2] = [ + metrics::SharedString::const_str(env!("CARGO_PKG_NAME")), + metrics::SharedString::const_str("build.info"), + ]; + static METRIC_LABELS: [metrics::Label; 1] = + [metrics::Label::from_static_parts( + "version", + env!("CARGO_PKG_VERSION"), + )]; + static METRIC_KEY: metrics::KeyData = + metrics::KeyData::from_static_parts(&METRIC_NAME, &METRIC_LABELS); + if let Some(recorder) = metrics::try_recorder() { + recorder.increment_counter(metrics::Key::Borrowed(&METRIC_KEY), 1); + } } Err(e) => panic!( "Opening metrics endpoint listener {:?} failed: {:?}. \