Skip to content

Commit

Permalink
Moved is_sentry_url to utils (#2304)
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex authored Aug 10, 2023
1 parent 2f14816 commit f1fb5e1
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 46 deletions.
11 changes: 0 additions & 11 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down
4 changes: 0 additions & 4 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion sentry_sdk/integrations/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sentry_sdk.utils import (
SENSITIVE_DATA_SUBSTITUTE,
capture_internal_exceptions,
is_sentry_url,
logger,
safe_repr,
parse_url,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...]]
"""
Expand Down
28 changes: 0 additions & 28 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sentry_sdk.utils import (
Components,
Dsn,
get_error_message,
is_valid_sample_rate,
logger,
Expand All @@ -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:
Expand Down Expand Up @@ -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",
[
Expand Down
2 changes: 1 addition & 1 deletion tests/tracing/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit f1fb5e1

Please sign in to comment.