|
16 | 16 | * |
17 | 17 | */ |
18 | 18 | use opentelemetry_proto::tonic::collector::metrics::v1::ExportMetricsServiceRequest; |
| 19 | +use opentelemetry_proto::tonic::common::v1::KeyValue; |
19 | 20 | use opentelemetry_proto::tonic::metrics::v1::number_data_point::Value as NumberDataPointValue; |
20 | 21 | use opentelemetry_proto::tonic::metrics::v1::{ |
21 | 22 | Exemplar, ExponentialHistogram, Gauge, Histogram, Metric, MetricsData, NumberDataPoint, Sum, |
22 | 23 | Summary, exemplar::Value as ExemplarValue, exponential_histogram_data_point::Buckets, metric, |
23 | 24 | }; |
24 | 25 | use serde_json::{Map, Value}; |
25 | 26 |
|
| 27 | +use crate::otel::otel_utils::flatten_attributes; |
| 28 | + |
26 | 29 | use super::otel_utils::{ |
27 | 30 | convert_epoch_nano_to_timestamp, insert_attributes, insert_number_if_some, |
28 | 31 | }; |
@@ -73,7 +76,7 @@ fn flatten_exemplar(exemplars: &[Exemplar]) -> Vec<Map<String, Value>> { |
73 | 76 | .iter() |
74 | 77 | .map(|exemplar| { |
75 | 78 | let mut exemplar_json = Map::new(); |
76 | | - insert_attributes(&mut exemplar_json, &exemplar.filtered_attributes); |
| 79 | + insert_exemplar_attributes(&mut exemplar_json, &exemplar.filtered_attributes); |
77 | 80 | exemplar_json.insert( |
78 | 81 | "exemplar_time_unix_nano".to_string(), |
79 | 82 | Value::String(convert_epoch_nano_to_timestamp( |
@@ -120,7 +123,7 @@ fn flatten_number_data_points(data_points: &[NumberDataPoint]) -> Vec<Map<String |
120 | 123 | .iter() |
121 | 124 | .map(|data_point| { |
122 | 125 | let mut data_point_json = Map::new(); |
123 | | - insert_attributes(&mut data_point_json, &data_point.attributes); |
| 126 | + insert_metric_attributes(&mut data_point_json, &data_point.attributes); |
124 | 127 | data_point_json.insert( |
125 | 128 | "start_time_unix_nano".to_string(), |
126 | 129 | Value::String(convert_epoch_nano_to_timestamp( |
@@ -218,7 +221,7 @@ fn flatten_histogram(histogram: &Histogram) -> Vec<Map<String, Value>> { |
218 | 221 | let mut data_points_json = Vec::new(); |
219 | 222 | for data_point in &histogram.data_points { |
220 | 223 | let mut data_point_json = Map::new(); |
221 | | - insert_attributes(&mut data_point_json, &data_point.attributes); |
| 224 | + insert_metric_attributes(&mut data_point_json, &data_point.attributes); |
222 | 225 | data_point_json.insert( |
223 | 226 | "start_time_unix_nano".to_string(), |
224 | 227 | Value::String(convert_epoch_nano_to_timestamp( |
@@ -315,7 +318,7 @@ fn flatten_exp_histogram(exp_histogram: &ExponentialHistogram) -> Vec<Map<String |
315 | 318 | let mut data_points_json = Vec::new(); |
316 | 319 | for data_point in &exp_histogram.data_points { |
317 | 320 | let mut data_point_json = Map::new(); |
318 | | - insert_attributes(&mut data_point_json, &data_point.attributes); |
| 321 | + insert_metric_attributes(&mut data_point_json, &data_point.attributes); |
319 | 322 | data_point_json.insert( |
320 | 323 | "start_time_unix_nano".to_string(), |
321 | 324 | Value::String(convert_epoch_nano_to_timestamp( |
@@ -384,7 +387,7 @@ fn flatten_summary(summary: &Summary) -> Vec<Map<String, Value>> { |
384 | 387 | let mut data_points_json = Vec::new(); |
385 | 388 | for data_point in &summary.data_points { |
386 | 389 | let mut data_point_json = Map::new(); |
387 | | - insert_attributes(&mut data_point_json, &data_point.attributes); |
| 390 | + insert_metric_attributes(&mut data_point_json, &data_point.attributes); |
388 | 391 | data_point_json.insert( |
389 | 392 | "start_time_unix_nano".to_string(), |
390 | 393 | Value::String(convert_epoch_nano_to_timestamp( |
@@ -647,3 +650,17 @@ fn flatten_data_point_flags(flags: u32) -> Map<String, Value> { |
647 | 650 | ); |
648 | 651 | data_point_flags_json |
649 | 652 | } |
| 653 | + |
| 654 | +fn insert_metric_attributes(map: &mut Map<String, Value>, attributes: &[KeyValue]) { |
| 655 | + let attributes_json = flatten_attributes(attributes); |
| 656 | + for (key, value) in attributes_json { |
| 657 | + map.insert(format!("metric_{}", key), value); |
| 658 | + } |
| 659 | +} |
| 660 | + |
| 661 | +fn insert_exemplar_attributes(map: &mut Map<String, Value>, attributes: &[KeyValue]) { |
| 662 | + let attributes_json = flatten_attributes(attributes); |
| 663 | + for (key, value) in attributes_json { |
| 664 | + map.insert(format!("exemplar_{}", key), value); |
| 665 | + } |
| 666 | +} |
0 commit comments