use std::time::Duration;
use vise::{
Buckets, Counter, EncodeLabelSet, EncodeLabelValue, Gauge, Histogram, Info, LabeledFamily,
Metrics, Unit,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue)]
#[metrics(rename_all = "snake_case")]
pub(super) enum Method {
Get,
Insert,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue)]
#[metrics(rename_all = "snake_case")]
pub(super) enum RequestOutcome {
Hit,
Miss,
}
impl RequestOutcome {
pub fn from_hit(hit: bool) -> Self {
if hit {
Self::Hit
} else {
Self::Miss
}
}
}
const SMALL_LATENCIES: Buckets = Buckets::values(&[
1e-8, 2.5e-8, 5e-8, 1e-7, 2.5e-7, 5e-7, 1e-6, 2.5e-6, 5e-6, 1e-5, 2.5e-5, 5e-5, 1e-4, 1e-3,
]);
#[derive(Debug, EncodeLabelSet)]
pub(super) struct LruCacheConfig {
#[metrics(unit = Unit::Bytes)]
pub capacity: u64,
}
#[derive(Debug, EncodeLabelSet)]
pub(super) struct SequentialCacheConfig {
pub capacity: u64,
}
#[derive(Debug, Metrics)]
#[metrics(prefix = "server_state_cache")]
pub(super) struct CacheMetrics {
#[metrics(labels = ["name"])]
pub lru_info: LabeledFamily<&'static str, Info<LruCacheConfig>>,
#[metrics(labels = ["name"])]
pub sequential_info: LabeledFamily<&'static str, Info<SequentialCacheConfig>>,
#[metrics(buckets = SMALL_LATENCIES, labels = ["name", "method"])]
pub latency: LabeledFamily<(&'static str, Method), Histogram<Duration>, 2>,
#[metrics(labels = ["name", "kind"])]
pub requests: LabeledFamily<(&'static str, RequestOutcome), Counter, 2>,
#[metrics(labels = ["name"])]
pub len: LabeledFamily<&'static str, Gauge<u64>>,
#[metrics(labels = ["name"])]
pub used_memory: LabeledFamily<&'static str, Gauge<u64>>,
}
#[vise::register]
pub(super) static METRICS: vise::Global<CacheMetrics> = vise::Global::new();