use std::{
collections::HashMap,
sync::{atomic::AtomicU64, Arc, Mutex, PoisonError, RwLock},
time::Instant,
};
use metrics::{
Counter, Gauge, Histogram, HistogramFn, Key, KeyName, Metadata, Recorder, SharedString, Unit,
};
use super::{summary::RollingSummary, text};
#[derive(Clone, Debug, Default)]
pub(super) struct PrometheusRecorder {
metrics: Arc<Metrics>,
}
#[derive(Debug, Default)]
pub(super) struct Metrics {
pub(super) counters: RwLock<HashMap<Key, Arc<AtomicU64>>>,
pub(super) gauges: RwLock<HashMap<Key, Arc<AtomicU64>>>,
pub(super) histograms: RwLock<HashMap<Key, Arc<HistogramSamples>>>,
pub(super) descriptions: RwLock<HashMap<String, SharedString>>,
}
impl PrometheusRecorder {
pub(super) fn render(&self, now: Instant) -> String {
text::render(&self.metrics, now)
}
fn describe(&self, name: &KeyName, description: SharedString) {
write(&self.metrics.descriptions)
.entry(text::sanitize_metric_name(name.as_str()))
.or_insert(description);
}
}
impl Recorder for PrometheusRecorder {
fn describe_counter(&self, name: KeyName, _unit: Option<Unit>, description: SharedString) {
self.describe(&name, description);
}
fn describe_gauge(&self, name: KeyName, _unit: Option<Unit>, description: SharedString) {
self.describe(&name, description);
}
fn describe_histogram(&self, name: KeyName, _unit: Option<Unit>, description: SharedString) {
self.describe(&name, description);
}
fn register_counter(&self, key: &Key, _metadata: &Metadata<'_>) -> Counter {
Counter::from_arc(get_or_insert(&self.metrics.counters, key, Default::default))
}
fn register_gauge(&self, key: &Key, _metadata: &Metadata<'_>) -> Gauge {
Gauge::from_arc(get_or_insert(&self.metrics.gauges, key, Default::default))
}
fn register_histogram(&self, key: &Key, _metadata: &Metadata<'_>) -> Histogram {
Histogram::from_arc(get_or_insert(&self.metrics.histograms, key, || {
HistogramSamples(Mutex::new(RollingSummary::new(Instant::now())))
}))
}
}
#[derive(Debug)]
pub(super) struct HistogramSamples(Mutex<RollingSummary>);
impl HistogramSamples {
pub(super) fn with_summary<T>(&self, f: impl FnOnce(&RollingSummary) -> T) -> T {
f(&self.0.lock().unwrap_or_else(PoisonError::into_inner))
}
}
impl HistogramFn for HistogramSamples {
fn record(&self, value: f64) {
let now = Instant::now();
self.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.add(value, now);
}
}
fn get_or_insert<T>(
metrics: &RwLock<HashMap<Key, Arc<T>>>,
key: &Key,
new: impl FnOnce() -> T,
) -> Arc<T> {
if let Some(metric) = read(metrics).get(key) {
return metric.clone();
}
write(metrics)
.entry(key.clone())
.or_insert_with(|| Arc::new(new()))
.clone()
}
pub(super) fn read<T>(lock: &RwLock<T>) -> std::sync::RwLockReadGuard<'_, T> {
lock.read().unwrap_or_else(PoisonError::into_inner)
}
fn write<T>(lock: &RwLock<T>) -> std::sync::RwLockWriteGuard<'_, T> {
lock.write().unwrap_or_else(PoisonError::into_inner)
}