Skip to content

Commit

Permalink
Fix timestamp in transaction created by OTel (#2627)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker authored Jan 9, 2024
1 parent b873c38 commit b74ea08
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
11 changes: 4 additions & 7 deletions sentry_sdk/integrations/opentelemetry/span_processor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -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:
Expand All @@ -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,
)

Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions tests/integrations/opentelemetry/test_span_processor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from datetime import timezone
import time
import pytest

Expand Down Expand Up @@ -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",
)

Expand Down Expand Up @@ -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",
)

Expand Down

0 comments on commit b74ea08

Please sign in to comment.