|
1 | 1 | use opentelemetry_proto::tonic::common::v1::any_value::Value as OtelValue;
|
2 | 2 |
|
3 | 3 | use crate::OtelLog;
|
| 4 | +use relay_common::time::UnixTimestamp; |
4 | 5 | use relay_event_schema::protocol::{AttributeValue, OurLog, SpanId, TraceId};
|
5 | 6 | use relay_protocol::{Annotated, Object};
|
6 | 7 |
|
@@ -29,6 +30,10 @@ pub fn otel_to_sentry_log(otel_log: OtelLog) -> OurLog {
|
29 | 30 |
|
30 | 31 | let mut attribute_data = Object::new();
|
31 | 32 |
|
| 33 | + // We ignore the passed observed time since Relay always acts as the collector in Sentry. |
| 34 | + // We may change this in the future with forwarding Relays. |
| 35 | + let observed_time_unix_nano = UnixTimestamp::now().as_nanos(); |
| 36 | + |
32 | 37 | for attribute in attributes.into_iter() {
|
33 | 38 | if let Some(value) = attribute.value.and_then(|v| v.value) {
|
34 | 39 | let key = attribute.key;
|
@@ -58,7 +63,7 @@ pub fn otel_to_sentry_log(otel_log: OtelLog) -> OurLog {
|
58 | 63 |
|
59 | 64 | OurLog {
|
60 | 65 | timestamp_nanos: Annotated::new(otel_log.time_unix_nano),
|
61 |
| - observed_timestamp_nanos: Annotated::new(otel_log.observed_time_unix_nano), |
| 66 | + observed_timestamp_nanos: Annotated::new(observed_time_unix_nano), |
62 | 67 | trace_id: TraceId(trace_id).into(),
|
63 | 68 | span_id: Annotated::new(SpanId(span_id)),
|
64 | 69 | trace_flags: Annotated::new(0),
|
@@ -201,4 +206,61 @@ mod tests {
|
201 | 206 | Some(&"SELECT \"table\".\"col\" FROM \"table\" WHERE \"table\".\"col\" = %s".into())
|
202 | 207 | );
|
203 | 208 | }
|
| 209 | + |
| 210 | + #[test] |
| 211 | + fn parse_log_without_observed_time() { |
| 212 | + let json_without_observed_time = r#"{ |
| 213 | + "timeUnixNano": "1544712660300000000", |
| 214 | + "observedTimeUnixNano": "0", |
| 215 | + "severityNumber": 10, |
| 216 | + "severityText": "Information", |
| 217 | + "traceId": "5B8EFFF798038103D269B633813FC60C", |
| 218 | + "spanId": "EEE19B7EC3C1B174", |
| 219 | + "body": { |
| 220 | + "stringValue": "Example log record" |
| 221 | + }, |
| 222 | + "attributes": [] |
| 223 | + }"#; |
| 224 | + |
| 225 | + let before_test = UnixTimestamp::now().as_nanos(); |
| 226 | + let otel_log: OtelLog = serde_json::from_str(json_without_observed_time).unwrap(); |
| 227 | + let our_log: OurLog = otel_to_sentry_log(otel_log); |
| 228 | + let after_test = UnixTimestamp::now().as_nanos(); |
| 229 | + |
| 230 | + let observed_time = our_log.observed_timestamp_nanos.value().unwrap(); |
| 231 | + assert!(*observed_time > 0); |
| 232 | + assert!(*observed_time >= before_test); |
| 233 | + assert!(*observed_time <= after_test); |
| 234 | + } |
| 235 | + |
| 236 | + #[test] |
| 237 | + fn parse_log_ignores_observed_time() { |
| 238 | + let json_with_observed_time = r#"{ |
| 239 | + "timeUnixNano": "1544712660300000000", |
| 240 | + "observedTimeUnixNano": "1544712660300000000", |
| 241 | + "severityNumber": 10, |
| 242 | + "severityText": "Information", |
| 243 | + "traceId": "5B8EFFF798038103D269B633813FC60C", |
| 244 | + "spanId": "EEE19B7EC3C1B174", |
| 245 | + "body": { |
| 246 | + "stringValue": "Example log record" |
| 247 | + }, |
| 248 | + "attributes": [] |
| 249 | + }"#; |
| 250 | + |
| 251 | + let before_test = UnixTimestamp::now().as_nanos(); |
| 252 | + let otel_log: OtelLog = serde_json::from_str(json_with_observed_time).unwrap(); |
| 253 | + let our_log: OurLog = otel_to_sentry_log(otel_log); |
| 254 | + let after_test = UnixTimestamp::now().as_nanos(); |
| 255 | + |
| 256 | + let observed_time = our_log.observed_timestamp_nanos.value().unwrap(); |
| 257 | + assert!(*observed_time > 0); |
| 258 | + assert!(*observed_time >= before_test); |
| 259 | + assert!(*observed_time <= after_test); |
| 260 | + |
| 261 | + assert_ne!( |
| 262 | + our_log.observed_timestamp_nanos, |
| 263 | + Annotated::new(1544712660300000000) |
| 264 | + ); |
| 265 | + } |
204 | 266 | }
|
0 commit comments