Skip to main content

folk_api/
metrics.rs

1//! Metrics registration. Plugins create metric families and update counters,
2//! gauges, and histograms.
3
4use std::sync::Arc;
5
6/// Single counter handle (one specific label combination).
7pub trait Counter: Send + Sync + 'static {
8    fn inc(&self);
9    fn inc_by(&self, v: u64);
10    fn get(&self) -> u64;
11}
12
13/// Single gauge handle (one specific label combination).
14pub trait Gauge: Send + Sync + 'static {
15    fn set(&self, v: i64);
16    fn inc(&self);
17    fn dec(&self);
18    fn get(&self) -> i64;
19}
20
21/// Single histogram handle (one specific label combination).
22pub trait Histogram: Send + Sync + 'static {
23    fn observe(&self, v: f64);
24}
25
26/// A counter metric family — use `with_labels` to get a concrete handle.
27pub trait CounterVec: Send + Sync + 'static {
28    fn with_labels(&self, labels: &[&str]) -> Arc<dyn Counter>;
29}
30
31pub trait GaugeVec: Send + Sync + 'static {
32    fn with_labels(&self, labels: &[&str]) -> Arc<dyn Gauge>;
33}
34
35pub trait HistogramVec: Send + Sync + 'static {
36    fn with_labels(&self, labels: &[&str]) -> Arc<dyn Histogram>;
37}
38
39/// Plugins register metric families here. Observers render them.
40pub trait MetricsRegistry: Send + Sync + 'static {
41    fn counter_vec(&self, name: &str, help: &str, label_keys: &[&str]) -> Arc<dyn CounterVec>;
42    fn gauge_vec(&self, name: &str, help: &str, label_keys: &[&str]) -> Arc<dyn GaugeVec>;
43    fn histogram_vec(&self, name: &str, help: &str, label_keys: &[&str]) -> Arc<dyn HistogramVec>;
44
45    /// Render all registered metrics in Prometheus text exposition format.
46    fn render(&self) -> String;
47}