Skip to main content

faucet_core/observability/
labels.rs

1//! Labels carried by every observability span and metric emitted by the
2//! pipeline-internal decorators.
3
4use std::sync::Arc;
5
6/// Common labels carried by every span and metric.
7///
8/// `pipeline` and `row` are Prometheus labels (bounded cardinality). `run_id`
9/// is a span attribute only — never a metric label — used for trace
10/// correlation. `connector` is captured per-decorator at construction time,
11/// not stored here.
12#[derive(Debug, Clone)]
13pub struct Labels {
14    pub pipeline: Arc<str>,
15    pub row: Arc<str>,
16    pub run_id: Arc<str>,
17}
18
19impl Labels {
20    pub fn new(
21        pipeline: impl Into<Arc<str>>,
22        row: impl Into<Arc<str>>,
23        run_id: impl Into<Arc<str>>,
24    ) -> Self {
25        Self {
26            pipeline: pipeline.into(),
27            row: row.into(),
28            run_id: run_id.into(),
29        }
30    }
31
32    /// Convenience constructor for non-CLI library callers: pipeline name only,
33    /// empty row, freshly-minted UUIDv7 run id.
34    pub fn for_named(pipeline: impl Into<Arc<str>>) -> Self {
35        let run_id: Arc<str> = uuid::Uuid::now_v7().to_string().into();
36        Self::new(pipeline, "", run_id)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn stores_all_fields() {
46        let l = Labels::new("p", "r", "rid");
47        assert_eq!(&*l.pipeline, "p");
48        assert_eq!(&*l.row, "r");
49        assert_eq!(&*l.run_id, "rid");
50    }
51
52    #[test]
53    fn for_named_mints_run_id() {
54        let l = Labels::for_named("p");
55        assert_eq!(&*l.pipeline, "p");
56        assert_eq!(&*l.row, "");
57        // UUIDv7 string is 36 chars (8-4-4-4-12 hex + 4 dashes).
58        assert_eq!(l.run_id.len(), 36);
59    }
60
61    #[test]
62    fn clone_is_arc_shallow() {
63        let l = Labels::new("p", "r", "rid");
64        let c = l.clone();
65        // Cloning Arc<str> bumps the refcount; the underlying allocation is shared.
66        assert!(Arc::ptr_eq(&l.pipeline, &c.pipeline));
67        assert!(Arc::ptr_eq(&l.row, &c.row));
68        assert!(Arc::ptr_eq(&l.run_id, &c.run_id));
69    }
70}