1use ahash::HashSet;
16
17#[derive(Debug, Clone, Default)]
20pub enum ProtocolCtx {
21 #[default]
22 None,
23 OtlpMetric(OtlpMetricCtx),
24}
25
26impl ProtocolCtx {
27 pub fn get_otlp_metric_ctx(&self) -> Option<&OtlpMetricCtx> {
28 match self {
29 ProtocolCtx::None => None,
30 ProtocolCtx::OtlpMetric(opt) => Some(opt),
31 }
32 }
33}
34
35#[derive(Debug, Clone, Default)]
51pub struct OtlpMetricCtx {
52 pub promote_all_resource_attrs: bool,
53 pub resource_attrs: HashSet<String>,
54 pub promote_scope_attrs: bool,
55 pub with_metric_engine: bool,
56 pub is_legacy: bool,
57 pub resource_info: bool,
60 pub metric_type: MetricType,
61 pub metric_translation_strategy: OtlpMetricTranslationStrategy,
62}
63
64impl OtlpMetricCtx {
65 pub fn set_metric_type(&mut self, metric_type: MetricType) {
66 self.metric_type = metric_type;
67 }
68}
69
70#[derive(Debug, Clone, Default)]
71pub enum MetricType {
72 #[default]
74 Init,
75 NonMonotonicSum,
76 MonotonicSum,
77 Gauge,
78 Histogram,
79 ExponentialHistogram,
80 Summary,
81}
82
83#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
84pub enum OtlpMetricTranslationStrategy {
85 #[default]
86 UnderscoreEscapingWithSuffixes,
87 UnderscoreEscapingWithoutSuffixes,
88 NoUtf8EscapingWithSuffixes,
89 NoTranslation,
90}
91
92impl OtlpMetricTranslationStrategy {
93 pub const VALUES: [&'static str; 4] = [
94 "UnderscoreEscapingWithSuffixes",
95 "UnderscoreEscapingWithoutSuffixes",
96 "NoUTF8EscapingWithSuffixes",
97 "NoTranslation",
98 ];
99
100 pub fn as_str(self) -> &'static str {
101 match self {
102 Self::UnderscoreEscapingWithSuffixes => "UnderscoreEscapingWithSuffixes",
103 Self::UnderscoreEscapingWithoutSuffixes => "UnderscoreEscapingWithoutSuffixes",
104 Self::NoUtf8EscapingWithSuffixes => "NoUTF8EscapingWithSuffixes",
105 Self::NoTranslation => "NoTranslation",
106 }
107 }
108
109 pub fn should_escape(self) -> bool {
110 matches!(
111 self,
112 Self::UnderscoreEscapingWithSuffixes | Self::UnderscoreEscapingWithoutSuffixes
113 )
114 }
115
116 pub fn should_add_suffixes(self) -> bool {
117 matches!(
118 self,
119 Self::UnderscoreEscapingWithSuffixes | Self::NoUtf8EscapingWithSuffixes
120 )
121 }
122}
123
124impl std::str::FromStr for OtlpMetricTranslationStrategy {
125 type Err = ();
126
127 fn from_str(value: &str) -> Result<Self, Self::Err> {
128 match value {
129 "UnderscoreEscapingWithSuffixes" => Ok(Self::UnderscoreEscapingWithSuffixes),
130 "UnderscoreEscapingWithoutSuffixes" => Ok(Self::UnderscoreEscapingWithoutSuffixes),
131 "NoUTF8EscapingWithSuffixes" => Ok(Self::NoUtf8EscapingWithSuffixes),
132 "NoTranslation" => Ok(Self::NoTranslation),
133 _ => Err(()),
134 }
135 }
136}
137
138impl std::fmt::Display for OtlpMetricTranslationStrategy {
139 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140 f.write_str(self.as_str())
141 }
142}