|
1 | 1 | //! Version information for the Base binary. |
| 2 | +//! |
| 3 | +//! [`VersionInfo`] metrics. |
| 4 | +//! |
| 5 | +//! Derived from [`reth-node-core`'s type][reth-version-info] |
| 6 | +//! |
| 7 | +//! [reth-version-info]: https://github.com/paradigmxyz/reth/blob/805fb1012cd1601c3b4fe9e8ca2d97c96f61355b/crates/node/metrics/src/version.rs#L6 |
| 8 | +
|
| 9 | +use metrics::gauge; |
| 10 | + |
| 11 | +/// Cargo package version. |
| 12 | +pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 13 | + |
| 14 | +/// Build timestamp from vergen. |
| 15 | +pub const VERGEN_BUILD_TIMESTAMP: &str = env!("VERGEN_BUILD_TIMESTAMP"); |
| 16 | + |
| 17 | +/// Cargo features from vergen. |
| 18 | +pub const VERGEN_CARGO_FEATURES: &str = env!("VERGEN_CARGO_FEATURES"); |
| 19 | + |
| 20 | +/// Git SHA from vergen. |
| 21 | +pub const VERGEN_GIT_SHA: &str = env!("VERGEN_GIT_SHA"); |
| 22 | + |
| 23 | +/// Cargo target triple from vergen. |
| 24 | +pub const VERGEN_CARGO_TARGET_TRIPLE: &str = env!("VERGEN_CARGO_TARGET_TRIPLE"); |
| 25 | + |
| 26 | +/// Build profile name. |
| 27 | +pub const BUILD_PROFILE_NAME: &str = env!("BASE_CONSENSUS_BUILD_PROFILE"); |
2 | 28 |
|
3 | 29 | /// Short version string. |
4 | | -pub(crate) const SHORT_VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 30 | +pub const SHORT_VERSION: &str = env!("BASE_CONSENSUS_SHORT_VERSION"); |
5 | 31 |
|
6 | 32 | /// Long version string with additional build info. |
7 | | -pub(crate) const LONG_VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\n", "Base Stack CLI"); |
| 33 | +pub const LONG_VERSION: &str = concat!( |
| 34 | + env!("BASE_CONSENSUS_LONG_VERSION_0"), |
| 35 | + "\n", |
| 36 | + env!("BASE_CONSENSUS_LONG_VERSION_1"), |
| 37 | + "\n", |
| 38 | + env!("BASE_CONSENSUS_LONG_VERSION_2"), |
| 39 | + "\n", |
| 40 | + env!("BASE_CONSENSUS_LONG_VERSION_3"), |
| 41 | + "\n", |
| 42 | + env!("BASE_CONSENSUS_LONG_VERSION_4") |
| 43 | +); |
| 44 | + |
| 45 | +/// Contains version information for the application and allows for exposing the contained |
| 46 | +/// information as a prometheus metric. |
| 47 | +#[derive(Debug, Clone)] |
| 48 | +pub struct VersionInfo { |
| 49 | + /// The version of the application. |
| 50 | + pub version: &'static str, |
| 51 | + /// The build timestamp of the application. |
| 52 | + pub build_timestamp: &'static str, |
| 53 | + /// The cargo features enabled for the build. |
| 54 | + pub cargo_features: &'static str, |
| 55 | + /// The Git SHA of the build. |
| 56 | + pub git_sha: &'static str, |
| 57 | + /// The target triple for the build. |
| 58 | + pub target_triple: &'static str, |
| 59 | + /// The build profile (e.g., debug or release). |
| 60 | + pub build_profile: &'static str, |
| 61 | +} |
| 62 | + |
| 63 | +impl VersionInfo { |
| 64 | + /// Creates a new instance of [`VersionInfo`] from the constants defined in [`crate::version`] |
| 65 | + /// at compile time. |
| 66 | + pub const fn from_build() -> Self { |
| 67 | + Self { |
| 68 | + version: CARGO_PKG_VERSION, |
| 69 | + build_timestamp: VERGEN_BUILD_TIMESTAMP, |
| 70 | + cargo_features: VERGEN_CARGO_FEATURES, |
| 71 | + git_sha: VERGEN_GIT_SHA, |
| 72 | + target_triple: VERGEN_CARGO_TARGET_TRIPLE, |
| 73 | + build_profile: BUILD_PROFILE_NAME, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Exposes kona-node's version information over prometheus. |
| 78 | + pub fn register_version_metrics(&self) { |
| 79 | + // If no features are enabled, the string will be empty, and the metric will not be |
| 80 | + // reported. Report "none" if the string is empty. |
| 81 | + let features = if self.cargo_features.is_empty() { "none" } else { self.cargo_features }; |
| 82 | + |
| 83 | + let labels: [(&str, &str); 6] = [ |
| 84 | + ("version", self.version), |
| 85 | + ("build_timestamp", self.build_timestamp), |
| 86 | + ("cargo_features", features), |
| 87 | + ("git_sha", self.git_sha), |
| 88 | + ("target_triple", self.target_triple), |
| 89 | + ("build_profile", self.build_profile), |
| 90 | + ]; |
| 91 | + |
| 92 | + let gauge = gauge!("kona_node_info", &labels); |
| 93 | + gauge.set(1); |
| 94 | + } |
| 95 | +} |
0 commit comments