From 7fa04e2d141ac878fd41f7ffab6271c7783592e3 Mon Sep 17 00:00:00 2001 From: Egor Suvorov Date: Fri, 8 Oct 2021 00:14:56 +0300 Subject: [PATCH] zenith_metrics: exit process on config errors (#706) --- zenith_metrics/src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/zenith_metrics/src/lib.rs b/zenith_metrics/src/lib.rs index c972bae72a..e3c3c81ee7 100644 --- a/zenith_metrics/src/lib.rs +++ b/zenith_metrics/src/lib.rs @@ -31,16 +31,29 @@ static COMMON_METRICS_PREFIX: OnceBox<&str> = OnceBox::new(); /// name like 'pageserver'. Should be executed exactly once in the beginning of /// any executable which uses common metrics. pub fn set_common_metrics_prefix(prefix: &'static str) { - COMMON_METRICS_PREFIX.set(prefix.into()).unwrap(); + // Not unwrap() because metrics may be initialized after multiple threads have been started. + COMMON_METRICS_PREFIX + .set(prefix.into()) + .unwrap_or_else(|_| { + eprintln!( + "set_common_metrics_prefix() was called second time with '{}', exiting", + prefix + ); + std::process::exit(1); + }); } /// Prepends a prefix to a common metric name so they are distinguished between /// different services, see https://github.com/zenithdb/zenith/pull/681 /// A call to set_common_metrics_prefix() is necessary prior to calling this. pub fn new_common_metric_name(unprefixed_metric_name: &str) -> String { + // Not unwrap() because metrics may be initialized after multiple threads have been started. format!( "{}_{}", - COMMON_METRICS_PREFIX.get().unwrap(), + COMMON_METRICS_PREFIX.get().unwrap_or_else(|| { + eprintln!("set_common_metrics_prefix() was not called, but metrics are used, exiting"); + std::process::exit(1); + }), unprefixed_metric_name ) }