Skip to content

Commit 19e6754

Browse files
committed
chore(cli-utils): metrics args
1 parent ab40b94 commit 19e6754

File tree

5 files changed

+139
-7
lines changed

5 files changed

+139
-7
lines changed

crates/shared/cli-utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ kona-registry.workspace = true
2727

2828
# Tracing
2929
tracing.workspace = true
30+
reth-node-core.workspace = true
3031
tracing-appender.workspace = true
3132
tracing-subscriber = { workspace = true, features = ["fmt", "env-filter", "json", "tracing-log"] }
32-
reth-node-core.workspace = true
3333

3434
# Metrics
3535
metrics-process = { workspace = true, features = ["metrics-rs"] }

crates/shared/cli-utils/src/args.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use clap::{ArgAction, Parser};
77
use kona_registry::OPCHAINS;
88

99
use crate::{
10-
FileLogConfig, LogConfig, LogFormat, LogRotation, StdoutLogConfig, verbosity_to_level_filter,
10+
FileLogConfig, LogConfig, LogFormat, LogRotation, MetricsArgs, StdoutLogConfig,
11+
verbosity_to_level_filter,
1112
};
1213

1314
/// Log-related CLI arguments.
@@ -98,6 +99,10 @@ pub struct GlobalArgs {
9899
/// Logging configuration.
99100
#[command(flatten)]
100101
pub logging: LogArgs,
102+
103+
/// Prometheus CLI arguments.
104+
#[command(flatten)]
105+
pub metrics: MetricsArgs,
101106
}
102107

103108
impl GlobalArgs {

crates/shared/cli-utils/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ pub use sigsegv::SigsegvHandler;
99
mod backtrace;
1010
pub use backtrace::Backtracing;
1111

12-
mod args;
13-
pub use args::{GlobalArgs, LogArgs};
12+
mod metrics;
13+
pub use metrics::MetricsArgs;
1414

1515
mod prometheus;
1616
pub use prometheus::PrometheusServer;
1717

18+
mod args;
19+
pub use args::{GlobalArgs, LogArgs};
20+
21+
mod styles;
22+
pub use styles::CliStyles;
23+
1824
mod logging;
1925
pub use logging::{
2026
FileLogConfig, LogConfig, LogFormat, LogRotation, StdoutLogConfig, verbosity_to_level_filter,
@@ -28,6 +34,3 @@ pub use version::Version;
2834

2935
mod runtime;
3036
pub use runtime::RuntimeManager;
31-
32-
mod styles;
33-
pub use styles::CliStyles;
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//! Utility module to house implementation and declaration of MetricsArgs since it's being used in
2+
//! multiple places, it's just being referenced from this module.
3+
4+
use std::net::IpAddr;
5+
6+
use clap::Parser;
7+
8+
use crate::PrometheusServer;
9+
10+
/// Configuration for Prometheus metrics.
11+
#[derive(Debug, Clone, Parser)]
12+
#[command(next_help_heading = "Metrics")]
13+
pub struct MetricsArgs {
14+
/// Controls whether Prometheus metrics are enabled. Disabled by default.
15+
#[arg(
16+
long = "metrics.enabled",
17+
global = true,
18+
default_value_t = false,
19+
env = "KONA_METRICS_ENABLED"
20+
)]
21+
pub enabled: bool,
22+
23+
/// The port to serve Prometheus metrics on.
24+
#[arg(long = "metrics.port", global = true, default_value = "9090", env = "KONA_METRICS_PORT")]
25+
pub port: u16,
26+
27+
/// The IP address to use for Prometheus metrics.
28+
#[arg(
29+
long = "metrics.addr",
30+
global = true,
31+
default_value = "0.0.0.0",
32+
env = "KONA_METRICS_ADDR"
33+
)]
34+
pub addr: IpAddr,
35+
}
36+
37+
impl Default for MetricsArgs {
38+
fn default() -> Self {
39+
Self::parse_from::<[_; 0], &str>([])
40+
}
41+
}
42+
43+
impl MetricsArgs {
44+
/// Initialize the Prometheus metrics recorder.
45+
///
46+
/// This function should be called at the beginning of the program.
47+
pub fn init(&self) -> Result<(), metrics_exporter_prometheus::BuildError> {
48+
if self.enabled {
49+
PrometheusServer::init(self.addr, self.port, None)?;
50+
}
51+
52+
Ok(())
53+
}
54+
55+
/// Initialize the Prometheus metrics recorder and run a callback to initialize
56+
/// subsystem-specific metrics if metrics are enabled.
57+
///
58+
/// This function should be called at the beginning of the program.
59+
pub fn init_with<F>(&self, f: F) -> Result<(), metrics_exporter_prometheus::BuildError>
60+
where
61+
F: FnOnce(),
62+
{
63+
if self.enabled {
64+
PrometheusServer::init(self.addr, self.port, None)?;
65+
f();
66+
}
67+
68+
Ok(())
69+
}
70+
}
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use std::net::{IpAddr, Ipv4Addr};
75+
76+
use clap::Parser;
77+
78+
use super::*;
79+
80+
/// Helper struct to parse MetricsArgs within a test CLI structure.
81+
#[derive(Parser, Debug)]
82+
struct TestCli {
83+
#[command(flatten)]
84+
metrics: MetricsArgs,
85+
}
86+
87+
#[test]
88+
fn test_default_metrics_args() {
89+
let cli = TestCli::parse_from(["test_app"]);
90+
assert!(!cli.metrics.enabled, "Default for metrics.enabled should be false.");
91+
assert_eq!(cli.metrics.port, 9090, "Default for metrics.port should be 9090.");
92+
assert_eq!(
93+
cli.metrics.addr,
94+
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
95+
"Default for metrics.addr should be 0.0.0.0."
96+
);
97+
}
98+
99+
#[test]
100+
fn test_metrics_args_from_cli() {
101+
let cli = TestCli::parse_from([
102+
"test_app",
103+
"--metrics.enabled",
104+
"--metrics.port",
105+
"9999",
106+
"--metrics.addr",
107+
"127.0.0.1",
108+
]);
109+
assert!(cli.metrics.enabled, "metrics.enabled should be true.");
110+
assert_eq!(cli.metrics.port, 9999, "metrics.port should be parsed from CLI.");
111+
assert_eq!(
112+
cli.metrics.addr,
113+
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
114+
"metrics.addr should be parsed from CLI."
115+
);
116+
}
117+
}

crates/shared/cli-utils/src/tracing.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ impl LogConfig {
7878
let filter =
7979
EnvFilter::builder().with_default_directive(self.global_level.into()).from_env_lossy();
8080

81+
self.init_tracing_subscriber_with_filter(filter)
82+
}
83+
84+
/// Initialize the tracing subscriber with a custom filter.
85+
///
86+
/// This sets the global default subscriber. Should only be called once.
87+
pub fn init_tracing_subscriber_with_filter(&self, filter: EnvFilter) -> eyre::Result<()> {
8188
let registry = tracing_subscriber::registry().with(filter);
8289

8390
// Build stdout layer

0 commit comments

Comments
 (0)