diff --git a/sentry_sdk/integrations/opentelemetry/propagator.py b/sentry_sdk/integrations/opentelemetry/propagator.py index d3fdc2306d..3df2ee2f2f 100644 --- a/sentry_sdk/integrations/opentelemetry/propagator.py +++ b/sentry_sdk/integrations/opentelemetry/propagator.py @@ -17,6 +17,8 @@ SpanContext, TraceFlags, ) + +from sentry_sdk._types import TYPE_CHECKING from sentry_sdk.integrations.opentelemetry.consts import ( SENTRY_BAGGAGE_KEY, SENTRY_TRACE_KEY, @@ -24,17 +26,14 @@ from sentry_sdk.integrations.opentelemetry.span_processor import ( SentrySpanProcessor, ) - from sentry_sdk.tracing import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, ) from sentry_sdk.tracing_utils import Baggage, extract_sentrytrace_data -from sentry_sdk._types import TYPE_CHECKING if TYPE_CHECKING: - from typing import Optional - from typing import Set + from typing import Optional, Set class SentryPropagator(TextMapPropagator): @@ -107,7 +106,9 @@ def inject(self, carrier, context=None, setter=default_setter): if sentry_span.containing_transaction: baggage = sentry_span.containing_transaction.get_baggage() if baggage: - setter.set(carrier, BAGGAGE_HEADER_NAME, baggage.serialize()) + baggage_data = baggage.serialize() + if baggage_data: + setter.set(carrier, BAGGAGE_HEADER_NAME, baggage_data) @property def fields(self): diff --git a/tests/integrations/opentelemetry/test_propagator.py b/tests/integrations/opentelemetry/test_propagator.py index 1b3249e87c..d999b0bb2b 100644 --- a/tests/integrations/opentelemetry/test_propagator.py +++ b/tests/integrations/opentelemetry/test_propagator.py @@ -4,12 +4,13 @@ from unittest.mock import MagicMock from opentelemetry.context import get_current -from opentelemetry.trace.propagation import get_current_span from opentelemetry.trace import ( - set_span_in_context, - TraceFlags, SpanContext, + TraceFlags, + set_span_in_context, ) +from opentelemetry.trace.propagation import get_current_span + from sentry_sdk.integrations.opentelemetry.consts import ( SENTRY_BAGGAGE_KEY, SENTRY_TRACE_KEY, @@ -198,7 +199,50 @@ def test_inject_sentry_span_no_baggage(): ) -@pytest.mark.forked +def test_inject_sentry_span_empty_baggage(): + """ + Inject a sentry span with no baggage. + """ + carrier = None + context = get_current() + setter = MagicMock() + setter.set = MagicMock() + + trace_id = "1234567890abcdef1234567890abcdef" + span_id = "1234567890abcdef" + + span_context = SpanContext( + trace_id=int(trace_id, 16), + span_id=int(span_id, 16), + trace_flags=TraceFlags(TraceFlags.SAMPLED), + is_remote=True, + ) + span = MagicMock() + span.get_span_context.return_value = span_context + + sentry_span = MagicMock() + sentry_span.to_traceparent = mock.Mock( + return_value="1234567890abcdef1234567890abcdef-1234567890abcdef-1" + ) + sentry_span.containing_transaction.get_baggage = mock.Mock(return_value=Baggage({})) + + span_processor = SentrySpanProcessor() + span_processor.otel_span_map[span_id] = sentry_span + + 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_called_once_with( + carrier, + "sentry-trace", + "1234567890abcdef1234567890abcdef-1234567890abcdef-1", + ) + + def test_inject_sentry_span_baggage(): """ Inject a sentry span with baggage.