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

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

## Description

A backend with only `traces_sample_rate=0.5` (no `traces_sampler`) continues a
sampled trace whose incoming baggage has `sentry-sample_rate=0.01`. The SDK
replaces the incoming sample rate with `1.0` in the baggage it propagates and in
the trace header (dynamic sampling context) of the transaction envelope it sends.

## Steps to Reproduce

1. Install dependencies and run the reproduction:
```bash
uv run python main.py
```

The script exits with 1 when the bug reproduces and 0 when the incoming sample
rate is passed on unchanged. It uses a dummy DSN and an in-memory transport, so
nothing is sent.

## Expected Behavior

Both the outgoing baggage and the transaction trace header carry
`sample_rate=0.01`, the incoming value.

## Actual Behavior

Both carry `sample_rate=1.0`:

```
outgoing baggage: sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-sample_rate=1.0,sentry-sample_rand=0.005,sentry-sampled=true,sentry-public_key=public
transaction envelope trace header: {'trace_id': '771a43a4192642f0b136d5159a501700', 'sample_rate': '1.0', 'sample_rand': '0.005', 'sampled': 'true', 'public_key': 'public'}
BUG REPRODUCED: expected sample_rate=0.01, but baggage sample_rate is '1.0'; trace header sample_rate is '1.0'
```

## Environment

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

A backend with only `traces_sample_rate` continues a sampled trace whose
baggage says `sentry-sample_rate=0.01`. The SDK should pass the incoming
sample rate on unchanged, in both the outgoing baggage and the trace header
(dynamic sampling context) of the transaction it sends.

Bug: both say `sample_rate=1.0` instead of `0.01`.

Exits 1 when the bug reproduces, 0 otherwise. Uses a dummy DSN and an
in-memory transport, so nothing is sent.
"""

import sys

import sentry_sdk
from sentry_sdk.transport import Transport

INCOMING_SAMPLE_RATE = "0.01"


class MemoryTransport(Transport):
def __init__(self, options=None):
super().__init__(options)
self.envelopes = []

def capture_envelope(self, envelope):
self.envelopes.append(envelope)


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

headers = {
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef-1",
"baggage": "sentry-trace_id=771a43a4192642f0b136d5159a501700,"
f"sentry-sample_rate={INCOMING_SAMPLE_RATE},"
"sentry-sample_rand=0.005,sentry-sampled=true,sentry-public_key=public",
}

tx = sentry_sdk.continue_trace(headers, name="t", op="http.server")
with sentry_sdk.start_transaction(tx):
outgoing_baggage = sentry_sdk.get_baggage()

sentry_sdk.flush()

trace_header = None
for envelope in transport.envelopes:
if envelope.get_transaction_event() is not None:
trace_header = envelope.headers.get("trace") or {}

print("sentry-sdk version:", sentry_sdk.VERSION)
print("incoming sample_rate:", INCOMING_SAMPLE_RATE)
print("outgoing baggage:", outgoing_baggage)
print("transaction envelope trace header:", trace_header)

baggage_rate = None
for item in (outgoing_baggage or "").split(","):
key, _, value = item.partition("=")
if key.strip() == "sentry-sample_rate":
baggage_rate = value.strip()

header_rate = (trace_header or {}).get("sample_rate")

problems = []
if baggage_rate != INCOMING_SAMPLE_RATE:
problems.append(f"baggage sample_rate is {baggage_rate!r}")
if header_rate != INCOMING_SAMPLE_RATE:
problems.append(f"trace header sample_rate is {header_rate!r}")

if problems:
print(
f"BUG REPRODUCED: expected sample_rate={INCOMING_SAMPLE_RATE}, but "
+ "; ".join(problems)
)
sys.exit(1)

print("OK: incoming sample_rate was passed on unchanged")
8 changes: 8 additions & 0 deletions sdk-repro-test-python/28/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "continued-trace-sample-rate-repro"
version = "0.1.0"
description = "Reproduction for sdk-repro-test-python#28"
requires-python = ">=3.12"
dependencies = [
"sentry-sdk==2.73.0",
]
45 changes: 45 additions & 0 deletions sdk-repro-test-python/28/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading