Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metric): add forest version to prometheus #4535

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
30 changes: 30 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, EncodeMetric},
metrics::info::Info,
};

/// Current git commit hash of the Forest repository.
pub const GIT_HASH: &str =
Expand All @@ -15,3 +20,28 @@ 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(Debug)]
pub struct ForestVersionCollector {
version: Info<Vec<(&'static str, &'static str)>>,
}

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(())
}
}
Loading