Skip to content
Open
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
2 changes: 1 addition & 1 deletion sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ def serialize(self, include_third_party: bool = False) -> str:
item = Baggage.SENTRY_PREFIX + quote(key) + "=" + quote(str(val))
items.append(item)

if include_third_party:
if include_third_party and self.third_party_items:
items.append(self.third_party_items)

return ",".join(items)
Expand Down
28 changes: 28 additions & 0 deletions tests/integrations/celery/test_update_celery_task_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"custom=value"
)

SENTRY_ONLY_BAGGAGE_VALUE = (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=0.1337"
)

SENTRY_TRACE_VALUE = "771a43a4192642f0b136d5159a501700-1234567890abcdef-1"


Expand Down Expand Up @@ -124,6 +130,28 @@ def test_span_with_transaction_custom_headers(sentry_init):
)


def test_span_with_transaction_custom_headers_without_third_party_baggage(
sentry_init,
):
sentry_init(traces_sample_rate=1.0)
headers = {
"baggage": SENTRY_ONLY_BAGGAGE_VALUE,
"sentry-trace": SENTRY_TRACE_VALUE,
}

with sentry_sdk.start_transaction(name="test_transaction"):
with sentry_sdk.start_span(op="test_span") as span:
outgoing_headers = _update_celery_task_headers(headers, span, False)

for baggage in (
outgoing_headers["baggage"],
outgoing_headers["headers"]["baggage"],
):
assert baggage.startswith("sentry-")
assert not baggage.endswith(",")
assert "custom=" not in baggage


@pytest.mark.parametrize("monitor_beat_tasks", [True, False])
def test_celery_trace_propagation_default(sentry_init, monitor_beat_tasks):
"""
Expand Down
24 changes: 24 additions & 0 deletions tests/tracing/test_baggage.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ def test_mixed_baggage():
)


def test_serialize_no_trailing_comma_without_third_party_items():
# With Sentry items but no third-party items, include_third_party=True must not
# append an empty segment, which would leave a trailing comma in the header.
header = (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=0.01337"
)
baggage = Baggage.from_incoming_header(header)

assert baggage.third_party_items == ""

serialized = baggage.serialize(include_third_party=True)

assert not serialized.endswith(",")
# with no third-party items, include_third_party should change nothing
assert serialized == baggage.serialize()
assert serialized == (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=0.01337"
)


def test_malformed_baggage():
header = ","

Expand Down