Skip to content

Commit feb2c13

Browse files
committed
test
1 parent 75f64c6 commit feb2c13

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

sentry_sdk/scope.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,9 @@ def start_transaction(
10431043
sampling_context.update(custom_sampling_context)
10441044
transaction._set_initial_sampling_decision(sampling_context=sampling_context)
10451045

1046-
# update the sample rate in the dsc
1046+
# update the sample rate in the dsc. the baggage should be immutable
1047+
# at this point, but in order to not have broken traces we make
1048+
# an exception here
10471049
propagation_context = self.get_active_propagation_context()
10481050
if propagation_context:
10491051
dsc = propagation_context.dynamic_sampling_context

sentry_sdk/tracing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,6 @@ def get_baggage(self):
10611061
10621062
The first time a new baggage with Sentry items is made,
10631063
it will be frozen."""
1064-
10651064
if not self._baggage or self._baggage.mutable:
10661065
self._baggage = Baggage.populate_from_transaction(self)
10671066

tests/test_dsc.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
This is not tested in this file.
99
"""
1010

11+
import random
12+
from unittest import mock
13+
1114
import pytest
1215

1316
import sentry_sdk
@@ -134,6 +137,82 @@ def test_dsc_continuation_of_trace(sentry_init, capture_envelopes):
134137
assert envelope_trace_header["transaction"] == "bar"
135138

136139

140+
def test_dsc_continuation_of_trace_sample_rate_changed(sentry_init, capture_envelopes):
141+
"""
142+
Another service calls our service and passes tracing information to us.
143+
Our service is continuing the trace, but modifies the sample rate.
144+
The DSC propagated further should contain the updated sample rate.
145+
"""
146+
147+
def my_traces_sampler(sampling_context):
148+
return 0.25
149+
150+
sentry_init(
151+
dsn="https://[email protected]/12312012",
152+
release="[email protected]",
153+
environment="canary",
154+
traces_sampler=my_traces_sampler,
155+
)
156+
envelopes = capture_envelopes()
157+
158+
# This is what the upstream service sends us
159+
sentry_trace = "771a43a4192642f0b136d5159a501700-1234567890abcdef-1"
160+
baggage = (
161+
"other-vendor-value-1=foo;bar;baz, "
162+
"sentry-trace_id=771a43a4192642f0b136d5159a501700, "
163+
"sentry-public_key=frontendpublickey, "
164+
"sentry-sample_rate=1.0, "
165+
"sentry-sampled=true, "
166+
167+
"sentry-environment=bird, "
168+
"sentry-transaction=bar, "
169+
"other-vendor-value-2=foo;bar;"
170+
)
171+
incoming_http_headers = {
172+
"HTTP_SENTRY_TRACE": sentry_trace,
173+
"HTTP_BAGGAGE": baggage,
174+
}
175+
176+
# We continue the incoming trace and start a new transaction
177+
with mock.patch.object(random, "random", return_value=0.2):
178+
transaction = sentry_sdk.continue_trace(incoming_http_headers)
179+
with sentry_sdk.start_transaction(transaction, name="foo"):
180+
pass
181+
182+
assert len(envelopes) == 1
183+
184+
transaction_envelope = envelopes[0]
185+
envelope_trace_header = transaction_envelope.headers["trace"]
186+
187+
assert "trace_id" in envelope_trace_header
188+
assert type(envelope_trace_header["trace_id"]) == str
189+
assert envelope_trace_header["trace_id"] == "771a43a4192642f0b136d5159a501700"
190+
191+
assert "public_key" in envelope_trace_header
192+
assert type(envelope_trace_header["public_key"]) == str
193+
assert envelope_trace_header["public_key"] == "frontendpublickey"
194+
195+
assert "sample_rate" in envelope_trace_header
196+
assert type(envelope_trace_header["sample_rate"]) == str
197+
assert envelope_trace_header["sample_rate"] == "0.25"
198+
199+
assert "sampled" in envelope_trace_header
200+
assert type(envelope_trace_header["sampled"]) == str
201+
assert envelope_trace_header["sampled"] == "true"
202+
203+
assert "release" in envelope_trace_header
204+
assert type(envelope_trace_header["release"]) == str
205+
assert envelope_trace_header["release"] == "[email protected]"
206+
207+
assert "environment" in envelope_trace_header
208+
assert type(envelope_trace_header["environment"]) == str
209+
assert envelope_trace_header["environment"] == "bird"
210+
211+
assert "transaction" in envelope_trace_header
212+
assert type(envelope_trace_header["transaction"]) == str
213+
assert envelope_trace_header["transaction"] == "bar"
214+
215+
137216
def test_dsc_issue(sentry_init, capture_envelopes):
138217
"""
139218
Our service is a standalone service that does not have tracing enabled. Just uses Sentry for error reporting.

0 commit comments

Comments
 (0)