From e4bfeed07c1404f86bd226b7e557ab548ad3b31e Mon Sep 17 00:00:00 2001 From: Andreas Bigger Date: Wed, 14 Jan 2026 08:25:46 -0500 Subject: [PATCH] chore(bin): consensus client versioning --- Cargo.lock | 1 + bin/consensus/Cargo.toml | 2 +- bin/consensus/src/main.rs | 3 +- bin/consensus/src/version.rs | 92 +++++++++++++++++++++++++++++++++++- 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 00595b1c..a8495096 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1741,6 +1741,7 @@ dependencies = [ "base-cli-utils", "clap", "eyre", + "metrics", "vergen", "vergen-git2", ] diff --git a/bin/consensus/Cargo.toml b/bin/consensus/Cargo.toml index e8d0da14..abbd4b96 100644 --- a/bin/consensus/Cargo.toml +++ b/bin/consensus/Cargo.toml @@ -19,7 +19,7 @@ base-cli-utils.workspace = true # CLI clap.workspace = true eyre.workspace = true - +metrics.workspace = true [build-dependencies] vergen = { workspace = true, features = ["build", "cargo", "emit_and_set"] } diff --git a/bin/consensus/src/main.rs b/bin/consensus/src/main.rs index 75778ec1..ab12147b 100644 --- a/bin/consensus/src/main.rs +++ b/bin/consensus/src/main.rs @@ -4,8 +4,7 @@ #![cfg_attr(not(test), warn(unused_crate_dependencies))] pub mod cli; - -pub(crate) mod version; +pub mod version; fn main() { base_cli_utils::Backtracing::enable(); diff --git a/bin/consensus/src/version.rs b/bin/consensus/src/version.rs index 75874a4e..af57c214 100644 --- a/bin/consensus/src/version.rs +++ b/bin/consensus/src/version.rs @@ -1,7 +1,95 @@ //! Version information for the Base binary. +//! +//! [`VersionInfo`] metrics. +//! +//! Derived from [`reth-node-core`'s type][reth-version-info] +//! +//! [reth-version-info]: https://github.com/paradigmxyz/reth/blob/805fb1012cd1601c3b4fe9e8ca2d97c96f61355b/crates/node/metrics/src/version.rs#L6 + +use metrics::gauge; + +/// Cargo package version. +pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); + +/// Build timestamp from vergen. +pub const VERGEN_BUILD_TIMESTAMP: &str = env!("VERGEN_BUILD_TIMESTAMP"); + +/// Cargo features from vergen. +pub const VERGEN_CARGO_FEATURES: &str = env!("VERGEN_CARGO_FEATURES"); + +/// Git SHA from vergen. +pub const VERGEN_GIT_SHA: &str = env!("VERGEN_GIT_SHA"); + +/// Cargo target triple from vergen. +pub const VERGEN_CARGO_TARGET_TRIPLE: &str = env!("VERGEN_CARGO_TARGET_TRIPLE"); + +/// Build profile name. +pub const BUILD_PROFILE_NAME: &str = env!("BASE_CONSENSUS_BUILD_PROFILE"); /// Short version string. -pub(crate) const SHORT_VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const SHORT_VERSION: &str = env!("BASE_CONSENSUS_SHORT_VERSION"); /// Long version string with additional build info. -pub(crate) const LONG_VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\n", "Base Stack CLI"); +pub const LONG_VERSION: &str = concat!( + env!("BASE_CONSENSUS_LONG_VERSION_0"), + "\n", + env!("BASE_CONSENSUS_LONG_VERSION_1"), + "\n", + env!("BASE_CONSENSUS_LONG_VERSION_2"), + "\n", + env!("BASE_CONSENSUS_LONG_VERSION_3"), + "\n", + env!("BASE_CONSENSUS_LONG_VERSION_4") +); + +/// Contains version information for the application and allows for exposing the contained +/// information as a prometheus metric. +#[derive(Debug, Clone)] +pub struct VersionInfo { + /// The version of the application. + pub version: &'static str, + /// The build timestamp of the application. + pub build_timestamp: &'static str, + /// The cargo features enabled for the build. + pub cargo_features: &'static str, + /// The Git SHA of the build. + pub git_sha: &'static str, + /// The target triple for the build. + pub target_triple: &'static str, + /// The build profile (e.g., debug or release). + pub build_profile: &'static str, +} + +impl VersionInfo { + /// Creates a new instance of [`VersionInfo`] from the constants defined in [`crate::version`] + /// at compile time. + pub const fn from_build() -> Self { + Self { + version: CARGO_PKG_VERSION, + build_timestamp: VERGEN_BUILD_TIMESTAMP, + cargo_features: VERGEN_CARGO_FEATURES, + git_sha: VERGEN_GIT_SHA, + target_triple: VERGEN_CARGO_TARGET_TRIPLE, + build_profile: BUILD_PROFILE_NAME, + } + } + + /// Exposes kona-node's version information over prometheus. + pub fn register_version_metrics(&self) { + // If no features are enabled, the string will be empty, and the metric will not be + // reported. Report "none" if the string is empty. + let features = if self.cargo_features.is_empty() { "none" } else { self.cargo_features }; + + let labels: [(&str, &str); 6] = [ + ("version", self.version), + ("build_timestamp", self.build_timestamp), + ("cargo_features", features), + ("git_sha", self.git_sha), + ("target_triple", self.target_triple), + ("build_profile", self.build_profile), + ]; + + let gauge = gauge!("kona_node_info", &labels); + gauge.set(1); + } +}