From 4087550886038c367c3be37a4cf337b39bdafc14 Mon Sep 17 00:00:00 2001 From: Nathaniel Brown Date: Sat, 8 Apr 2023 09:00:48 -0400 Subject: [PATCH] Fixes propagation of Open Telemetry NonRecordingSpan --- .../integrations/opentelemetry/propagator.py | 6 ++-- .../opentelemetry/test_propagator.py | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/propagator.py b/sentry_sdk/integrations/opentelemetry/propagator.py index 3e1f696939..fe1d5bc180 100644 --- a/sentry_sdk/integrations/opentelemetry/propagator.py +++ b/sentry_sdk/integrations/opentelemetry/propagator.py @@ -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) diff --git a/tests/integrations/opentelemetry/test_propagator.py b/tests/integrations/opentelemetry/test_propagator.py index 529aa99c09..b52a562bdb 100644 --- a/tests/integrations/opentelemetry/test_propagator.py +++ b/tests/integrations/opentelemetry/test_propagator.py @@ -7,6 +7,7 @@ set_span_in_context, TraceFlags, SpanContext, + NonRecordingSpan, ) from sentry_sdk.integrations.opentelemetry.consts import ( SENTRY_BAGGAGE_KEY, @@ -18,6 +19,7 @@ from sentry_sdk.tracing_utils import Baggage + def test_extract_no_context_no_sentry_trace_header(): """ No context and NO Sentry trace data in getter. @@ -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", @@ -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( @@ -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(