Skip to content

Baggage.serialize(include_third_party=True) leaves a trailing comma when there are no third-party items #8120

Open

Description

@alphasafal

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.73.0 (current master)

Steps to Reproduce

Baggage.serialize(include_third_party=True) in sentry_sdk/tracing_utils.py appends self.third_party_items unconditionally. When there are no third-party items it is "", so ",".join(items) ends with an empty member.

from sentry_sdk.tracing_utils import Baggage

baggage = Baggage({"trace_id": "771a43a4192642f0b136d5159a501700", "sample_rate": "0.5"})
print(baggage.serialize(include_third_party=True))

This is reachable from the Celery integration: _update_celery_task_headers calls combined.serialize(include_third_party=True) when it merges incoming and Sentry baggage, so the common Sentry -> Celery -> Sentry case (incoming baggage with only sentry- items) sends the outgoing baggage header with the stray comma.

Expected Result

No trailing comma when there are no third-party items:

sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rate=0.5

The output with third-party items present stays as it is today.

Actual Result

sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rate=0.5,

Under the W3C Baggage spec the trailing comma is an empty list-member. Sentry's own parser tolerates it, but a strict W3C / OpenTelemetry baggage parser on a downstream service can reject or mishandle the header.

Proposed fix: only append third_party_items when it is non-empty (if include_third_party and self.third_party_items:). I have this ready with a test in #8119 and am happy to adjust the approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions