Skip to content
Closed
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
53 changes: 53 additions & 0 deletions sdk-repro-test-python/25/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Reproduction for sdk-repro-test-python#25

**Issue:** https://github.com/getsentry/sdk-repro-test-python/issues/25

## Description

When a client sends the `sentry-trace` header twice, many server frameworks join the repeated headers into one comma-separated value. `sentry_sdk.continue_trace` does not parse that value, so it starts a new trace instead of continuing the incoming one, and the trace is split.

The cause is in `sentry_sdk/tracing_utils.py`: `extract_sentrytrace_data` matches the whole header against `SENTRY_TRACE_REGEX`, which is anchored with `^…$`. A comma-joined value does not match, so the function returns `None` and the incoming trace is dropped.

## Steps to Reproduce

1. Install dependencies:
```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

2. Run the reproduction:
```bash
python repro.py
```

The script exits with `1` when the bug reproduces.

## Expected Behavior

The transaction continues from the first header value:

```
trace_id=efa64e95faf54da59b81cce3fb159825 parent_span_id=b8efba9273e7a14f
```

## Actual Behavior

A single header is continued, but the comma-joined header starts a new random trace with no parent span:

```
sentry-sdk 2.64.0
single header: trace_id=efa64e95faf54da59b81cce3fb159825 parent_span_id=b8efba9273e7a14f
joined headers: trace_id=6aa83115cbad4dc4992d1ba4daca8e39 parent_span_id=None
expected (joined): trace_id=efa64e95faf54da59b81cce3fb159825 parent_span_id=b8efba9273e7a14f

BUG REPRODUCED: the comma-joined sentry-trace header started a new trace.
```

The same result happens with the current `getsentry/sdk-repro-test-python` source (2.75.0).

## Environment

- Python: 3.12
- sentry-sdk: 2.64.0
64 changes: 64 additions & 0 deletions sdk-repro-test-python/25/repro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Reproduction for getsentry/sdk-repro-test-python#25

When a client sends the `sentry-trace` header twice, many server frameworks
join the repeated headers into one comma-separated value. `continue_trace`
should continue the trace from the first value. Instead, the SDK starts a new
trace, so the incoming trace is split.

Exits non-zero when the bug reproduces.
"""

import sys

import sentry_sdk
from sentry_sdk.transport import Transport

TRACE_ID = "efa64e95faf54da59b81cce3fb159825"
FIRST_SPAN_ID = "b8efba9273e7a14f"
SECOND_SPAN_ID = "b8ffba9273e7a14f"


class NoopTransport(Transport):
# Send nothing; we only care about the transaction the SDK builds.
def capture_envelope(self, envelope):
pass


sentry_sdk.init(
dsn="https://public@example.com/1",
transport=NoopTransport(),
traces_sample_rate=1.0,
)

print(f"sentry-sdk {sentry_sdk.VERSION}")

# Control: a single header continues the trace as expected.
single = sentry_sdk.continue_trace(
{"sentry-trace": f"{TRACE_ID}-{FIRST_SPAN_ID}-1"},
op="http.server",
name="single",
)
print(f"single header: trace_id={single.trace_id} parent_span_id={single.parent_span_id}")

# Bug: the same header sent twice and joined with a comma.
joined = sentry_sdk.continue_trace(
{
"sentry-trace": f"{TRACE_ID}-{FIRST_SPAN_ID}-1,"
f"{TRACE_ID}-{SECOND_SPAN_ID}-1"
},
op="http.server",
name="joined",
)
print(f"joined headers: trace_id={joined.trace_id} parent_span_id={joined.parent_span_id}")
print(f"expected (joined): trace_id={TRACE_ID} parent_span_id={FIRST_SPAN_ID}")

if single.trace_id != TRACE_ID or single.parent_span_id != FIRST_SPAN_ID:
print("\nUNEXPECTED: even a single sentry-trace header was not continued.")
sys.exit(2)

if joined.trace_id != TRACE_ID or joined.parent_span_id != FIRST_SPAN_ID:
print("\nBUG REPRODUCED: the comma-joined sentry-trace header started a new trace.")
sys.exit(1)

print("\nNot reproduced: the trace was continued from the first header.")
1 change: 1 addition & 0 deletions sdk-repro-test-python/25/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sentry-sdk==2.64.0
Loading