Skip to content

Commit 3f98848

Browse files
transform: epoch nanosecond number to timestamp
1 parent 61986dc commit 3f98848

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

src/otel/logs.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use serde_json::Value;
2424
use std::collections::BTreeMap;
2525

2626
use super::otel_utils::collect_json_from_values;
27+
use super::otel_utils::convert_epoch_nano_to_timestamp;
2728
use super::otel_utils::insert_attributes;
2829

2930
/// otel log event has severity number
@@ -51,11 +52,15 @@ pub fn flatten_log_record(log_record: &LogRecord) -> BTreeMap<String, Value> {
5152
let mut log_record_json: BTreeMap<String, Value> = BTreeMap::new();
5253
log_record_json.insert(
5354
"time_unix_nano".to_string(),
54-
Value::Number(log_record.time_unix_nano.into()),
55+
Value::String(convert_epoch_nano_to_timestamp(
56+
log_record.time_unix_nano as i64,
57+
)),
5558
);
5659
log_record_json.insert(
57-
"observable_time_unix_nano".to_string(),
58-
Value::Number(log_record.observed_time_unix_nano.into()),
60+
"observed_time_unix_nano".to_string(),
61+
Value::String(convert_epoch_nano_to_timestamp(
62+
log_record.observed_time_unix_nano as i64,
63+
)),
5964
);
6065

6166
log_record_json.extend(flatten_severity(log_record.severity_number));

src/otel/metrics.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use opentelemetry_proto::tonic::metrics::v1::{
2525
};
2626
use serde_json::Value;
2727

28-
use super::otel_utils::{insert_attributes, insert_number_if_some};
28+
use super::otel_utils::{
29+
convert_epoch_nano_to_timestamp, insert_attributes, insert_number_if_some,
30+
};
2931

3032
/// otel metrics event has json array for exemplar
3133
/// this function flatten the exemplar json array
@@ -37,7 +39,9 @@ fn flatten_exemplar(exemplars: &[Exemplar]) -> BTreeMap<String, Value> {
3739
insert_attributes(&mut exemplar_json, &exemplar.filtered_attributes);
3840
exemplar_json.insert(
3941
"exemplar_time_unix_nano".to_string(),
40-
Value::Number(exemplar.time_unix_nano.into()),
42+
Value::String(convert_epoch_nano_to_timestamp(
43+
exemplar.time_unix_nano as i64,
44+
)),
4145
);
4246
exemplar_json.insert(
4347
"exemplar_span_id".to_string(),
@@ -79,11 +83,15 @@ fn flatten_number_data_points(data_points: &[NumberDataPoint]) -> Vec<BTreeMap<S
7983
insert_attributes(&mut data_point_json, &data_point.attributes);
8084
data_point_json.insert(
8185
"start_time_unix_nano".to_string(),
82-
Value::Number(data_point.start_time_unix_nano.into()),
86+
Value::String(convert_epoch_nano_to_timestamp(
87+
data_point.start_time_unix_nano as i64,
88+
)),
8389
);
8490
data_point_json.insert(
8591
"time_unix_nano".to_string(),
86-
Value::Number(data_point.time_unix_nano.into()),
92+
Value::String(convert_epoch_nano_to_timestamp(
93+
data_point.time_unix_nano as i64,
94+
)),
8795
);
8896
let exemplar_json = flatten_exemplar(&data_point.exemplars);
8997
for (key, value) in exemplar_json {
@@ -167,11 +175,15 @@ fn flatten_histogram(histogram: &Histogram) -> Vec<BTreeMap<String, Value>> {
167175
insert_attributes(&mut data_point_json, &data_point.attributes);
168176
data_point_json.insert(
169177
"histogram_start_time_unix_nano".to_string(),
170-
Value::Number(data_point.start_time_unix_nano.into()),
178+
Value::String(convert_epoch_nano_to_timestamp(
179+
data_point.start_time_unix_nano as i64,
180+
)),
171181
);
172182
data_point_json.insert(
173183
"histogram_time_unix_nano".to_string(),
174-
Value::Number(data_point.time_unix_nano.into()),
184+
Value::String(convert_epoch_nano_to_timestamp(
185+
data_point.time_unix_nano as i64,
186+
)),
175187
);
176188
data_point_json.insert(
177189
"histogram_data_point_count".to_string(),
@@ -242,11 +254,15 @@ fn flatten_exp_histogram(exp_histogram: &ExponentialHistogram) -> Vec<BTreeMap<S
242254
insert_attributes(&mut data_point_json, &data_point.attributes);
243255
data_point_json.insert(
244256
"exponential_histogram_start_time_unix_nano".to_string(),
245-
Value::Number(data_point.start_time_unix_nano.into()),
257+
Value::String(convert_epoch_nano_to_timestamp(
258+
data_point.start_time_unix_nano as i64,
259+
)),
246260
);
247261
data_point_json.insert(
248262
"exponential_histogram_time_unix_nano".to_string(),
249-
Value::Number(data_point.time_unix_nano.into()),
263+
Value::String(convert_epoch_nano_to_timestamp(
264+
data_point.time_unix_nano as i64,
265+
)),
250266
);
251267
data_point_json.insert(
252268
"exponential_histogram_data_point_count".to_string(),
@@ -306,11 +322,15 @@ fn flatten_summary(summary: &Summary) -> Vec<BTreeMap<String, Value>> {
306322
insert_attributes(&mut data_point_json, &data_point.attributes);
307323
data_point_json.insert(
308324
"summary_start_time_unix_nano".to_string(),
309-
Value::Number(data_point.start_time_unix_nano.into()),
325+
Value::String(convert_epoch_nano_to_timestamp(
326+
data_point.start_time_unix_nano as i64,
327+
)),
310328
);
311329
data_point_json.insert(
312330
"summary_time_unix_nano".to_string(),
313-
Value::Number(data_point.time_unix_nano.into()),
331+
Value::String(convert_epoch_nano_to_timestamp(
332+
data_point.time_unix_nano as i64,
333+
)),
314334
);
315335
data_point_json.insert(
316336
"summary_data_point_count".to_string(),

src/otel/otel_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
*/
1818

19+
use chrono::DateTime;
1920
use opentelemetry_proto::tonic::common::v1::{any_value::Value as OtelValue, AnyValue, KeyValue};
2021
use serde_json::Value;
2122
use std::collections::BTreeMap;
@@ -154,3 +155,8 @@ pub fn insert_attributes(map: &mut BTreeMap<String, Value>, attributes: &Vec<Key
154155
map.insert(key, value);
155156
}
156157
}
158+
159+
pub fn convert_epoch_nano_to_timestamp(epoch_ns: i64) -> String {
160+
let dt = DateTime::from_timestamp_nanos(epoch_ns).naive_utc();
161+
dt.format("%Y-%m-%dT%H:%M:%S%.6fZ").to_string()
162+
}

src/otel/traces.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use opentelemetry_proto::tonic::trace::v1::TracesData;
2525
use serde_json::Value;
2626
use std::collections::BTreeMap;
2727

28+
use super::otel_utils::convert_epoch_nano_to_timestamp;
2829
use super::otel_utils::insert_attributes;
2930

3031
/// this function flattens the `ScopeSpans` object
@@ -116,7 +117,9 @@ fn flatten_events(events: &[Event]) -> Vec<BTreeMap<String, Value>> {
116117
let mut event_json = BTreeMap::new();
117118
event_json.insert(
118119
"event_time_unix_nano".to_string(),
119-
Value::Number(event.time_unix_nano.into()),
120+
Value::String(
121+
convert_epoch_nano_to_timestamp(event.time_unix_nano as i64).to_string(),
122+
),
120123
);
121124
event_json.insert("event_name".to_string(), Value::String(event.name.clone()));
122125
insert_attributes(&mut event_json, &event.attributes);
@@ -261,11 +264,15 @@ fn flatten_span_record(span_record: &Span) -> Vec<BTreeMap<String, Value>> {
261264
span_record_json.extend(flatten_kind(span_record.kind));
262265
span_record_json.insert(
263266
"span_start_time_unix_nano".to_string(),
264-
Value::Number(span_record.start_time_unix_nano.into()),
267+
Value::String(convert_epoch_nano_to_timestamp(
268+
span_record.start_time_unix_nano as i64,
269+
)),
265270
);
266271
span_record_json.insert(
267272
"span_end_time_unix_nano".to_string(),
268-
Value::Number(span_record.end_time_unix_nano.into()),
273+
Value::String(convert_epoch_nano_to_timestamp(
274+
span_record.end_time_unix_nano as i64,
275+
)),
269276
);
270277
insert_attributes(&mut span_record_json, &span_record.attributes);
271278
span_record_json.insert(

0 commit comments

Comments
 (0)