1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Label trait for enum-indexed metrics.
//!
//! Labels provide O(1) dimensional metrics without HashMap lookups.
//! Each label enum variant maps directly to an array index.
use Debug;
/// Trait for label enums used with `LabeledCounter`, `LabeledGauge`, etc.
///
/// Implementing this trait allows an enum to be used as a dimension for metrics.
/// Each variant maps to an array index, enabling O(1) metric lookup and update.
///
/// # Example
///
/// ```ignore
/// #[derive(Copy, Clone, Debug)]
/// enum HttpMethod {
/// Get,
/// Post,
/// Put,
/// Delete,
/// Other,
/// }
///
/// impl LabelEnum for HttpMethod {
/// const CARDINALITY: usize = 5;
/// const LABEL_NAME: &'static str = "method";
///
/// fn as_index(self) -> usize {
/// self as usize
/// }
///
/// fn from_index(index: usize) -> Self {
/// match index {
/// 0 => Self::Get,
/// 1 => Self::Post,
/// 2 => Self::Put,
/// 3 => Self::Delete,
/// _ => Self::Other,
/// }
/// }
///
/// fn variant_name(self) -> &'static str {
/// match self {
/// Self::Get => "get",
/// Self::Post => "post",
/// Self::Put => "put",
/// Self::Delete => "delete",
/// Self::Other => "other",
/// }
/// }
/// }
/// ```