Skip to main content

feldera_adapterlib/
metrics.rs

1use feldera_storage::histogram::ExponentialHistogramSnapshot;
2
3/// The type of a connector metric.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
5pub enum ValueType {
6    /// A monotonically increasing counter.
7    Counter,
8    /// An instantaneous gauge.
9    Gauge,
10}
11
12impl ValueType {
13    pub fn as_str(&self) -> &'static str {
14        match self {
15            ValueType::Counter => "counter",
16            ValueType::Gauge => "gauge",
17        }
18    }
19}
20
21/// A connector histogram metric.
22///
23/// The histogram is exported in the unit it was recorded in, so `name` should
24/// name that unit (for example, a histogram of microseconds should be named
25/// `..._microseconds`).
26pub struct ConnectorHistogram {
27    /// Metric name; must be a valid Prometheus metric name component (no spaces,
28    /// no special characters other than `_`).
29    pub name: &'static str,
30
31    /// Human-readable help text.
32    pub help: &'static str,
33
34    /// The recorded histogram.
35    pub snapshot: ExponentialHistogramSnapshot,
36}
37
38/// Connector-specific metrics exported via the Prometheus `/metrics` endpoint.
39///
40/// Connectors that have metrics beyond the standard `InputEndpointMetrics`
41/// should implement this trait and register themselves via
42/// `InputConsumer::set_custom_metrics` during `TransportInputEndpoint::open`.
43pub trait ConnectorMetrics: Send + Sync {
44    /// Return a list of `(name, help, value_type, value)` tuples.
45    ///
46    /// The returned `name`s must be valid Prometheus metric name components
47    /// (no spaces, no special characters other than `_`).
48    fn metrics(&self) -> Vec<(&'static str, &'static str, ValueType, f64)>;
49
50    /// Return the connector's histogram metrics.
51    ///
52    /// Defaults to an empty list, so connectors that export only scalar metrics
53    /// need not implement it.
54    fn histograms(&self) -> Vec<ConnectorHistogram> {
55        Vec::new()
56    }
57}