feat: make metrics layer configurable (#20703)

This commit is contained in:
Karl Yu
2026-01-06 03:30:42 +08:00
committed by GitHub
parent b6f95866cc
commit e9e940919a
4 changed files with 26 additions and 5 deletions

View File

@@ -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::<C, Ext, Rpc, N, SubCmd>(self.cli, runner, components, launcher)
}

View File

@@ -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::<C, Ext, Rpc, N, SubCmd>(self, runner, components, launcher)

View File

@@ -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<Self> {
/// 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<Self> {
let recorder = builder.build_recorder();
let handle = recorder.handle();

View File

@@ -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<OpChainSpec>| {
(OpExecutorProvider::optimism(spec.clone()), Arc::new(OpBeaconConsensus::new(spec)))