Skip to content

Commit ca41efa

Browse files
author
Solar Mithril
committed
Add version metric to op-rbuilder
1 parent 95c2409 commit ca41efa

File tree

6 files changed

+200
-1
lines changed

6 files changed

+200
-1
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,5 @@ tracing = "0.1.37"
161161
metrics = { version = "0.24.1" }
162162
ahash = "0.8.6"
163163
time = { version = "0.3.36", features = ["macros", "formatting", "parsing"] }
164+
vergen = "9.0.4"
165+
vergen-git2 = "1.0.5"

crates/op-rbuilder/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ rollup-boost = { git = "https://github.com/flashbots/rollup-boost", rev = "608853
9696
[target.'cfg(unix)'.dependencies]
9797
tikv-jemallocator = { version = "0.6", optional = true }
9898

99+
[build-dependencies]
100+
vergen = { workspace = true, features = ["build", "cargo", "emit_and_set"] }
101+
vergen-git2.workspace = true
102+
99103
[features]
100104
default = ["jemalloc"]
101105

crates/op-rbuilder/build.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Taken from reth [https://github.com/paradigmxyz/reth/blob/main/crates/node/core/build.rs]
2+
// The MIT License (MIT)
3+
//
4+
// Copyright (c) 2022-2025 Reth Contributors
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
use std::{env, error::Error};
25+
use vergen::{BuildBuilder, CargoBuilder, Emitter};
26+
use vergen_git2::Git2Builder;
27+
28+
fn main() -> Result<(), Box<dyn Error>> {
29+
let mut emitter = Emitter::default();
30+
31+
let build_builder = BuildBuilder::default().build_timestamp(true).build()?;
32+
33+
emitter.add_instructions(&build_builder)?;
34+
35+
let cargo_builder = CargoBuilder::default()
36+
.features(true)
37+
.target_triple(true)
38+
.build()?;
39+
40+
emitter.add_instructions(&cargo_builder)?;
41+
42+
let git_builder = Git2Builder::default()
43+
.describe(false, true, None)
44+
.dirty(true)
45+
.sha(false)
46+
.build()?;
47+
48+
emitter.add_instructions(&git_builder)?;
49+
50+
emitter.emit_and_set()?;
51+
let sha = env::var("VERGEN_GIT_SHA")?;
52+
let sha_short = &sha[0..7];
53+
54+
let is_dirty = env::var("VERGEN_GIT_DIRTY")? == "true";
55+
// > git describe --always --tags
56+
// if not on a tag: v0.2.0-beta.3-82-g1939939b
57+
// if on a tag: v0.2.0-beta.3
58+
let not_on_tag = env::var("VERGEN_GIT_DESCRIBE")?.ends_with(&format!("-g{sha_short}"));
59+
let version_suffix = if is_dirty || not_on_tag { "-dev" } else { "" };
60+
println!("cargo:rustc-env=OP_RBUILDER_VERSION_SUFFIX={version_suffix}");
61+
62+
// Set short SHA
63+
println!("cargo:rustc-env=VERGEN_GIT_SHA_SHORT={}", &sha[..8]);
64+
65+
// Set the build profile
66+
let out_dir = env::var("OUT_DIR").unwrap();
67+
let profile = out_dir.rsplit(std::path::MAIN_SEPARATOR).nth(3).unwrap();
68+
println!("cargo:rustc-env=OP_RBUILDER_BUILD_PROFILE={profile}");
69+
70+
// Set formatted version strings
71+
let pkg_version = env!("CARGO_PKG_VERSION");
72+
73+
// The short version information for op-rbuilder.
74+
// - The latest version from Cargo.toml
75+
// - The short SHA of the latest commit.
76+
// Example: 0.1.0 (defa64b2)
77+
println!(
78+
"cargo:rustc-env=OP_RBUILDER_SHORT_VERSION={pkg_version}{version_suffix} ({sha_short})"
79+
);
80+
81+
// LONG_VERSION
82+
// The long version information for op-rbuilder.
83+
//
84+
// - The latest version from Cargo.toml + version suffix (if any)
85+
// - The full SHA of the latest commit
86+
// - The build datetime
87+
// - The build features
88+
// - The build profile
89+
//
90+
// Example:
91+
//
92+
// ```text
93+
// Version: 0.1.0
94+
// Commit SHA: defa64b2
95+
// Build Timestamp: 2023-05-19T01:47:19.815651705Z
96+
// Build Features: jemalloc
97+
// Build Profile: maxperf
98+
// ```
99+
println!("cargo:rustc-env=OP_RBUILDER_LONG_VERSION_0=Version: {pkg_version}{version_suffix}");
100+
println!("cargo:rustc-env=OP_RBUILDER_LONG_VERSION_1=Commit SHA: {sha}");
101+
println!(
102+
"cargo:rustc-env=OP_RBUILDER_LONG_VERSION_2=Build Timestamp: {}",
103+
env::var("VERGEN_BUILD_TIMESTAMP")?
104+
);
105+
println!(
106+
"cargo:rustc-env=OP_RBUILDER_LONG_VERSION_3=Build Features: {}",
107+
env::var("VERGEN_CARGO_FEATURES")?
108+
);
109+
println!("cargo:rustc-env=OP_RBUILDER_LONG_VERSION_4=Build Profile: {profile}");
110+
111+
// The version information for op-rbuilder formatted for P2P (devp2p).
112+
// - The latest version from Cargo.toml
113+
// - The target triple
114+
//
115+
// Example: op-rbuilder/v0.1.0-alpha.1-428a6dc2f/aarch64-apple-darwin
116+
println!(
117+
"cargo:rustc-env=OP_RBUILDER_P2P_CLIENT_VERSION={}",
118+
format_args!(
119+
"op-rbuilder/v{pkg_version}-{sha_short}/{}",
120+
env::var("VERGEN_CARGO_TARGET_TRIPLE")?
121+
)
122+
);
123+
124+
Ok(())
125+
}

crates/op-rbuilder/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ mod primitives;
2323
#[cfg(test)]
2424
mod tester;
2525
mod tx_signer;
26+
use metrics::{
27+
VersionInfo, BUILD_PROFILE_NAME, CARGO_PKG_VERSION, VERGEN_BUILD_TIMESTAMP,
28+
VERGEN_CARGO_FEATURES, VERGEN_CARGO_TARGET_TRIPLE, VERGEN_GIT_SHA,
29+
};
2630
use monitor_tx_pool::monitor_tx_pool;
2731

2832
// Prefer jemalloc for performance reasons.
@@ -31,6 +35,15 @@ use monitor_tx_pool::monitor_tx_pool;
3135
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
3236

3337
fn main() {
38+
let version = VersionInfo {
39+
version: CARGO_PKG_VERSION,
40+
build_timestamp: VERGEN_BUILD_TIMESTAMP,
41+
cargo_features: VERGEN_CARGO_FEATURES,
42+
git_sha: VERGEN_GIT_SHA,
43+
target_triple: VERGEN_CARGO_TARGET_TRIPLE,
44+
build_profile: BUILD_PROFILE_NAME,
45+
};
46+
3447
Cli::<OpChainSpecParser, args::OpRbuilderArgs>::parse()
3548
.run(|builder, builder_args| async move {
3649
let rollup_args = builder_args.rollup_args;
@@ -53,6 +66,7 @@ fn main() {
5366
.build(),
5467
)
5568
.on_node_started(move |ctx| {
69+
version.register_version_metrics();
5670
if builder_args.log_pool_transactions {
5771
tracing::info!("Logging pool transactions");
5872
ctx.task_executor.spawn_critical(

crates/op-rbuilder/src/metrics.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
use reth_metrics::{
2-
metrics::{Counter, Histogram},
2+
metrics::{gauge, Counter, Histogram},
33
Metrics,
44
};
55

6+
/// The latest version from Cargo.toml.
7+
pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
8+
9+
/// The 8 character short SHA of the latest commit.
10+
pub const VERGEN_GIT_SHA: &str = env!("VERGEN_GIT_SHA_SHORT");
11+
12+
/// The build timestamp.
13+
pub const VERGEN_BUILD_TIMESTAMP: &str = env!("VERGEN_BUILD_TIMESTAMP");
14+
15+
/// The target triple.
16+
pub const VERGEN_CARGO_TARGET_TRIPLE: &str = env!("VERGEN_CARGO_TARGET_TRIPLE");
17+
18+
/// The build features.
19+
pub const VERGEN_CARGO_FEATURES: &str = env!("VERGEN_CARGO_FEATURES");
20+
21+
/// The build profile name.
22+
pub const BUILD_PROFILE_NAME: &str = env!("OP_RBUILDER_BUILD_PROFILE");
23+
624
/// op-rbuilder metrics
725
#[derive(Metrics, Clone)]
826
#[metrics(scope = "op_rbuilder")]
@@ -50,3 +68,37 @@ pub struct OpRBuilderMetrics {
5068
/// Byte size of transactions
5169
pub tx_byte_size: Histogram,
5270
}
71+
72+
/// Contains version information for the application.
73+
#[derive(Debug, Clone)]
74+
pub struct VersionInfo {
75+
/// The version of the application.
76+
pub version: &'static str,
77+
/// The build timestamp of the application.
78+
pub build_timestamp: &'static str,
79+
/// The cargo features enabled for the build.
80+
pub cargo_features: &'static str,
81+
/// The Git SHA of the build.
82+
pub git_sha: &'static str,
83+
/// The target triple for the build.
84+
pub target_triple: &'static str,
85+
/// The build profile (e.g., debug or release).
86+
pub build_profile: &'static str,
87+
}
88+
89+
impl VersionInfo {
90+
/// This exposes reth's version information over prometheus.
91+
pub fn register_version_metrics(&self) {
92+
let labels: [(&str, &str); 6] = [
93+
("version", self.version),
94+
("build_timestamp", self.build_timestamp),
95+
("cargo_features", self.cargo_features),
96+
("git_sha", self.git_sha),
97+
("target_triple", self.target_triple),
98+
("build_profile", self.build_profile),
99+
];
100+
101+
let gauge = gauge!("builder_info", &labels);
102+
gauge.set(1);
103+
}
104+
}

0 commit comments

Comments
 (0)