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)]
25pub struct VersionLabel {
26 version: &'static str,
27}
28
29impl VersionLabel {
30 pub const fn new(version: &'static str) -> Self {
31 Self { version }
32 }
33}
34
35#[derive(Debug)]
36pub struct ForestVersionCollector {
37 version: Family<VersionLabel, Gauge>,
38}
39
40impl ForestVersionCollector {
41 pub fn new() -> Self {
42 Self {
43 version: Family::default(),
44 }
45 }
46}
47
48impl Collector for ForestVersionCollector {
49 fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
50 let metric_encoder = encoder.encode_descriptor(
51 "build_info",
52 "semantic version of the forest binary",
53 None,
54 self.version.metric_type(),
55 )?;
56 self.version
57 .get_or_create(&VersionLabel::new(FOREST_VERSION_STRING.as_str()))
58 .set(1);
59 self.version.encode(metric_encoder)?;
60 Ok(())
61 }
62}