sentry-python
sentry-python copied to clipboard
Handle multiple values in incoming trace headers
When we get an incoming sentry-trace or baggage header with multiple values separated by a comma our header parsing function fails silently and starting a new span:
Example header:
sentry-trace: 6a5871d13d8441818ccc593c6c3b07c8-b252c3821bdbaceb-1, 6a5871d13d8441818ccc593c6c3b07c8-88529d2712c386fd-1
Current behavior:
>>> from sentry_sdk.tracing_utils import extract_sentrytrace_data as extract
>>> header1 = "6a5871d13d8441818ccc593c6c3b07c8-b252c3821bdbaceb-1"
>>> header2="6a5871d13d8441818ccc593c6c3b07c8-b252c3821bdbaceb-1, 6a5871d13d8441818ccc593c6c3b07c8-88529d2712c386fd-1"
>>> extract(header1)
{'trace_id': '6a5871d13d8441818ccc593c6c3b07c8', 'parent_span_id': 'b252c3821bdbaceb', 'parent_sampled': True}
>>> extract(header2)
>>>
Guard against this on incoming headers:
- If we get two
sentry-traceheaders like this, we should just pick the first one - If we get two
baggageheaders we need to look in all of them to find the sentry information, if both have them take the first.
See also issue in Sentry where this originated: https://github.com/getsentry/sentry/issues/66701
Related issue for outgoing headers: https://github.com/getsentry/sentry-python/issues/2803