Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the transaction/span status from an otel span #2115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sentry_sdk/integrations/opentelemetry/span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def on_end(self, otel_span):

sentry_span.op = otel_span.name

self._update_span_with_otel_status(sentry_span, otel_span)

if isinstance(sentry_span, Transaction):
sentry_span.name = otel_span.name
sentry_span.set_context(
Expand Down Expand Up @@ -234,6 +236,20 @@ def _get_trace_data(self, otel_span, parent_context):

return trace_data

def _update_span_with_otel_status(self, sentry_span, otel_span):
# type: (SentrySpan, OTelSpan) -> None
"""
Set the Sentry span status from the OTel span
"""
if otel_span.status.is_unset:
return

if otel_span.status.is_ok:
sentry_span.set_status("ok")
return

sentry_span.set_status("internal_error")

def _update_span_with_otel_data(self, sentry_span, otel_span):
# type: (SentrySpan, OTelSpan) -> None
"""
Expand Down
29 changes: 28 additions & 1 deletion tests/integrations/opentelemetry/test_span_processor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
import time
import pytest

try:
from unittest import mock # python 3.3 and above
Expand All @@ -14,7 +15,7 @@
)
from sentry_sdk.tracing import Span, Transaction

from opentelemetry.trace import SpanKind, SpanContext
from opentelemetry.trace import SpanKind, SpanContext, Status, StatusCode
from sentry_sdk.tracing_utils import extract_sentrytrace_data


Expand Down Expand Up @@ -218,6 +219,28 @@ def test_update_span_with_otel_data_http_method():
assert sentry_span._data["http.target"] == "/"


@pytest.mark.parametrize(
"otel_status, expected_status",
[
pytest.param(Status(StatusCode.UNSET), None, id="unset"),
pytest.param(Status(StatusCode.OK), "ok", id="ok"),
pytest.param(Status(StatusCode.ERROR), "internal_error", id="error"),
],
)
def test_update_span_with_otel_status(otel_status, expected_status):
sentry_span = Span()

otel_span = MagicMock()
otel_span.name = "Test OTel Span"
otel_span.kind = SpanKind.INTERNAL
otel_span.status = otel_status

span_processor = SentrySpanProcessor()
span_processor._update_span_with_otel_status(sentry_span, otel_span)

assert sentry_span.get_trace_context().get("status") == expected_status


def test_update_span_with_otel_data_http_method2():
sentry_span = Span()

Expand Down Expand Up @@ -394,6 +417,7 @@ def test_on_end_sentry_transaction():
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.end_time = time.time_ns()
otel_span.status = Status(StatusCode.OK)
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
Expand All @@ -414,6 +438,7 @@ def test_on_end_sentry_transaction():

fake_sentry_span.set_context.assert_called_once()
span_processor._update_span_with_otel_data.assert_not_called()
fake_sentry_span.set_status.assert_called_once_with("ok")
fake_sentry_span.finish.assert_called_once()


Expand All @@ -424,6 +449,7 @@ def test_on_end_sentry_span():
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.end_time = time.time_ns()
otel_span.status = Status(StatusCode.OK)
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
Expand All @@ -446,6 +472,7 @@ def test_on_end_sentry_span():
span_processor._update_span_with_otel_data.assert_called_once_with(
fake_sentry_span, otel_span
)
fake_sentry_span.set_status.assert_called_once_with("ok")
fake_sentry_span.finish.assert_called_once()


Expand Down