Skip to main content

llm_optimizer_processor/metrics/
labels.rs

1//! Label management for Prometheus metrics
2//!
3//! This module provides type-safe label management for metrics, ensuring
4//! proper validation and consistent naming.
5
6use std::fmt;
7
8/// Common label names used across metrics
9pub struct LabelNames;
10
11impl LabelNames {
12    pub const RESULT: &'static str = "result";
13    pub const OPERATION: &'static str = "operation";
14    pub const BACKEND: &'static str = "backend";
15    pub const LAYER: &'static str = "layer";
16    pub const STRATEGY: &'static str = "strategy";
17    pub const WINDOW_TYPE: &'static str = "window_type";
18    pub const AGGREGATE_TYPE: &'static str = "aggregate_type";
19    pub const PARTITION: &'static str = "partition";
20    pub const TOPIC: &'static str = "topic";
21    pub const ERROR_TYPE: &'static str = "error_type";
22}
23
24/// Label value for processing results
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26pub enum ResultLabel {
27    Success,
28    Error,
29    Dropped,
30    Filtered,
31}
32
33impl fmt::Display for ResultLabel {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            ResultLabel::Success => write!(f, "success"),
37            ResultLabel::Error => write!(f, "error"),
38            ResultLabel::Dropped => write!(f, "dropped"),
39            ResultLabel::Filtered => write!(f, "filtered"),
40        }
41    }
42}
43
44/// Label value for state operations
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
46pub enum OperationLabel {
47    Get,
48    Put,
49    Delete,
50    Scan,
51    BatchGet,
52    BatchPut,
53}
54
55impl fmt::Display for OperationLabel {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        match self {
58            OperationLabel::Get => write!(f, "get"),
59            OperationLabel::Put => write!(f, "put"),
60            OperationLabel::Delete => write!(f, "delete"),
61            OperationLabel::Scan => write!(f, "scan"),
62            OperationLabel::BatchGet => write!(f, "batch_get"),
63            OperationLabel::BatchPut => write!(f, "batch_put"),
64        }
65    }
66}
67
68/// Label value for state backends
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70pub enum BackendLabel {
71    InMemory,
72    Sled,
73    Redis,
74    Postgres,
75}
76
77impl fmt::Display for BackendLabel {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        match self {
80            BackendLabel::InMemory => write!(f, "in_memory"),
81            BackendLabel::Sled => write!(f, "sled"),
82            BackendLabel::Redis => write!(f, "redis"),
83            BackendLabel::Postgres => write!(f, "postgres"),
84        }
85    }
86}
87
88/// Label value for cache layers
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
90pub enum CacheLayerLabel {
91    L1,
92    L2,
93    L3,
94}
95
96impl fmt::Display for CacheLayerLabel {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        match self {
99            CacheLayerLabel::L1 => write!(f, "l1"),
100            CacheLayerLabel::L2 => write!(f, "l2"),
101            CacheLayerLabel::L3 => write!(f, "l3"),
102        }
103    }
104}
105
106/// Label value for fill strategies in normalization
107#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
108pub enum StrategyLabel {
109    Forward,
110    Backward,
111    Linear,
112    Zero,
113    Drop,
114}
115
116impl fmt::Display for StrategyLabel {
117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118        match self {
119            StrategyLabel::Forward => write!(f, "forward"),
120            StrategyLabel::Backward => write!(f, "backward"),
121            StrategyLabel::Linear => write!(f, "linear"),
122            StrategyLabel::Zero => write!(f, "zero"),
123            StrategyLabel::Drop => write!(f, "drop"),
124        }
125    }
126}
127
128/// Label value for window types
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
130pub enum WindowTypeLabel {
131    Tumbling,
132    Sliding,
133    Session,
134}
135
136impl fmt::Display for WindowTypeLabel {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        match self {
139            WindowTypeLabel::Tumbling => write!(f, "tumbling"),
140            WindowTypeLabel::Sliding => write!(f, "sliding"),
141            WindowTypeLabel::Session => write!(f, "session"),
142        }
143    }
144}
145
146/// Type-safe label value wrapper
147#[derive(Debug, Clone, PartialEq, Eq, Hash)]
148pub enum LabelValue {
149    Result(ResultLabel),
150    Operation(OperationLabel),
151    Backend(BackendLabel),
152    CacheLayer(CacheLayerLabel),
153    Strategy(StrategyLabel),
154    WindowType(WindowTypeLabel),
155    Custom(String),
156}
157
158impl fmt::Display for LabelValue {
159    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160        match self {
161            LabelValue::Result(v) => write!(f, "{}", v),
162            LabelValue::Operation(v) => write!(f, "{}", v),
163            LabelValue::Backend(v) => write!(f, "{}", v),
164            LabelValue::CacheLayer(v) => write!(f, "{}", v),
165            LabelValue::Strategy(v) => write!(f, "{}", v),
166            LabelValue::WindowType(v) => write!(f, "{}", v),
167            LabelValue::Custom(v) => write!(f, "{}", v),
168        }
169    }
170}
171
172impl From<ResultLabel> for LabelValue {
173    fn from(label: ResultLabel) -> Self {
174        LabelValue::Result(label)
175    }
176}
177
178impl From<OperationLabel> for LabelValue {
179    fn from(label: OperationLabel) -> Self {
180        LabelValue::Operation(label)
181    }
182}
183
184impl From<BackendLabel> for LabelValue {
185    fn from(label: BackendLabel) -> Self {
186        LabelValue::Backend(label)
187    }
188}
189
190impl From<CacheLayerLabel> for LabelValue {
191    fn from(label: CacheLayerLabel) -> Self {
192        LabelValue::CacheLayer(label)
193    }
194}
195
196impl From<StrategyLabel> for LabelValue {
197    fn from(label: StrategyLabel) -> Self {
198        LabelValue::Strategy(label)
199    }
200}
201
202impl From<WindowTypeLabel> for LabelValue {
203    fn from(label: WindowTypeLabel) -> Self {
204        LabelValue::WindowType(label)
205    }
206}
207
208impl From<String> for LabelValue {
209    fn from(s: String) -> Self {
210        LabelValue::Custom(s)
211    }
212}
213
214impl From<&str> for LabelValue {
215    fn from(s: &str) -> Self {
216        LabelValue::Custom(s.to_string())
217    }
218}
219
220/// A set of metric labels
221#[derive(Debug, Clone, Default)]
222pub struct LabelSet {
223    labels: Vec<(String, String)>,
224}
225
226impl LabelSet {
227    /// Create a new empty label set
228    pub fn new() -> Self {
229        Self {
230            labels: Vec::new(),
231        }
232    }
233
234    /// Add a label to the set
235    pub fn with(mut self, name: impl Into<String>, value: impl Into<LabelValue>) -> Self {
236        let value = value.into();
237        self.labels.push((name.into(), value.to_string()));
238        self
239    }
240
241    /// Get all labels as a slice
242    pub fn as_slice(&self) -> &[(String, String)] {
243        &self.labels
244    }
245}
246
247/// Trait for types that can provide metric labels
248pub trait MetricLabels {
249    fn labels(&self) -> LabelSet;
250}