Skip to content

Commit 7605898

Browse files
test: Add test for custom sample rates
1 parent 2fe7a0c commit 7605898

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

tests/sentry/ingest/ingest_consumer/test_ingest_consumer_processing.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from __future__ import annotations
22

33
import datetime
4+
import os
45
import time
56
import uuid
67
import zipfile
78
from io import BytesIO
89
from typing import Any
910
from unittest.mock import Mock, patch
1011

12+
import django.test
1113
import orjson
1214
import pytest
1315
from arroyo.backends.kafka.consumer import KafkaPayload
@@ -24,6 +26,7 @@
2426
process_event,
2527
process_individual_attachment,
2628
process_userreport,
29+
trace_func,
2730
)
2831
from sentry.ingest.types import ConsumerType
2932
from sentry.models.debugfile import create_files_from_dif_zip
@@ -621,3 +624,58 @@ def test_collect_span_metrics(default_project):
621624
assert mock_metrics.incr.call_count == 0
622625
collect_span_metrics(default_project, {"spans": [1, 2, 3]})
623626
assert mock_metrics.incr.call_count == 1
627+
628+
629+
@pytest.mark.parametrize(
630+
"env_value,settings_value,expected_sample_rate",
631+
[
632+
# Both unset - should use default of 0
633+
(None, None, 0.0),
634+
# Only environment variable set
635+
("0", None, 0.0),
636+
("1", None, 1.0),
637+
("0.5", None, 0.5),
638+
# Only settings value set
639+
(None, 0, 0.0),
640+
(None, 1, 1.0),
641+
(None, 0.7, 0.7),
642+
# Both set - environment variable should take precedence
643+
("0", 1, 0.0), # env=0, settings=1 -> should use env (0)
644+
("1", 0, 1.0), # env=1, settings=0 -> should use env (1)
645+
("0.3", 0.8, 0.3), # env=0.3, settings=0.8 -> should use env (0.3)
646+
],
647+
)
648+
def test_sample_rate_passed(env_value, settings_value, expected_sample_rate):
649+
# Test various combinations of environment variable and settings values
650+
651+
# Prepare environment
652+
env_dict = {}
653+
if env_value is not None:
654+
env_dict["SENTRY_INGEST_CONSUMER_APM_SAMPLING"] = env_value
655+
656+
with patch.dict(os.environ, env_dict, clear=True):
657+
with django.test.override_settings(SENTRY_INGEST_CONSUMER_APM_SAMPLING=settings_value):
658+
# If settings_value is None, delete the setting to simulate it not being set
659+
if settings_value is None:
660+
del settings.SENTRY_INGEST_CONSUMER_APM_SAMPLING
661+
662+
with patch(
663+
"sentry.ingest.consumer.processors.sentry_sdk.start_span"
664+
) as mock_start_span:
665+
# Create a dummy function to decorate
666+
@trace_func(name="test_span")
667+
def dummy_function():
668+
return "test_result"
669+
670+
# Call the decorated function
671+
result = dummy_function()
672+
673+
# Verify the function returned correctly
674+
assert result == "test_result"
675+
676+
# Verify start_span was called with correct arguments
677+
mock_start_span.assert_called_once()
678+
call_args = mock_start_span.call_args
679+
680+
# Check that the span_kwargs include the expected sample_rate
681+
assert call_args.kwargs["attributes"]["sample_rate"] == expected_sample_rate

tests/sentry/utils/test_sdk.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
check_current_scope_transaction,
2020
check_tag_for_scope_bleed,
2121
merge_context_into_scope,
22+
traces_sampler,
2223
)
2324

2425

@@ -64,6 +65,18 @@ def test_context_scope_merge_with_existing_context(self):
6465
"charlie": "goofy",
6566
}
6667

68+
def test_traces_sampler_custom_sample_rate_0_0(self):
69+
sampling_context = {"sample_rate": 0.0}
70+
assert traces_sampler(sampling_context) == 0.0
71+
72+
def test_traces_sampler_custom_sample_rate_0_5(self):
73+
sampling_context = {"sample_rate": 0.5}
74+
assert traces_sampler(sampling_context) == 0.5
75+
76+
def test_traces_sampler_custom_sample_rate_1_0(self):
77+
sampling_context = {"sample_rate": 1.0}
78+
assert traces_sampler(sampling_context) == 1.0
79+
6780

6881
@patch("sentry.utils.sdk.logger.warning")
6982
class CheckTagForScopeBleedTest(TestCase):

0 commit comments

Comments
 (0)