Skip to content

Commit

Permalink
Add Sampling Decision to Trace Envelope Header (#2239)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker committed Jul 12, 2023
1 parent 5704f12 commit d874091
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ def populate_from_transaction(cls, transaction):
if transaction.sample_rate is not None:
sentry_items["sample_rate"] = str(transaction.sample_rate)

if transaction.sampled is not None:
sentry_items["sampled"] = "true" if transaction.sampled else "false"

# there's an existing baggage but it was mutable,
# which is why we are creating this new baggage.
# However, if by chance the user put some sentry items in there, give them precedence.
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,5 @@ async def handler(request):

assert (
resp.request_info.headers["baggage"]
== "custom=value,sentry-trace_id=0123456789012345678901234567890,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0"
== "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"
)
1 change: 1 addition & 0 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ def dummy_task(self, x, y):
"sentry-trace_id={}".format(transaction.trace_id),
"sentry-environment=production",
"sentry-sample_rate=1.0",
"sentry-sampled=true",
"custom=value",
]
)
2 changes: 1 addition & 1 deletion tests/integrations/httpx/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_outgoing_trace_headers_append_to_baggage(sentry_init, httpx_client):
)
assert (
response.request.headers["baggage"]
== "custom=data,sentry-trace_id=01234567890123456789012345678901,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0"
== "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"
)


Expand Down
1 change: 1 addition & 0 deletions tests/integrations/stdlib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
expected_outgoing_baggage_items = [
"sentry-trace_id=%s" % transaction.trace_id,
"sentry-sample_rate=0.5",
"sentry-sampled=%s" % "true" if transaction.sampled else "false",
"sentry-release=foo",
"sentry-environment=production",
]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def test_baggage_with_tracing_disabled(sentry_init):
def test_baggage_with_tracing_enabled(sentry_init):
sentry_init(traces_sample_rate=1.0, release="1.0.0", environment="dev")
with start_transaction() as transaction:
expected_baggage = "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0,sentry-sample_rate=1.0".format(
transaction.trace_id
expected_baggage = "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0,sentry-sample_rate=1.0,sentry-sampled={}".format(
transaction.trace_id, "true" if transaction.sampled else "false"
)
# order not guaranteed in older python versions
assert sorted(get_baggage().split(",")) == sorted(expected_baggage.split(","))
Expand Down
6 changes: 4 additions & 2 deletions tests/tracing/test_integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ def test_dynamic_sampling_head_sdk_creates_dsc(
"environment": "production",
"release": "foo",
"sample_rate": str(sample_rate),
"sampled": "true" if transaction.sampled else "false",
"transaction": "Head SDK tx",
"trace_id": trace_id,
}

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

Expand All @@ -188,6 +189,7 @@ def test_dynamic_sampling_head_sdk_creates_dsc(
"environment": "production",
"release": "foo",
"sample_rate": str(sample_rate),
"sampled": "true" if transaction.sampled else "false",
"transaction": "Head SDK tx",
"trace_id": trace_id,
}
Expand Down

0 comments on commit d874091

Please sign in to comment.