fix: harden typed metrics review findings
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
sync::{Mutex, OnceLock},
|
||||
sync::{Arc, Mutex, OnceLock},
|
||||
};
|
||||
|
||||
use crate::{DURATION_BUCKETS_SECONDS, max_exemplar_slots};
|
||||
@@ -44,8 +44,9 @@ struct ExemplarKey {
|
||||
bucket_index: usize,
|
||||
}
|
||||
|
||||
fn store() -> &'static Mutex<BTreeMap<ExemplarKey, ExemplarObservation>> {
|
||||
static STORE: OnceLock<Mutex<BTreeMap<ExemplarKey, ExemplarObservation>>> = OnceLock::new();
|
||||
fn store() -> &'static Mutex<BTreeMap<ExemplarKey, Arc<ExemplarObservation>>> {
|
||||
static STORE: OnceLock<Mutex<BTreeMap<ExemplarKey, Arc<ExemplarObservation>>>> =
|
||||
OnceLock::new();
|
||||
STORE.get_or_init(|| Mutex::new(BTreeMap::new()))
|
||||
}
|
||||
|
||||
@@ -76,20 +77,13 @@ pub(crate) fn record_exemplar(
|
||||
bucket_index,
|
||||
};
|
||||
if observations.contains_key(&key) || observations.len() < max_exemplar_slots() {
|
||||
observations.insert(key, observation);
|
||||
observations.insert(key, Arc::new(observation));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exemplar_snapshot() -> Vec<ExemplarObservation> {
|
||||
pub fn exemplar_snapshot() -> Vec<Arc<ExemplarObservation>> {
|
||||
store()
|
||||
.lock()
|
||||
.map(|observations| observations.values().cloned().collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn reset_exemplars_for_test() {
|
||||
if let Ok(mut observations) = store().lock() {
|
||||
observations.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user