diff --git a/src/metrics/mod.rs b/src/metrics/mod.rs index dd9d255ae0ca..ca4f262acf83 100644 --- a/src/metrics/mod.rs +++ b/src/metrics/mod.rs @@ -53,7 +53,9 @@ where warn!("Failed to register process metrics: {err}"); } - // Add the DBCollector to the registry + DEFAULT_REGISTRY.write().register_collector(Box::new( + crate::utils::version::ForestVersionCollector::new(), + )); DEFAULT_REGISTRY .write() .register_collector(Box::new(crate::metrics::db::DBCollector::new(db_directory))); diff --git a/src/utils/version/mod.rs b/src/utils/version/mod.rs index 792525298f32..bb1de6fc4838 100644 --- a/src/utils/version/mod.rs +++ b/src/utils/version/mod.rs @@ -3,6 +3,11 @@ use git_version::git_version; use once_cell::sync::Lazy; +use prometheus_client::{ + collector::Collector, + encoding::{DescriptorEncoder, EncodeMetric}, + metrics::info::Info, +}; /// Current git commit hash of the Forest repository. pub const GIT_HASH: &str = @@ -15,3 +20,28 @@ pub static FOREST_VERSION_STRING: Lazy = pub static FOREST_VERSION: Lazy = Lazy::new(|| semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid version")); + +#[derive(Debug)] +pub struct ForestVersionCollector { + version: Info>, +} + +impl ForestVersionCollector { + pub fn new() -> Self { + let version = Info::new(vec![("version", FOREST_VERSION_STRING.as_str())]); + Self { version } + } +} + +impl Collector for ForestVersionCollector { + fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { + let metric_encoder = encoder.encode_descriptor( + "forest_version", + "semantic version of the forest binary", + None, + self.version.metric_type(), + )?; + self.version.encode(metric_encoder)?; + Ok(()) + } +}