Skip to content

Commit 2fdd3a5

Browse files
committed
chore(bin): refactor out cli logic
1 parent 8f7681d commit 2fdd3a5

File tree

5 files changed

+45
-81
lines changed

5 files changed

+45
-81
lines changed

Cargo.lock

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

bin/consensus/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ clap.workspace = true
3636
eyre.workspace = true
3737
strum.workspace = true
3838
serde_json.workspace = true
39-
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
4039

4140
# Tracing
4241
tracing.workspace = true

bin/consensus/src/cli.rs

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{fs::File, path::PathBuf, sync::Arc, time::Duration};
44

5-
use base_cli_utils::{CliStyles, GlobalArgs, LogConfig, MetricsArgs};
5+
use base_cli_utils::{CliStyles, GlobalArgs, LogConfig, RuntimeManager};
66
use base_client_cli::{
77
BuilderClientArgs, JwtSecret, L1ClientArgs, L2ClientArgs, P2PArgs, RollupBoostFlags, RpcArgs,
88
SequencerArgs,
@@ -89,10 +89,18 @@ impl Cli {
8989
Self::init_logs(&self.global)?;
9090

9191
// Initialize unified metrics
92-
Self::init_metrics(&self.global.metrics)?;
92+
self.global.metrics.init_with(|| {
93+
kona_gossip::Metrics::init();
94+
kona_disc::Metrics::init();
95+
kona_engine::Metrics::init();
96+
kona_node_service::Metrics::init();
97+
kona_derive::Metrics::init();
98+
kona_providers_alloy::Metrics::init();
99+
version::VersionInfo::from_build().register_version_metrics();
100+
})?;
93101

94102
// Run the subcommand.
95-
Self::run_until_ctrl_c(self.exec(&self.global))
103+
RuntimeManager::run_until_ctrl_c(self.exec(&self.global))
96104
}
97105

98106
/// Run the Node subcommand.
@@ -241,45 +249,4 @@ impl Cli {
241249
LogConfig::new(args.logging.clone()).init_tracing_subscriber(Some(filter))?;
242250
Ok(())
243251
}
244-
245-
/// Initializes metrics for a Kona application, including Prometheus and node-specific metrics.
246-
/// Initialize the tracing stack and Prometheus metrics recorder.
247-
///
248-
/// This function should be called at the beginning of the program.
249-
pub fn init_metrics(args: &MetricsArgs) -> eyre::Result<()> {
250-
args.init_metrics()?;
251-
if args.enabled {
252-
kona_gossip::Metrics::init();
253-
kona_disc::Metrics::init();
254-
kona_engine::Metrics::init();
255-
kona_node_service::Metrics::init();
256-
kona_derive::Metrics::init();
257-
kona_providers_alloy::Metrics::init();
258-
version::VersionInfo::from_build().register_version_metrics();
259-
}
260-
Ok(())
261-
}
262-
263-
/// Run until ctrl-c is pressed.
264-
pub fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
265-
where
266-
F: std::future::Future<Output = eyre::Result<()>>,
267-
{
268-
let rt = Self::tokio_runtime().map_err(|e| eyre::eyre!(e))?;
269-
rt.block_on(async move {
270-
tokio::select! {
271-
res = fut => res,
272-
_ = tokio::signal::ctrl_c() => {
273-
tracing::info!(target: "cli", "Received Ctrl-C, shutting down...");
274-
Ok(())
275-
}
276-
}
277-
})
278-
}
279-
280-
/// Creates a new default tokio multi-thread [Runtime](tokio::runtime::Runtime) with all
281-
/// features enabled
282-
pub fn tokio_runtime() -> Result<tokio::runtime::Runtime, std::io::Error> {
283-
tokio::runtime::Builder::new_multi_thread().enable_all().build()
284-
}
285252
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,32 @@ impl Default for MetricsArgs {
4141
}
4242

4343
impl MetricsArgs {
44-
/// Initialize the tracing stack and Prometheus metrics recorder.
44+
/// Initialize the Prometheus metrics recorder.
4545
///
4646
/// This function should be called at the beginning of the program.
47-
pub fn init_metrics(&self) -> Result<(), metrics_exporter_prometheus::BuildError> {
47+
pub fn init(&self) -> Result<(), metrics_exporter_prometheus::BuildError> {
4848
if self.enabled {
4949
PrometheusServer::init(self.addr, self.port)?;
5050
}
5151

5252
Ok(())
5353
}
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)?;
65+
f();
66+
}
67+
68+
Ok(())
69+
}
5470
}
5571

5672
#[cfg(test)]

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

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,26 @@ use std::future::Future;
77
pub struct RuntimeManager;
88

99
impl RuntimeManager {
10-
/// Builds a multi-threaded Tokio runtime with all features enabled.
11-
pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> {
12-
tokio::runtime::Builder::new_multi_thread()
13-
.enable_all()
14-
.build()
15-
.map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e))
10+
/// Creates a new default tokio multi-thread [Runtime](tokio::runtime::Runtime) with all
11+
/// features enabled.
12+
pub fn tokio_runtime() -> Result<tokio::runtime::Runtime, std::io::Error> {
13+
tokio::runtime::Builder::new_multi_thread().enable_all().build()
1614
}
1715

18-
/// Runs a future to completion, returning early on Ctrl+C.
19-
pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
20-
where
21-
F: Future<Output = ()>,
22-
{
23-
let ctrl_c = async {
24-
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
25-
};
26-
27-
tokio::select! {
28-
biased;
29-
() = ctrl_c => Ok(()),
30-
() = fut => Ok(()),
31-
}
32-
}
33-
34-
/// Runs a fallible future to completion, returning early on Ctrl+C.
35-
pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()>
16+
/// Run a fallible future until ctrl-c is pressed.
17+
pub fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
3618
where
3719
F: Future<Output = eyre::Result<()>>,
3820
{
39-
let ctrl_c = async {
40-
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
41-
};
42-
43-
tokio::select! {
44-
biased;
45-
() = ctrl_c => Ok(()),
46-
result = fut => result,
47-
}
21+
let rt = Self::tokio_runtime().map_err(|e| eyre::eyre!(e))?;
22+
rt.block_on(async move {
23+
tokio::select! {
24+
res = fut => res,
25+
_ = tokio::signal::ctrl_c() => {
26+
tracing::info!(target: "cli", "Received Ctrl-C, shutting down...");
27+
Ok(())
28+
}
29+
}
30+
})
4831
}
4932
}

0 commit comments

Comments
 (0)