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

Fixes Open Telemetry propagation for NonRecordingSpan #1995

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions sentry_sdk/integrations/opentelemetry/propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ def inject(self, carrier, context=None, setter=default_setter):
context = get_current()

current_span = trace.get_current_span(context)

if not current_span.context.is_valid:
span_context = current_span.get_span_context()
if not span_context.is_valid:
return

span_id = trace.format_span_id(current_span.context.span_id)
span_id = trace.format_span_id(span_context.span_id)

span_map = SentrySpanProcessor().otel_span_map
sentry_span = span_map.get(span_id, None)
Expand Down
32 changes: 29 additions & 3 deletions tests/integrations/opentelemetry/test_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
set_span_in_context,
TraceFlags,
SpanContext,
NonRecordingSpan,
)
from sentry_sdk.integrations.opentelemetry.consts import (
SENTRY_BAGGAGE_KEY,
Expand All @@ -18,6 +19,7 @@
from sentry_sdk.tracing_utils import Baggage



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra blank line is making the linter fail.

def test_extract_no_context_no_sentry_trace_header():
"""
No context and NO Sentry trace data in getter.
Expand Down Expand Up @@ -135,7 +137,31 @@ def test_inject_empty_otel_span_map():
is_remote=True,
)
span = MagicMock()
span.context = span_context
span.get_span_context.return_value = span_context

with mock.patch(
"sentry_sdk.integrations.opentelemetry.propagator.trace.get_current_span",
return_value=span,
):
full_context = set_span_in_context(span, context)
SentryPropagator().inject(carrier, full_context, setter)

setter.set.assert_not_called()


def test_inject_sentry_span_non_recording_span():
carrier = None
context = get_current()
setter = MagicMock()
setter.set = MagicMock()

span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
trace_flags=TraceFlags(TraceFlags.SAMPLED),
is_remote=True,
)
span = NonRecordingSpan(context=span_context)

with mock.patch(
"sentry_sdk.integrations.opentelemetry.propagator.trace.get_current_span",
Expand Down Expand Up @@ -166,7 +192,7 @@ def test_inject_sentry_span_no_baggage():
is_remote=True,
)
span = MagicMock()
span.context = span_context
span.get_span_context.return_value = span_context

sentry_span = MagicMock()
sentry_span.to_traceparent = mock.Mock(
Expand Down Expand Up @@ -210,7 +236,7 @@ def test_inject_sentry_span_baggage():
is_remote=True,
)
span = MagicMock()
span.context = span_context
span.get_span_context.return_value = span_context

sentry_span = MagicMock()
sentry_span.to_traceparent = mock.Mock(
Expand Down