diff --git a/sentry_sdk/integrations/opentelemetry/span_processor.py b/sentry_sdk/integrations/opentelemetry/span_processor.py index 661e5e3629..0ed4e7f709 100644 --- a/sentry_sdk/integrations/opentelemetry/span_processor.py +++ b/sentry_sdk/integrations/opentelemetry/span_processor.py @@ -1,5 +1,3 @@ -from datetime import datetime - from opentelemetry.context import get_value # type: ignore from opentelemetry.sdk.trace import SpanProcessor # type: ignore from opentelemetry.semconv.trace import SpanAttributes # type: ignore @@ -15,6 +13,7 @@ INVALID_SPAN_ID, INVALID_TRACE_ID, ) +from sentry_sdk._compat import utc_from_timestamp from sentry_sdk.consts import INSTRUMENTER from sentry_sdk.hub import Hub from sentry_sdk.integrations.opentelemetry.consts import ( @@ -126,7 +125,7 @@ def on_start(self, otel_span, parent_context=None): sentry_span = sentry_parent_span.start_child( span_id=trace_data["span_id"], description=otel_span.name, - start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9), + start_timestamp=utc_from_timestamp(otel_span.start_time / 1e9), instrumenter=INSTRUMENTER.OTEL, ) else: @@ -136,7 +135,7 @@ def on_start(self, otel_span, parent_context=None): parent_span_id=parent_span_id, trace_id=trace_data["trace_id"], baggage=trace_data["baggage"], - start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9), + start_timestamp=utc_from_timestamp(otel_span.start_time / 1e9), instrumenter=INSTRUMENTER.OTEL, ) @@ -174,9 +173,7 @@ def on_end(self, otel_span): else: self._update_span_with_otel_data(sentry_span, otel_span) - sentry_span.finish( - end_timestamp=datetime.fromtimestamp(otel_span.end_time / 1e9) - ) + sentry_span.finish(end_timestamp=utc_from_timestamp(otel_span.end_time / 1e9)) def _is_sentry_span(self, hub, otel_span): # type: (Hub, OTelSpan) -> bool diff --git a/tests/integrations/opentelemetry/test_span_processor.py b/tests/integrations/opentelemetry/test_span_processor.py index 679e51e808..b7e5a7928d 100644 --- a/tests/integrations/opentelemetry/test_span_processor.py +++ b/tests/integrations/opentelemetry/test_span_processor.py @@ -1,4 +1,5 @@ from datetime import datetime +from datetime import timezone import time import pytest @@ -331,7 +332,9 @@ def test_on_start_transaction(): parent_span_id="abcdef1234567890", trace_id="1234567890abcdef1234567890abcdef", baggage=None, - start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9), + start_timestamp=datetime.fromtimestamp( + otel_span.start_time / 1e9, timezone.utc + ), instrumenter="otel", ) @@ -376,7 +379,9 @@ def test_on_start_child(): fake_span.start_child.assert_called_once_with( span_id="1234567890abcdef", description="Sample OTel Span", - start_timestamp=datetime.fromtimestamp(otel_span.start_time / 1e9), + start_timestamp=datetime.fromtimestamp( + otel_span.start_time / 1e9, timezone.utc + ), instrumenter="otel", )