saluki_components/sources/otlp/metrics/
config.rs1use std::time::Duration;
2
3use saluki_core::data_model::origin::OriginTagCardinality;
4use saluki_error::{generic_error, GenericError};
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum HistogramMode {
9 NoBuckets,
11
12 Counters,
14
15 Distributions,
17}
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
21pub enum CumulativeMonotonicMode {
22 ToDelta,
24
25 RawValue,
27}
28
29#[derive(Clone, Copy, Debug, Eq, PartialEq)]
31pub enum InitialCumulativeMonotonicValue {
32 Auto,
35
36 Drop,
38
39 Keep,
41}
42
43#[derive(Clone, Copy, Debug, Eq, PartialEq)]
45pub enum SummaryMode {
46 Gauges,
48
49 NoQuantiles,
51}
52
53#[derive(Clone, Debug, PartialEq)]
55pub struct OtlpMetricsSettings {
56 pub tag_cardinality: OriginTagCardinality,
58
59 pub histogram_mode: HistogramMode,
61
62 pub send_histogram_aggregations: bool,
64
65 pub resource_attributes_as_tags: bool,
68
69 pub instrumentation_scope_metadata_as_tags: bool,
71
72 pub cumulative_monotonic_mode: CumulativeMonotonicMode,
74
75 pub initial_cumulative_monotonic_value: InitialCumulativeMonotonicValue,
77
78 pub tags: String,
80
81 pub summary_mode: SummaryMode,
83
84 pub delta_ttl: Duration,
86}
87
88#[derive(Debug, Clone, Copy)]
89#[allow(dead_code)]
90pub struct OtlpMetricsTranslatorConfig {
91 pub tag_cardinality: OriginTagCardinality,
93 pub hist_mode: HistogramMode,
94 pub send_histogram_aggregations: bool,
95 pub cumulative_monotonic_mode: CumulativeMonotonicMode,
96 pub initial_cumulative_monotonic_value: InitialCumulativeMonotonicValue,
97 pub instrumentation_scope_metadata_as_tags: bool,
98 pub instrumentation_library_metadata_as_tags: bool,
99 pub resource_attributes_as_tags: bool,
102 pub with_remapping: bool,
106 pub with_otel_prefix: bool,
110 pub quantiles: bool,
111 pub delta_ttl: Duration,
113 pub infer_delta_interval: bool,
114}
115
116#[allow(dead_code)]
117impl OtlpMetricsTranslatorConfig {
118 pub fn from_settings(settings: &OtlpMetricsSettings) -> Self {
120 Self {
121 tag_cardinality: settings.tag_cardinality,
122 hist_mode: settings.histogram_mode,
123 send_histogram_aggregations: settings.send_histogram_aggregations,
124 cumulative_monotonic_mode: settings.cumulative_monotonic_mode,
125 initial_cumulative_monotonic_value: settings.initial_cumulative_monotonic_value,
126 instrumentation_scope_metadata_as_tags: settings.instrumentation_scope_metadata_as_tags,
127 instrumentation_library_metadata_as_tags: false,
128 resource_attributes_as_tags: settings.resource_attributes_as_tags,
129 with_remapping: false,
130 with_otel_prefix: false,
131 quantiles: matches!(settings.summary_mode, SummaryMode::Gauges),
132 delta_ttl: settings.delta_ttl,
133 infer_delta_interval: false,
134 }
135 }
136
137 #[cfg(test)]
139 pub fn for_tests() -> Self {
140 Self {
141 tag_cardinality: OriginTagCardinality::Low,
142 hist_mode: HistogramMode::Distributions,
143 send_histogram_aggregations: false,
144 cumulative_monotonic_mode: CumulativeMonotonicMode::ToDelta,
145 initial_cumulative_monotonic_value: InitialCumulativeMonotonicValue::Auto,
146 instrumentation_scope_metadata_as_tags: true,
147 instrumentation_library_metadata_as_tags: false,
148 resource_attributes_as_tags: false,
149 with_remapping: false,
150 with_otel_prefix: false,
151 quantiles: false,
152 infer_delta_interval: false,
153 delta_ttl: Duration::from_secs(3600),
154 }
155 }
156
157 pub fn validate(&self) -> Result<(), GenericError> {
158 if self.hist_mode == HistogramMode::NoBuckets && !self.send_histogram_aggregations {
159 return Err(generic_error!("no buckets mode and no send count sum are incompatible"));
160 }
161 Ok(())
162 }
163
164 pub fn with_remapping(mut self, with_remapping: bool) -> Self {
165 self.with_remapping = with_remapping;
166 if with_remapping {
167 self.with_otel_prefix = true;
168 }
169 self
170 }
171
172 pub fn with_otel_prefix(mut self, with_otel_prefix: bool) -> Self {
173 self.with_otel_prefix = with_otel_prefix;
174 self
175 }
176
177 pub fn with_histogram_mode(mut self, hist_mode: HistogramMode) -> Self {
178 self.hist_mode = hist_mode;
179 self
180 }
181
182 pub fn with_send_histogram_aggregations(mut self, send_histogram_aggregations: bool) -> Self {
183 self.send_histogram_aggregations = send_histogram_aggregations;
184 self
185 }
186
187 pub fn with_infer_delta_interval(mut self, infer_delta_interval: bool) -> Self {
188 self.infer_delta_interval = infer_delta_interval;
189 self
190 }
191
192 pub fn with_instrumentation_library_metadata_as_tags(
193 mut self, instrumentation_library_metadata_as_tags: bool,
194 ) -> Self {
195 self.instrumentation_library_metadata_as_tags = instrumentation_library_metadata_as_tags;
196 self
197 }
198}
199
200#[cfg(test)]
201mod tests {
202 use super::*;
203
204 #[test]
209 fn validate_rejects_no_buckets_mode_without_histogram_aggregations() {
210 let config = OtlpMetricsTranslatorConfig::for_tests()
211 .with_histogram_mode(HistogramMode::NoBuckets)
212 .with_send_histogram_aggregations(false);
213
214 let err = config
215 .validate()
216 .expect_err("no-buckets histogram mode without aggregations must be rejected");
217 assert_eq!(
218 err.to_string(),
219 "no buckets mode and no send count sum are incompatible"
220 );
221 }
222
223 #[test]
224 fn validate_allows_no_buckets_mode_with_histogram_aggregations() {
225 let config = OtlpMetricsTranslatorConfig::for_tests()
228 .with_histogram_mode(HistogramMode::NoBuckets)
229 .with_send_histogram_aggregations(true);
230
231 assert!(config.validate().is_ok());
232 }
233
234 #[test]
235 fn validate_allows_bucketed_modes_regardless_of_aggregations() {
236 for hist_mode in [HistogramMode::Counters, HistogramMode::Distributions] {
239 let config = OtlpMetricsTranslatorConfig::for_tests()
240 .with_histogram_mode(hist_mode)
241 .with_send_histogram_aggregations(false);
242
243 assert!(config.validate().is_ok(), "expected {hist_mode:?} to validate");
244 }
245 }
246}