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

fix(opentelemetry): avoid propagation of empty baggage #2968

Merged
merged 2 commits into from
Jul 1, 2024
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
11 changes: 6 additions & 5 deletions sentry_sdk/integrations/opentelemetry/propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@
SpanContext,
TraceFlags,
)

from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.integrations.opentelemetry.consts import (
SENTRY_BAGGAGE_KEY,
SENTRY_TRACE_KEY,
)
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):
Expand Down Expand Up @@ -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):
Expand Down
52 changes: 48 additions & 4 deletions tests/integrations/opentelemetry/test_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
Loading