diff --git a/crates/ethereum/cli/src/app.rs b/crates/ethereum/cli/src/app.rs index a36159a2fb..50a7bf64d7 100644 --- a/crates/ethereum/cli/src/app.rs +++ b/crates/ethereum/cli/src/app.rs @@ -108,7 +108,7 @@ where self.init_tracing(&runner)?; // Install the prometheus recorder to be sure to record all metrics - let _ = install_prometheus_recorder(); + install_prometheus_recorder(); run_commands_with::(self.cli, runner, components, launcher) } diff --git a/crates/ethereum/cli/src/interface.rs b/crates/ethereum/cli/src/interface.rs index 1c98612bdc..354748f1a7 100644 --- a/crates/ethereum/cli/src/interface.rs +++ b/crates/ethereum/cli/src/interface.rs @@ -212,7 +212,7 @@ impl< let _guard = self.init_tracing(&runner, Layers::new())?; // Install the prometheus recorder to be sure to record all metrics - let _ = install_prometheus_recorder(); + install_prometheus_recorder(); // Use the shared standalone function to avoid duplication run_commands_with::(self, runner, components, launcher) diff --git a/crates/node/metrics/src/recorder.rs b/crates/node/metrics/src/recorder.rs index d21d662b32..d09b5024f0 100644 --- a/crates/node/metrics/src/recorder.rs +++ b/crates/node/metrics/src/recorder.rs @@ -11,8 +11,25 @@ use std::sync::{atomic::AtomicBool, OnceLock}; /// /// Caution: This only configures the global recorder and does not spawn the exporter. /// Callers must run [`PrometheusRecorder::spawn_upkeep`] manually. +/// +/// Use [`init_prometheus_recorder`] to install a custom recorder. pub fn install_prometheus_recorder() -> &'static PrometheusRecorder { - PROMETHEUS_RECORDER_HANDLE.get_or_init(|| PrometheusRecorder::install().unwrap()) + PROMETHEUS_RECORDER_HANDLE.get_or_init(|| { + PrometheusRecorder::install().expect("Failed to install Prometheus recorder") + }) +} + +/// Installs the provided recorder as the global recorder. +/// +/// To customize the builder, first construct a recorder with +/// [`PrometheusRecorder::install_with_builder`], then pass it here. +/// +/// # Panics +/// +/// Panics if a recorder has already been installed. +pub fn init_prometheus_recorder(recorder: PrometheusRecorder) -> &'static PrometheusRecorder { + PROMETHEUS_RECORDER_HANDLE.set(recorder).expect("Prometheus recorder already installed"); + PROMETHEUS_RECORDER_HANDLE.get().expect("Prometheus recorder is set") } /// The default Prometheus recorder handle. We use a global static to ensure that it is only @@ -90,7 +107,11 @@ impl PrometheusRecorder { Self::install_with_builder(PrometheusBuilder::new()) } - fn install_with_builder(builder: PrometheusBuilder) -> eyre::Result { + /// Installs Prometheus as the metrics recorder with a custom builder. + /// + /// Caution: This only configures the global recorder and does not spawn the exporter. + /// Callers must run [`Self::spawn_upkeep`] manually. + pub fn install_with_builder(builder: PrometheusBuilder) -> eyre::Result { let recorder = builder.build_recorder(); let handle = recorder.handle(); diff --git a/crates/optimism/cli/src/app.rs b/crates/optimism/cli/src/app.rs index a23873daad..8785338e5e 100644 --- a/crates/optimism/cli/src/app.rs +++ b/crates/optimism/cli/src/app.rs @@ -67,7 +67,7 @@ where self.init_tracing(&runner)?; // Install the prometheus recorder to be sure to record all metrics - let _ = install_prometheus_recorder(); + install_prometheus_recorder(); let components = |spec: Arc| { (OpExecutorProvider::optimism(spec.clone()), Arc::new(OpBeaconConsensus::new(spec)))