diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index d7525ca242..75e44dd206 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -579,17 +579,6 @@ def capture_event( return event_id - def is_sentry_url(self, url): - # type: (str) -> bool - """ - Determines whether the given URL matches the Sentry DSN. - """ - return ( - self.transport is not None - and self.transport.parsed_dsn is not None - and self.transport.parsed_dsn.netloc in url - ) - def capture_session( self, session # type: Session ): diff --git a/sentry_sdk/hub.py b/sentry_sdk/hub.py index 7078463806..ac77fb42fc 100644 --- a/sentry_sdk/hub.py +++ b/sentry_sdk/hub.py @@ -837,10 +837,6 @@ def trace_propagation_meta(self, span=None): return meta - def is_sentry_url(self, url): - # type: (str) -> bool - return self.client is not None and self.client.is_sentry_url(url) - GLOBAL_HUB = Hub() _local.set(GLOBAL_HUB) diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index f6db43c54c..a5c3bfb2ae 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -11,6 +11,7 @@ from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, capture_internal_exceptions, + is_sentry_url, logger, safe_repr, parse_url, @@ -74,7 +75,7 @@ def putrequest(self, method, url, *args, **kwargs): port = self.port default_port = self.default_port - if hub.get_integration(StdlibIntegration) is None or hub.is_sentry_url(host): + if hub.get_integration(StdlibIntegration) is None or is_sentry_url(hub, host): return real_putrequest(self, method, url, *args, **kwargs) real_url = url diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 9906f18bfa..fca416028b 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -8,6 +8,7 @@ Dsn, match_regex_list, to_string, + is_sentry_url, ) from sentry_sdk._compat import PY2, iteritems from sentry_sdk._types import TYPE_CHECKING @@ -377,7 +378,7 @@ def should_propagate_trace(hub, url): client = hub.client # type: Any trace_propagation_targets = client.options["trace_propagation_targets"] - if hub.is_sentry_url(url): + if is_sentry_url(hub, url): return False return match_regex_list(url, trace_propagation_targets, substring_matching=True) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index e5bc4e4df3..480c55c647 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1498,6 +1498,19 @@ def match_regex_list(item, regex_list=None, substring_matching=False): return False +def is_sentry_url(hub, url): + # type: (sentry_sdk.Hub, str) -> bool + """ + Determines whether the given URL matches the Sentry DSN. + """ + return ( + hub.client is not None + and hub.client.transport is not None + and hub.client.transport.parsed_dsn is not None + and hub.client.transport.parsed_dsn.netloc in url + ) + + def parse_version(version): # type: (str) -> Optional[Tuple[int, ...]] """ diff --git a/tests/test_client.py b/tests/test_client.py index 3213da6911..83257ab213 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1136,31 +1136,3 @@ def test_max_value_length_option( capture_message("a" * 2000) assert len(events[0]["message"]) == expected_data_length - - -def test_is_sentry_url_true(): - client = Client(dsn="https://asdf@abcd1234.ingest.sentry.io/123456789") - test_url = "abcd1234.ingest.sentry.io" - - is_sentry_url = client.is_sentry_url(test_url) - - assert is_sentry_url - - -def test_is_sentry_url_false(): - client = Client(dsn="https://asdf@abcd1234.ingest.sentry.io/123456789") - test_url = "abcd1234.mywebsite.com" - - is_sentry_url = client.is_sentry_url(test_url) - - assert not is_sentry_url - - -def test_is_sentry_url_no_transport(): - client = Client() - client.transport = None - test_url = "abcd1234.mywebsite.com" - - is_sentry_url = client.is_sentry_url(test_url) - - assert not is_sentry_url diff --git a/tests/test_utils.py b/tests/test_utils.py index 1ce33c2223..ee73433dd5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,6 +4,7 @@ from sentry_sdk.utils import ( Components, + Dsn, get_error_message, is_valid_sample_rate, logger, @@ -13,8 +14,11 @@ safe_str, sanitize_url, serialize_frame, + is_sentry_url, ) +import sentry_sdk + try: from unittest import mock # python 3.3 and above except ImportError: @@ -427,6 +431,46 @@ def test_parse_version(version, expected_result): assert parse_version(version) == expected_result +@pytest.fixture +def mock_hub_with_dsn_netloc(): + """ + Returns a mocked hub with a DSN netloc of "abcd1234.ingest.sentry.io". + """ + + mock_hub = mock.Mock(spec=sentry_sdk.Hub) + mock_hub.client = mock.Mock(spec=sentry_sdk.Client) + mock_hub.client.transport = mock.Mock(spec=sentry_sdk.Transport) + mock_hub.client.transport.parsed_dsn = mock.Mock(spec=Dsn) + + mock_hub.client.transport.parsed_dsn.netloc = "abcd1234.ingest.sentry.io" + + return mock_hub + + +@pytest.mark.parametrize( + ["test_url", "is_sentry_url_expected"], + [ + ["https://asdf@abcd1234.ingest.sentry.io/123456789", True], + ["https://asdf@abcd1234.ingest.notsentry.io/123456789", False], + ], +) +def test_is_sentry_url_true(test_url, is_sentry_url_expected, mock_hub_with_dsn_netloc): + ret_val = is_sentry_url(mock_hub_with_dsn_netloc, test_url) + + assert ret_val == is_sentry_url_expected + + +def test_is_sentry_url_no_client(): + hub = mock.Mock() + hub.client = None + + test_url = "https://asdf@abcd1234.ingest.sentry.io/123456789" + + ret_val = is_sentry_url(hub, test_url) + + assert not ret_val + + @pytest.mark.parametrize( "error,expected_result", [ diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index c17110b11e..01bf1c1b07 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -306,7 +306,7 @@ def test_should_propagate_trace( hub = MagicMock() hub.client = MagicMock() - # This test assumes the urls are not Sentry URLs. Use test_should_propogate_trace_to_sentry for sentry URLs. + # This test assumes the urls are not Sentry URLs. Use test_should_propagate_trace_to_sentry for sentry URLs. hub.is_sentry_url = lambda _: False hub.client.options = {"trace_propagation_targets": trace_propagation_targets}