Skip to main content

obs_core/
metric.rs

1//! `MetricEmitter` — visitor implemented by metric sinks. Spec 12 § 3.6.
2//!
3//! Generated `EventSchema::project_metrics` calls one method per
4//! `MEASUREMENT` field on the event. The trait is `&mut self` so
5//! implementations can hold transient state (e.g. an attribute set
6//! being assembled across calls).
7
8/// Implemented by metric sinks (OTLP metrics, Prometheus exporters).
9/// Generated `EventSchema::project_metrics` calls one method per
10/// `MEASUREMENT` field. Spec 12 § 3.6.
11pub trait MetricEmitter {
12    /// Record a counter increment.
13    fn record_counter(&mut self, instrument: &'static str, value: u64, unit: Option<&'static str>);
14    /// Record a u64 gauge value.
15    fn record_gauge_u64(
16        &mut self,
17        instrument: &'static str,
18        value: u64,
19        unit: Option<&'static str>,
20    );
21    /// Record an f64 gauge value.
22    fn record_gauge_f64(
23        &mut self,
24        instrument: &'static str,
25        value: f64,
26        unit: Option<&'static str>,
27    );
28    /// Record a histogram observation against the supplied bounds.
29    fn record_histogram(
30        &mut self,
31        instrument: &'static str,
32        value: f64,
33        unit: Option<&'static str>,
34        bounds: &'static [f64],
35    );
36    /// Attribute set carried into every `record_*` on the same event.
37    fn with_attributes(&mut self, attrs: &[(&'static str, &str)]);
38}
39
40/// `MetricEmitter` that drops every record. Used as the default in
41/// non-metric sinks.
42#[derive(Debug, Default, Clone, Copy)]
43pub struct NoopMetricEmitter;
44
45impl MetricEmitter for NoopMetricEmitter {
46    fn record_counter(&mut self, _: &'static str, _: u64, _: Option<&'static str>) {}
47    fn record_gauge_u64(&mut self, _: &'static str, _: u64, _: Option<&'static str>) {}
48    fn record_gauge_f64(&mut self, _: &'static str, _: f64, _: Option<&'static str>) {}
49    fn record_histogram(
50        &mut self,
51        _: &'static str,
52        _: f64,
53        _: Option<&'static str>,
54        _: &'static [f64],
55    ) {
56    }
57    fn with_attributes(&mut self, _: &[(&'static str, &str)]) {}
58}