Skip to content

Commit

Permalink
feat: build info in metrics (#4641)
Browse files Browse the repository at this point in the history
  • Loading branch information
LesnyRumcajs authored Aug 14, 2024
1 parent 8891ed9 commit 6a41253
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
snapshots instead of moving or copying them. This can be invoked with
`--import-snapshot <path> --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.

- [#4628](https://github.com/ChainSafe/forest/issues/4628) Added support for
devnets (2k networks) in the offline Forest.

Expand Down
4 changes: 3 additions & 1 deletion src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
45 changes: 45 additions & 0 deletions src/utils/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

use git_version::git_version;
use once_cell::sync::Lazy;
use prometheus_client::{
collector::Collector,
encoding::{DescriptorEncoder, EncodeLabelSet, EncodeMetric},
metrics::{family::Family, gauge::Gauge},
};

/// Current git commit hash of the Forest repository.
pub const GIT_HASH: &str =
Expand All @@ -15,3 +20,43 @@ pub static FOREST_VERSION_STRING: Lazy<String> =

pub static FOREST_VERSION: Lazy<semver::Version> =
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: Family<VersionLabel, Gauge>,
}

impl ForestVersionCollector {
pub fn new() -> Self {
Self {
version: Family::default(),
}
}
}

impl Collector for ForestVersionCollector {
fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
let metric_encoder = encoder.encode_descriptor(
"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(())
}
}

0 comments on commit 6a41253

Please sign in to comment.