Skip to content
Draft
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
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