Skip to content

feat(tracing): Use sample_rand for sampling decisions #4044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

szokeasaurusrex
Copy link
Member

@szokeasaurusrex szokeasaurusrex commented Feb 12, 2025

Use the sample_rand value from an incoming trace to make sampling decisions, rather than generating a random value. When we are the head SDK starting a new trace, save our randomly-generated value as the sample_rand, and also change the random generation logic so that the sample_rand is computed deterministically based on the trace_id.

Depends on:

Closes #3998


Thank you for contributing to sentry-python! Please add tests to validate your changes, and lint your code using tox -e linters.

Running the test suite on your PR might require maintainer approval. The AWS Lambda tests additionally require a maintainer to add a special label, and they will fail until this label is added.

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.59%. Comparing base (61ae6e7) to head (0d49ac2).
Report is 1 commits behind head on szokeasaurusrex/sample_rand-3.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@                        Coverage Diff                        @@
##           szokeasaurusrex/sample_rand-3    #4044      +/-   ##
=================================================================
+ Coverage                          79.55%   79.59%   +0.03%     
=================================================================
  Files                                140      140              
  Lines                              15570    15579       +9     
  Branches                            2641     2643       +2     
=================================================================
+ Hits                               12387    12400      +13     
+ Misses                              2343     2341       -2     
+ Partials                             840      838       -2     
Files with missing lines Coverage Δ
sentry_sdk/tracing.py 77.73% <100.00%> (+0.13%) ⬆️
sentry_sdk/tracing_utils.py 85.50% <100.00%> (+0.21%) ⬆️

... and 3 files with indirect coverage changes

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from c3d6d12 to 5a906f6 Compare February 12, 2025 11:42
@szokeasaurusrex szokeasaurusrex marked this pull request as draft February 12, 2025 11:42
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch from af64b16 to 01334d7 Compare February 12, 2025 11:48
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from 5a906f6 to 548396d Compare February 12, 2025 11:51
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch from 01334d7 to 7ec8e08 Compare February 12, 2025 11:57
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch 4 times, most recently from c4abdc9 to 5f57723 Compare February 12, 2025 13:27
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review February 12, 2025 13:36
@szokeasaurusrex szokeasaurusrex linked an issue Feb 12, 2025 that may be closed by this pull request
Comment on lines 806 to 810
if baggage_sample_rand is not None:
self._sample_rand = baggage_sample_rand
else:
self._sample_rand = Random(self.trace_id).random()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases will we have to generate a new _sample_rand here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever we are starting a new trace, i.e. if we call start_transaction without calling continue_trace first and without manually passing in baggage

Copy link
Member

@sl0thentr0py sl0thentr0py left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just comments on the tests, testing exact float values like that can be non-deterministic so maybe better to just mock out the rng


TEST_TRACE_ID_SAMPLE_RANDS = {
"00000000000000000000000000000000": 0.8766381713144122,
"01234567012345670123456701234567": 0.6451742521664413,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not sure if rng is consistent like that across python versions and so on?

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from dc4c9aa to ffd3ead Compare February 24, 2025 15:58
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch from cf51e51 to 0db414f Compare February 24, 2025 15:58
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from ffd3ead to 2186af6 Compare February 24, 2025 16:04
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch from 0db414f to 620b617 Compare February 24, 2025 16:04
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from 2186af6 to 7d8d1f1 Compare February 24, 2025 16:20
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch 2 times, most recently from c8820e2 to 64b4d84 Compare February 25, 2025 10:44
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from 7d8d1f1 to a2852ba Compare February 25, 2025 10:45
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from a2852ba to 30769fe Compare February 25, 2025 10:50
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch from 64b4d84 to 6e56143 Compare February 25, 2025 10:50
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok -- please see comments

@@ -636,7 +636,7 @@ 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,sentry-sampled=true"
== "custom=value,sentry-trace_id=0123456789012345678901234567890,sentry-sample_rand=0.3015579701611357,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't sample_rand have 6 digits after the decimal point?

import sentry_sdk
from sentry_sdk.tracing_utils import Baggage

TEST_TRACE_ID_SAMPLE_RANDS = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These too -- aren't they supposed to have 6 decimal places?

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from 30769fe to 20bfe5a Compare February 25, 2025 14:48
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch 2 times, most recently from 11cfacd to f77a9ec Compare February 25, 2025 14:50
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch 2 times, most recently from ad9d103 to 8d4d858 Compare February 25, 2025 14:53
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-3 branch from f77a9ec to 61ae6e7 Compare February 25, 2025 14:53
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch 4 times, most recently from 74e7c41 to 5d65dc0 Compare February 25, 2025 19:13
Use the `sample_rand` value from an incoming trace to make sampling decisions, rather than generating a random value. When we are the head SDK starting a new trace, save our randomly-generated value as the `sample_rand`, and also change the random generation logic so that the `sample_rand` is computed deterministically based on the `trace_id`.

Closes #3998
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/sample_rand-4 branch from 5d65dc0 to 0d49ac2 Compare February 25, 2025 19:19
@sentrivana sentrivana merged commit ccb969e into szokeasaurusrex/sample_rand-3 Feb 26, 2025
141 checks passed
@sentrivana sentrivana deleted the szokeasaurusrex/sample_rand-4 branch February 26, 2025 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Sampling Seed Propagation
3 participants