sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

Handle multiple values in incoming trace headers

Open antonpirker opened this issue 1 year ago • 0 comments

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-trace headers like this, we should just pick the first one
  • If we get two baggage headers 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

antonpirker avatar Mar 12 '24 08:03 antonpirker