From 9e3f418121ea729eaa26bd803a797df7db140f3b Mon Sep 17 00:00:00 2001 From: Hubert Bugaj Date: Wed, 14 Aug 2024 12:59:00 +0200 Subject: [PATCH 1/2] Reapply "feat(metric): add forest version to prometheus (#4535)" (#4594) This reverts commit 7502528e42c4bca9c56ae6447fb8ffa3e47f1fc2. --- src/metrics/mod.rs | 4 +++- src/utils/version/mod.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/metrics/mod.rs b/src/metrics/mod.rs index f2d1239c2ad7..6b355a5518b4 100644 --- a/src/metrics/mod.rs +++ b/src/metrics/mod.rs @@ -80,7 +80,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(()) + } +} From fc2e8b24111c77fb76229cca573155bb51a7864f Mon Sep 17 00:00:00 2001 From: Hubert Bugaj Date: Wed, 14 Aug 2024 14:00:33 +0200 Subject: [PATCH 2/2] fix: use gauge instead of non-standard info --- CHANGELOG.md | 3 +++ src/utils/version/mod.rs | 27 +++++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28dfa85496d0..012db05ddcc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,9 @@ snapshots instead of moving or copying them. This can be invoked with `--import-snapshot --import-mode=symlink`. +- [#4533](https://github.com/ChainSafe/forest/pull/4641) Added `build_info` + metric to Prometheus metrics, which include the current build's version. + ### Changed - [#4583](https://github.com/ChainSafe/forest/pull/4583) Removed the expiration diff --git a/src/utils/version/mod.rs b/src/utils/version/mod.rs index bb1de6fc4838..779a73e20fae 100644 --- a/src/utils/version/mod.rs +++ b/src/utils/version/mod.rs @@ -5,8 +5,8 @@ use git_version::git_version; use once_cell::sync::Lazy; use prometheus_client::{ collector::Collector, - encoding::{DescriptorEncoder, EncodeMetric}, - metrics::info::Info, + encoding::{DescriptorEncoder, EncodeLabelSet, EncodeMetric}, + metrics::{family::Family, gauge::Gauge}, }; /// Current git commit hash of the Forest repository. @@ -21,26 +21,41 @@ pub static FOREST_VERSION_STRING: Lazy = pub static FOREST_VERSION: Lazy = Lazy::new(|| semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid version")); +#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] +pub struct VersionLabel { + version: &'static str, +} + +impl VersionLabel { + pub const fn new(version: &'static str) -> Self { + Self { version } + } +} + #[derive(Debug)] pub struct ForestVersionCollector { - version: Info>, + version: Family, } impl ForestVersionCollector { pub fn new() -> Self { - let version = Info::new(vec![("version", FOREST_VERSION_STRING.as_str())]); - Self { version } + Self { + version: Family::default(), + } } } impl Collector for ForestVersionCollector { fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { let metric_encoder = encoder.encode_descriptor( - "forest_version", + "build_info", "semantic version of the forest binary", None, self.version.metric_type(), )?; + self.version + .get_or_create(&VersionLabel::new(FOREST_VERSION_STRING.as_str())) + .set(1); self.version.encode(metric_encoder)?; Ok(()) }