Skip to content

Commit b74ea08

Browse files
authored
Fix timestamp in transaction created by OTel (#2627)
1 parent b873c38 commit b74ea08

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from datetime import datetime
2-
31
from opentelemetry.context import get_value # type: ignore
42
from opentelemetry.sdk.trace import SpanProcessor # type: ignore
53
from opentelemetry.semconv.trace import SpanAttributes # type: ignore
@@ -15,6 +13,7 @@
1513
INVALID_SPAN_ID,
1614
INVALID_TRACE_ID,
1715
)
16+
from sentry_sdk._compat import utc_from_timestamp
1817
from sentry_sdk.consts import INSTRUMENTER
1918
from sentry_sdk.hub import Hub
2019
from sentry_sdk.integrations.opentelemetry.consts import (
@@ -126,7 +125,7 @@ def on_start(self, otel_span, parent_context=None):
126125
sentry_span = sentry_parent_span.start_child(
127126
span_id=trace_data["span_id"],
128127
description=otel_span.name,
129-
start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9),
128+
start_timestamp=utc_from_timestamp(otel_span.start_time / 1e9),
130129
instrumenter=INSTRUMENTER.OTEL,
131130
)
132131
else:
@@ -136,7 +135,7 @@ def on_start(self, otel_span, parent_context=None):
136135
parent_span_id=parent_span_id,
137136
trace_id=trace_data["trace_id"],
138137
baggage=trace_data["baggage"],
139-
start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9),
138+
start_timestamp=utc_from_timestamp(otel_span.start_time / 1e9),
140139
instrumenter=INSTRUMENTER.OTEL,
141140
)
142141

@@ -174,9 +173,7 @@ def on_end(self, otel_span):
174173
else:
175174
self._update_span_with_otel_data(sentry_span, otel_span)
176175

177-
sentry_span.finish(
178-
end_timestamp=datetime.fromtimestamp(otel_span.end_time / 1e9)
179-
)
176+
sentry_span.finish(end_timestamp=utc_from_timestamp(otel_span.end_time / 1e9))
180177

181178
def _is_sentry_span(self, hub, otel_span):
182179
# type: (Hub, OTelSpan) -> bool

tests/integrations/opentelemetry/test_span_processor.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from datetime import timezone
23
import time
34
import pytest
45

@@ -331,7 +332,9 @@ def test_on_start_transaction():
331332
parent_span_id="abcdef1234567890",
332333
trace_id="1234567890abcdef1234567890abcdef",
333334
baggage=None,
334-
start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9),
335+
start_timestamp=datetime.fromtimestamp(
336+
otel_span.start_time / 1e9, timezone.utc
337+
),
335338
instrumenter="otel",
336339
)
337340

@@ -376,7 +379,9 @@ def test_on_start_child():
376379
fake_span.start_child.assert_called_once_with(
377380
span_id="1234567890abcdef",
378381
description="Sample OTel Span",
379-
start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9),
382+
start_timestamp=datetime.fromtimestamp(
383+
otel_span.start_time / 1e9, timezone.utc
384+
),
380385
instrumenter="otel",
381386
)
382387

0 commit comments

Comments
 (0)