forest/utils/version/
mod.rs1use git_version::git_version;
5use prometheus_client::{
6 collector::Collector,
7 encoding::{DescriptorEncoder, EncodeLabelSet, EncodeMetric},
8 metrics::{family::Family, gauge::Gauge},
9};
10use std::sync::LazyLock;
11
12pub const GIT_HASH: &str =
14 git_version!(args = ["--always", "--exclude", "*"], fallback = "unknown");
15
16pub static FOREST_VERSION_STRING: LazyLock<String> =
19 LazyLock::new(|| format!("{}+git.{}", env!("CARGO_PKG_VERSION"), GIT_HASH));
20
21pub static FOREST_VERSION: LazyLock<semver::Version> =
22 LazyLock::new(|| semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid version"));
23
24#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet, derive_more::Constructor)]
25pub struct VersionLabel {
26 version: &'static str,
27}
28
29#[derive(Debug)]
30pub struct ForestVersionCollector {
31 version: Family<VersionLabel, Gauge>,
32}
33
34impl ForestVersionCollector {
35 pub fn new() -> Self {
36 Self {
37 version: Family::default(),
38 }
39 }
40}
41
42impl Collector for ForestVersionCollector {
43 fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
44 let metric_encoder = encoder.encode_descriptor(
45 "build_info",
46 "semantic version of the forest binary",
47 None,
48 self.version.metric_type(),
49 )?;
50 self.version
51 .get_or_create(&VersionLabel::new(FOREST_VERSION_STRING.as_str()))
52 .set(1);
53 self.version.encode(metric_encoder)?;
54 Ok(())
55 }
56}