Skip to content
Merged
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
16 changes: 14 additions & 2 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,10 +1188,17 @@
"parent_sampled": transaction.parent_sampled,
}
sampling_context.update(custom_sampling_context)

keep_incoming_sample_rate = (
transaction.sampled is None
and transaction.parent_sampled is not None
and not callable(client.options.get("traces_sampler"))
)

transaction._set_initial_sampling_decision(sampling_context=sampling_context)

# update the sample rate in the dsc
if transaction.sample_rate is not None:
if transaction.sample_rate is not None and not keep_incoming_sample_rate:

Check warning on line 1201 in sentry_sdk/scope.py

View check run for this annotation

@sentry/warden / warden: code-review

Backpressure-adjusted sample rate is not written to DSC

`keep_incoming_sample_rate` stays true under backpressure, so a downsampled `transaction.sample_rate` never updates baggage/DSC even though this PR says backpressure should override; also gate on `client.monitor.downsample_factor` (or compare rates) and add a regression test.
Comment thread
sentrivana marked this conversation as resolved.
Resolved
Comment thread
sentrivana marked this conversation as resolved.
Resolved
propagation_context = self.get_active_propagation_context()
baggage = propagation_context.baggage

Expand Down Expand Up @@ -1337,7 +1344,12 @@
self,
)

if sample_rate is not None:
keep_incoming_sample_rate = (
propagation_context.parent_sampled is not None
and not callable(self.get_client().options.get("traces_sampler"))
)

if sample_rate is not None and not keep_incoming_sample_rate:
self._update_sample_rate(sample_rate)

if sampled is False or sampled is None:
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/stdlib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def getresponse(self, *args, **kwargs):
expected_outgoing_baggage = (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=1.0,"
"sentry-sample_rate=0.01337,"
"sentry-user_id=Am%C3%A9lie,"
"sentry-sample_rand=0.000005,"
"sentry-sampled=true"
Expand Down Expand Up @@ -487,7 +487,7 @@ def getresponse(self, *args, **kwargs):
expected_outgoing_baggage = (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=1.0,"
"sentry-sample_rate=0.01337,"
"sentry-user_id=Am%C3%A9lie,"
"sentry-sample_rand=0.000005,"
"sentry-sampled=true"
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_dsc_continuation_of_trace(sentry_init, capture_envelopes):
dsn="https://mysecret@o1234.ingest.sentry.io/12312012",
release="myapp@0.0.1",
environment="canary",
traces_sample_rate=1.0,
traces_sample_rate=0.5,
transport=TestTransportWithOptions,
)
envelopes = capture_envelopes()
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_dsc_continuation_of_trace(sentry_init, capture_envelopes):

assert "sample_rate" in envelope_trace_header
assert type(envelope_trace_header["sample_rate"]) == str
assert envelope_trace_header["sample_rate"] == "1.0"
assert envelope_trace_header["sample_rate"] == "0.01337"

assert "sampled" in envelope_trace_header
assert type(envelope_trace_header["sampled"]) == str
Expand Down Expand Up @@ -272,7 +272,7 @@ def test_dsc_continuation_of_trace_span_streaming(sentry_init, capture_envelopes
dsn="https://mysecret@o1234.ingest.sentry.io/12312012",
release="myapp@0.0.1",
environment="canary",
traces_sample_rate=1.0,
traces_sample_rate=0.5,
trace_lifecycle="stream",
transport=TestTransportWithOptions,
)
Expand Down Expand Up @@ -318,7 +318,7 @@ def test_dsc_continuation_of_trace_span_streaming(sentry_init, capture_envelopes

assert "sample_rate" in envelope_trace_header
assert type(envelope_trace_header["sample_rate"]) == str
assert envelope_trace_header["sample_rate"] == "1.0"
assert envelope_trace_header["sample_rate"] == "0.01337"

assert "sampled" in envelope_trace_header
assert type(envelope_trace_header["sampled"]) == str
Expand Down
6 changes: 3 additions & 3 deletions tests/tracing/test_integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_continue_trace(sentry_init, capture_envelopes, parent_sampled, sample_r
)

if parent_sampled is not None:
expected_sample_rate = str(float(parent_sampled))
expected_sample_rate = "0.01337"
else:
expected_sample_rate = str(sample_rate)

Expand Down Expand Up @@ -262,7 +262,7 @@ def test_continue_trace_span_streaming(
assert not baggage.mutable

if parent_sampled is not None:
expected_sample_rate = str(float(parent_sampled))
expected_sample_rate = parent_sample_rate
else:
if sample_rate is None:
expected_sample_rate = parent_sample_rate
Expand Down Expand Up @@ -312,7 +312,7 @@ def test_continue_trace_span_streaming(
)

if parent_sampled is not None:
expected_sample_rate = str(float(parent_sampled))
expected_sample_rate = parent_sample_rate
else:
if sample_rate is None:
expected_sample_rate = parent_sample_rate
Expand Down
12 changes: 2 additions & 10 deletions tests/tracing/test_span_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,7 @@ def test_outgoing_traceparent_and_baggage_incoming_trace(
baggage_items = dict(tuple(item.split("=")) for item in baggage.split(","))

# As we've received incoming baggage, we shouldn't modify it ourselves
# and should propagate it as-is. However, in the case where our
# effective sample rate overrides the parent sample rate, we update the
# sample rate in the baggage as a consequence of updating the sample rate
# in the DSC.
if traces_sample_rate is not None:
incoming_baggage["sentry-sample_rate"] = str(float(parent_sampled))
# and should propagate it as-is

assert baggage_items == incoming_baggage

Expand Down Expand Up @@ -1133,12 +1128,9 @@ def test_outgoing_traceparent_and_baggage_inconsistent_incoming_trace(
span_id = span.span_id
assert traceparent == f"{trace_id}-{span_id}-0"

# We shouldn't be updating incoming baggage, but in the case where our
# effective sample rate overrides the parent sample rate, we do this as
# a consequence of updating the sample rate in the DSC.
# We shouldn't be updating incoming baggage
baggage = sentry_sdk.get_baggage()
baggage_items = dict(tuple(item.split("=")) for item in baggage.split(","))
incoming_baggage["sentry-sample_rate"] = "0.0"
assert baggage_items == incoming_baggage


Expand Down
Loading