|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import datetime
|
| 4 | +import os |
4 | 5 | import time
|
5 | 6 | import uuid
|
6 | 7 | import zipfile
|
7 | 8 | from io import BytesIO
|
8 | 9 | from typing import Any
|
9 | 10 | from unittest.mock import Mock, patch
|
10 | 11 |
|
| 12 | +import django.test |
11 | 13 | import orjson
|
12 | 14 | import pytest
|
13 | 15 | from arroyo.backends.kafka.consumer import KafkaPayload
|
|
24 | 26 | process_event,
|
25 | 27 | process_individual_attachment,
|
26 | 28 | process_userreport,
|
| 29 | + trace_func, |
27 | 30 | )
|
28 | 31 | from sentry.ingest.types import ConsumerType
|
29 | 32 | from sentry.models.debugfile import create_files_from_dif_zip
|
@@ -621,3 +624,58 @@ def test_collect_span_metrics(default_project):
|
621 | 624 | assert mock_metrics.incr.call_count == 0
|
622 | 625 | collect_span_metrics(default_project, {"spans": [1, 2, 3]})
|
623 | 626 | 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 |
0 commit comments