Skip to content

Commit d874091

Browse files
authored
Add Sampling Decision to Trace Envelope Header (#2239)
1 parent 5704f12 commit d874091

File tree

7 files changed

+13
-6
lines changed

7 files changed

+13
-6
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ def populate_from_transaction(cls, transaction):
330330
if transaction.sample_rate is not None:
331331
sentry_items["sample_rate"] = str(transaction.sample_rate)
332332

333+
if transaction.sampled is not None:
334+
sentry_items["sampled"] = "true" if transaction.sampled else "false"
335+
333336
# there's an existing baggage but it was mutable,
334337
# which is why we are creating this new baggage.
335338
# However, if by chance the user put some sentry items in there, give them precedence.

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,5 @@ async def handler(request):
532532

533533
assert (
534534
resp.request_info.headers["baggage"]
535-
== "custom=value,sentry-trace_id=0123456789012345678901234567890,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0"
535+
== "custom=value,sentry-trace_id=0123456789012345678901234567890,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true"
536536
)

tests/integrations/celery/test_celery.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ def dummy_task(self, x, y):
522522
"sentry-trace_id={}".format(transaction.trace_id),
523523
"sentry-environment=production",
524524
"sentry-sample_rate=1.0",
525+
"sentry-sampled=true",
525526
"custom=value",
526527
]
527528
)

tests/integrations/httpx/test_httpx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_outgoing_trace_headers_append_to_baggage(sentry_init, httpx_client):
125125
)
126126
assert (
127127
response.request.headers["baggage"]
128-
== "custom=data,sentry-trace_id=01234567890123456789012345678901,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0"
128+
== "custom=data,sentry-trace_id=01234567890123456789012345678901,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true"
129129
)
130130

131131

tests/integrations/stdlib/test_httplib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
228228
expected_outgoing_baggage_items = [
229229
"sentry-trace_id=%s" % transaction.trace_id,
230230
"sentry-sample_rate=0.5",
231+
"sentry-sampled=%s" % "true" if transaction.sampled else "false",
231232
"sentry-release=foo",
232233
"sentry-environment=production",
233234
]

tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def test_baggage_with_tracing_disabled(sentry_init):
8383
def test_baggage_with_tracing_enabled(sentry_init):
8484
sentry_init(traces_sample_rate=1.0, release="1.0.0", environment="dev")
8585
with start_transaction() as transaction:
86-
expected_baggage = "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0,sentry-sample_rate=1.0".format(
87-
transaction.trace_id
86+
expected_baggage = "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0,sentry-sample_rate=1.0,sentry-sampled={}".format(
87+
transaction.trace_id, "true" if transaction.sampled else "false"
8888
)
8989
# order not guaranteed in older python versions
9090
assert sorted(get_baggage().split(",")) == sorted(expected_baggage.split(","))

tests/tracing/test_integration_tests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,14 @@ def test_dynamic_sampling_head_sdk_creates_dsc(
172172
"environment": "production",
173173
"release": "foo",
174174
"sample_rate": str(sample_rate),
175+
"sampled": "true" if transaction.sampled else "false",
175176
"transaction": "Head SDK tx",
176177
"trace_id": trace_id,
177178
}
178179

179180
expected_baggage = (
180-
"sentry-environment=production,sentry-release=foo,sentry-sample_rate=%s,sentry-transaction=Head%%20SDK%%20tx,sentry-trace_id=%s"
181-
% (sample_rate, trace_id)
181+
"sentry-environment=production,sentry-release=foo,sentry-sample_rate=%s,sentry-transaction=Head%%20SDK%%20tx,sentry-trace_id=%s,sentry-sampled=%s"
182+
% (sample_rate, trace_id, "true" if transaction.sampled else "false")
182183
)
183184
assert sorted(baggage.serialize().split(",")) == sorted(expected_baggage.split(","))
184185

@@ -188,6 +189,7 @@ def test_dynamic_sampling_head_sdk_creates_dsc(
188189
"environment": "production",
189190
"release": "foo",
190191
"sample_rate": str(sample_rate),
192+
"sampled": "true" if transaction.sampled else "false",
191193
"transaction": "Head SDK tx",
192194
"trace_id": trace_id,
193195
}

0 commit comments

Comments
 (0)