-
Notifications
You must be signed in to change notification settings - Fork 571
Port sample_rate update to potel-base
#4069
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3061f57
Port traces_sample_rate update to potel-base
sentrivana 9968358
Merge branch 'potel-base' into ivana/potel/port-sample-rate-update
sentrivana d2351e7
Added one more test
antonpirker 73c0543
Merge branch 'ivana/potel/port-sample-rate-update' of github.com:gets…
antonpirker d6fae76
update test
sentrivana 7649f34
Test case to try to override incoming with traces_sample_rate
antonpirker 7f95458
Merge branch 'ivana/potel/port-sample-rate-update' of github.com:gets…
antonpirker 1a09fc5
updated test
antonpirker 0064118
Updated test
antonpirker 0f56682
In case the sentry-trace says we defer the sampling descission, overr…
antonpirker 56a9eae
simplify
sentrivana f60670c
fix test
sentrivana e434be1
cleanup
antonpirker eb93d9c
Merge branch 'ivana/potel/port-sample-rate-update' of github.com:gets…
antonpirker 01acd94
...testing ci
sentrivana 62f3122
try with monkeypatch
sentrivana d177d6e
Merge branch 'potel-base' into ivana/potel/port-sample-rate-update
sentrivana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| This is not tested in this file. | ||
| """ | ||
|
|
||
| import random | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
@@ -117,7 +118,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 | ||
|
|
@@ -137,7 +138,7 @@ def test_dsc_continuation_of_trace(sentry_init, capture_envelopes): | |
|
|
||
|
|
||
| def test_dsc_continuation_of_trace_sample_rate_changed_in_traces_sampler( | ||
| sentry_init, capture_envelopes | ||
| sentry_init, capture_envelopes, monkeypatch | ||
| ): | ||
| """ | ||
| Another service calls our service and passes tracing information to us. | ||
|
|
@@ -175,10 +176,10 @@ def my_traces_sampler(sampling_context): | |
| } | ||
|
|
||
| # We continue the incoming trace and start a new transaction | ||
| with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.125): | ||
| with sentry_sdk.continue_trace(incoming_http_headers): | ||
| with sentry_sdk.start_span(name="foo"): | ||
| pass | ||
| monkeypatch.setattr(random, "random", lambda: 0.125) | ||
| with sentry_sdk.continue_trace(incoming_http_headers): | ||
| with sentry_sdk.start_span(name="foo"): | ||
| pass | ||
|
|
||
| assert len(envelopes) == 1 | ||
|
|
||
|
|
@@ -214,6 +215,212 @@ def my_traces_sampler(sampling_context): | |
| assert envelope_trace_header["transaction"] == "bar" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "test_data, expected_sample_rate, expected_sampled", | ||
| [ | ||
| # Test data: | ||
| # "incoming_sample_rate": | ||
| # The "sentry-sample_rate" in the incoming `baggage` header. | ||
| # "incoming_sampled": | ||
| # The "sentry-sampled" in the incoming `baggage` header. | ||
| # "sentry_trace_header_parent_sampled": | ||
| # The number at the end in the `sentry-trace` header, called "parent_sampled". | ||
| # "use_local_traces_sampler": | ||
| # Whether the local traces sampler is used. | ||
| # "local_traces_sampler_result": | ||
| # The result of the local traces sampler. | ||
| # "local_traces_sample_rate": | ||
| # The `traces_sample_rate` setting in the local `sentry_init` call. | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "true", | ||
| "sentry_trace_header_parent_sampled": 1, | ||
| "use_local_traces_sampler": False, | ||
| "local_traces_sampler_result": None, | ||
| "local_traces_sample_rate": 0.7, | ||
| }, | ||
| 1.0, # expected_sample_rate | ||
| "true", # expected_sampled | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "true", | ||
| "sentry_trace_header_parent_sampled": 1, | ||
| "use_local_traces_sampler": True, | ||
| "local_traces_sampler_result": 0.5, | ||
| "local_traces_sample_rate": 0.7, | ||
| }, | ||
| 0.5, # expected_sample_rate | ||
| "true", # expected_sampled | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "false", | ||
| "sentry_trace_header_parent_sampled": 0, | ||
| "use_local_traces_sampler": False, | ||
| "local_traces_sampler_result": None, | ||
| "local_traces_sample_rate": 0.7, | ||
| }, | ||
| None, # expected_sample_rate | ||
| "tracing-disabled-no-transactions-should-be-sent", # expected_sampled (because the parent sampled is 0) | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "false", | ||
| "sentry_trace_header_parent_sampled": 0, | ||
| "use_local_traces_sampler": True, | ||
| "local_traces_sampler_result": 0.5, | ||
| "local_traces_sample_rate": 0.7, | ||
| }, | ||
| 0.5, # expected_sample_rate | ||
| "false", # expected_sampled (traces sampler can override parent sampled) | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "true", | ||
| "sentry_trace_header_parent_sampled": 1, | ||
| "use_local_traces_sampler": False, | ||
| "local_traces_sampler_result": None, | ||
| "local_traces_sample_rate": None, | ||
| }, | ||
| None, # expected_sample_rate | ||
| "tracing-disabled-no-transactions-should-be-sent", # expected_sampled (traces_sample_rate=None disables all transaction creation) | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "true", | ||
| "sentry_trace_header_parent_sampled": 1, | ||
| "use_local_traces_sampler": True, | ||
| "local_traces_sampler_result": 0.5, | ||
| "local_traces_sample_rate": None, | ||
| }, | ||
| 0.5, # expected_sample_rate | ||
| "true", # expected_sampled (traces sampler overrides the traces_sample_rate setting, so transactions are created) | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "false", | ||
| "sentry_trace_header_parent_sampled": 0, | ||
| "use_local_traces_sampler": False, | ||
| "local_traces_sampler_result": None, | ||
| "local_traces_sample_rate": None, | ||
| }, | ||
| None, # expected_sample_rate | ||
| "tracing-disabled-no-transactions-should-be-sent", # expected_sampled (traces_sample_rate=None disables all transaction creation) | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": "false", | ||
| "sentry_trace_header_parent_sampled": 0, | ||
| "use_local_traces_sampler": True, | ||
| "local_traces_sampler_result": 0.5, | ||
| "local_traces_sample_rate": None, | ||
| }, | ||
| 0.5, # expected_sample_rate | ||
| "false", # expected_sampled | ||
| ), | ||
| ( | ||
| { | ||
| "incoming_sample_rate": 1.0, | ||
| "incoming_sampled": None, | ||
| "sentry_trace_header_parent_sampled": None, | ||
| "use_local_traces_sampler": False, | ||
| "local_traces_sampler_result": 0.5, | ||
| "local_traces_sample_rate": 0.7, | ||
| }, | ||
| 0.7, # expected_sample_rate | ||
| "true", # expected_sampled | ||
| ), | ||
| ], | ||
| ids=( | ||
| "1 traces_sample_rate does not override incoming", | ||
| "2 traces_sampler overrides incoming", | ||
| "3 traces_sample_rate does not overrides incoming sample rate or parent (incoming not sampled)", | ||
| "4 traces_sampler overrides incoming (incoming not sampled)", | ||
| "5 forwarding incoming (traces_sample_rate not set)", | ||
| "6 traces_sampler overrides incoming (traces_sample_rate not set)", | ||
| "7 forwarding incoming (traces_sample_rate not set) (incoming not sampled)", | ||
| "8 traces_sampler overrides incoming (traces_sample_rate not set) (incoming not sampled)", | ||
| "9 traces_sample_rate overrides incoming (upstream deferred sampling decision)", | ||
| ), | ||
| ) | ||
| def test_dsc_sample_rate_change( | ||
| sentry_init, | ||
| capture_envelopes, | ||
| test_data, | ||
| expected_sample_rate, | ||
| expected_sampled, | ||
| ): | ||
| """ | ||
| Another service calls our service and passes tracing information to us. | ||
| Our service is continuing the trace, but modifies the sample rate. | ||
| The DSC in transaction envelopes should contain the updated sample rate. | ||
| """ | ||
|
|
||
| def my_traces_sampler(sampling_context): | ||
| return test_data["local_traces_sampler_result"] | ||
|
|
||
| init_kwargs = { | ||
| "dsn": "https://[email protected]/12312012", | ||
| "release": "[email protected]", | ||
| "environment": "canary", | ||
| } | ||
|
|
||
| if test_data["local_traces_sample_rate"]: | ||
| init_kwargs["traces_sample_rate"] = test_data["local_traces_sample_rate"] | ||
|
|
||
| if test_data["use_local_traces_sampler"]: | ||
| init_kwargs["traces_sampler"] = my_traces_sampler | ||
|
|
||
| sentry_init(**init_kwargs) | ||
| envelopes = capture_envelopes() | ||
|
|
||
| # This is what the upstream service sends us | ||
| incoming_trace_id = "771a43a4192642f0b136d5159a501700" | ||
| if test_data["sentry_trace_header_parent_sampled"] is None: | ||
| sentry_trace = f"{incoming_trace_id}-1234567890abcdef" | ||
| else: | ||
| sentry_trace = f"{incoming_trace_id}-1234567890abcdef-{test_data['sentry_trace_header_parent_sampled']}" | ||
|
|
||
| baggage = ( | ||
| f"sentry-trace_id={incoming_trace_id}, " | ||
| f"sentry-sample_rate={str(test_data['incoming_sample_rate'])}, " | ||
| f"sentry-sampled={test_data['incoming_sampled']}, " | ||
| "sentry-public_key=frontendpublickey, " | ||
| "[email protected], " | ||
| "sentry-environment=prod, " | ||
| "sentry-transaction=foo, " | ||
| ) | ||
| incoming_http_headers = { | ||
| "HTTP_SENTRY_TRACE": sentry_trace, | ||
| "HTTP_BAGGAGE": baggage, | ||
| } | ||
|
|
||
| # We continue the incoming trace and start a new transaction | ||
| with mock.patch.object(random, "random", return_value=0.2): | ||
| with sentry_sdk.continue_trace(incoming_http_headers): | ||
| with sentry_sdk.start_span(name="foo"): | ||
| pass | ||
|
|
||
| if expected_sampled == "tracing-disabled-no-transactions-should-be-sent": | ||
| assert len(envelopes) == 0 | ||
| else: | ||
| transaction_envelope = envelopes[0] | ||
| dsc_in_envelope_header = transaction_envelope.headers["trace"] | ||
|
|
||
| assert dsc_in_envelope_header["sample_rate"] == str(expected_sample_rate) | ||
| assert dsc_in_envelope_header["sampled"] == str(expected_sampled).lower() | ||
| assert dsc_in_envelope_header["trace_id"] == incoming_trace_id | ||
|
|
||
|
|
||
| def test_dsc_issue(sentry_init, capture_envelopes): | ||
| """ | ||
| Our service is a standalone service that does not have tracing enabled. Just uses Sentry for error reporting. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this change, to make sure we set sentry-sampled to deferred in any case. If there is already a entry sentry-sampled in the trace_state then .add() will not do anything.
(There can be a sentry-sampled in the trace_state if the upstream SDK sends the
sentry-sampledDSC entry with thebaggagehttp request. If there was no upstream sampling descision, this entry should either not be sent, of if sent than asNone.)This change works in both cases.