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 metric_type: MetricType,
58 pub metric_translation_strategy: OtlpMetricTranslationStrategy,
59}
60
61impl OtlpMetricCtx {
62 pub fn set_metric_type(&mut self, metric_type: MetricType) {
63 self.metric_type = metric_type;
64 }
65}
66
67#[derive(Debug, Clone, Default)]
68pub enum MetricType {
69 #[default]
71 Init,
72 NonMonotonicSum,
73 MonotonicSum,
74 Gauge,
75 Histogram,
76 ExponentialHistogram,
77 Summary,
78}
79
80#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
81pub enum OtlpMetricTranslationStrategy {
82 #[default]
83 UnderscoreEscapingWithSuffixes,
84 UnderscoreEscapingWithoutSuffixes,
85 NoUtf8EscapingWithSuffixes,
86 NoTranslation,
87}
88
89impl OtlpMetricTranslationStrategy {
90 pub const VALUES: [&'static str; 4] = [
91 "UnderscoreEscapingWithSuffixes",
92 "UnderscoreEscapingWithoutSuffixes",
93 "NoUTF8EscapingWithSuffixes",
94 "NoTranslation",
95 ];
96
97 pub fn as_str(self) -> &'static str {
98 match self {
99 Self::UnderscoreEscapingWithSuffixes => "UnderscoreEscapingWithSuffixes",
100 Self::UnderscoreEscapingWithoutSuffixes => "UnderscoreEscapingWithoutSuffixes",
101 Self::NoUtf8EscapingWithSuffixes => "NoUTF8EscapingWithSuffixes",
102 Self::NoTranslation => "NoTranslation",
103 }
104 }
105
106 pub fn should_escape(self) -> bool {
107 matches!(
108 self,
109 Self::UnderscoreEscapingWithSuffixes | Self::UnderscoreEscapingWithoutSuffixes
110 )
111 }
112
113 pub fn should_add_suffixes(self) -> bool {
114 matches!(
115 self,
116 Self::UnderscoreEscapingWithSuffixes | Self::NoUtf8EscapingWithSuffixes
117 )
118 }
119}
120
121impl std::str::FromStr for OtlpMetricTranslationStrategy {
122 type Err = ();
123
124 fn from_str(value: &str) -> Result<Self, Self::Err> {
125 match value {
126 "UnderscoreEscapingWithSuffixes" => Ok(Self::UnderscoreEscapingWithSuffixes),
127 "UnderscoreEscapingWithoutSuffixes" => Ok(Self::UnderscoreEscapingWithoutSuffixes),
128 "NoUTF8EscapingWithSuffixes" => Ok(Self::NoUtf8EscapingWithSuffixes),
129 "NoTranslation" => Ok(Self::NoTranslation),
130 _ => Err(()),
131 }
132 }
133}
134
135impl std::fmt::Display for OtlpMetricTranslationStrategy {
136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137 f.write_str(self.as_str())
138 }
139}