diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 8a6961ba15..2ac3a67e4b 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -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) diff --git a/tests/test_tracing_utils.py b/tests/test_tracing_utils.py index 3098e73667..293d83b43f 100644 --- a/tests/test_tracing_utils.py +++ b/tests/test_tracing_utils.py @@ -133,11 +133,11 @@ def test_strip_sentry_baggage(header, expected): (Baggage(sentry_items={}, mutable=False), ''), ( Baggage(sentry_items={"foo": "bar"}), - '', + '', ), ( Baggage(sentry_items={"foo": "bar"}, mutable=False), - '', + '', ), ( Baggage(sentry_items={"foo": "bar"}, third_party_items="asdf=1234,"), diff --git a/tests/tracing/test_baggage.py b/tests/tracing/test_baggage.py index 1e0075feaa..3d70a7cfea 100644 --- a/tests/tracing/test_baggage.py +++ b/tests/tracing/test_baggage.py @@ -67,6 +67,27 @@ def test_mixed_baggage(): ) +def test_sentry_only_baggage(): + 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) + + # 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 = ","