8000 Moved is_sentry_url to utils (#2304) · crt-fork/sentry-python@f1fb5e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1fb5e1

Browse files
Moved is_sentry_url to utils (getsentry#2304)
1 parent 2f14816 commit f1fb5e1

File tree

8 files changed

+62
-46
lines changed

8 files changed

+62
-46
lines changed

sentry_sdk/client.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -579,17 +579,6 @@ def capture_event(
579579

580580
return event_id
581581

582-
def is_sentry_url(self, url):
583-
# type: (str) -> bool
584-
"""
585-
Determines whether the given URL matches the Sentry DSN.
586-
"""
587-
return (
588-
self.transport is not None
589-
and self.transport.parsed_dsn is not None
590-
and self.transport.parsed_dsn.netloc in url
591-
)
592-
593582
def capture_session(
594583
self, session # type: Session
595584
):

sentry_sdk/hub.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,6 @@ def trace_propagation_meta(self, span=None):
837837

838838
return meta
839839

840-
def is_sentry_url(self, url):
841-
# type: (str) -> bool
842-
return self.client is not None and self.client.is_sentry_url(url)
843-
844840

845841
GLOBAL_HUB = Hub()
846842
_local.set(GLOBAL_HUB)

sentry_sdk/integrations/stdlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from sentry_sdk.utils import (
1212
SENSITIVE_DATA_SUBSTITUTE,
1313
capture_internal_exceptions,
14+
is_sentry_url,
1415
logger,
1516
safe_repr,
1617
parse_url,
@@ -74,7 +75,7 @@ def putrequest(self, method, url, *args, **kwargs):
7475
port = self.port
7576
default_port = self.default_port
7677

77-
if hub.get_integration(StdlibIntegration) is None or hub.is_sentry_url(host):
78+
if hub.get_integration(StdlibIntegration) is None or is_sentry_url(hub, host):
7879
return real_putrequest(self, method, url, *args, **kwargs)
7980

8081
real_url = url

sentry_sdk/tracing_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Dsn,
99
match_regex_list,
1010
to_string,
11+
is_sentry_url,
1112
)
1213
from sentry_sdk._compat import PY2, iteritems
1314
from sentry_sdk._types import TYPE_CHECKING
@@ -377,7 +378,7 @@ def should_propagate_trace(hub, url):
377378
client = hub.client # type: Any
378379
trace_propagation_targets = client.options["trace_propagation_targets"]
379380

380-
if hub.is_sentry_url(url):
381+
if is_sentry_url(hub, url):
381382
return False
382383

383384
return match_regex_list(url, trace_propagation_targets, substring_matching=True)

sentry_sdk/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,19 @@ def match_regex_list(item, regex_list=None, substring_matching=False):
14981498
return False
14991499

15001500

1501+
def is_sentry_url(hub, url):
1502+
# type: (sentry_sdk.Hub, str) -> bool
1503+
"""
1504+
Determines whether the given URL matches the Sentry DSN.
1505+
"""
1506+
return (
1507+
hub.client is not None
1508+
and hub.client.transport is not None
1509+
and hub.client.transport.parsed_dsn is not None
1510+
and hub.client.transport.parsed_dsn.netloc in url
1511+
)
1512+
1513+
15011514
def parse_version(version):
15021515
# type: (str) -> Optional[Tuple[int, ...]]
15031516
"""

tests/test_client.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,31 +1136,3 @@ def test_max_value_length_option(
11361136
capture_message("a" * 2000)
11371137

11381138
assert len(events[0]["message"]) == expected_data_length
1139-
1140-
1141-
def test_is_sentry_url_true():
1142-
client = Client(dsn="https://asdf@abcd1234.ingest.sentry.io/123456789")
1143-
test_url = "abcd1234.ingest.sentry.io"
1144-
1145-
is_sentry_url = client.is_sentry_url(test_url)
1146-
1147-
assert is_sentry_url
1148-
1149-
1150-
def test_is_sentry_url_false():
1151-
client = Client(dsn="https://asdf@abcd1234.ingest.sentry.io/123456789")
1152-
test_url = "abcd1234.mywebsite.com"
1153-
1154-
is_sentry_url = client.is_sentry_url(test_url)
1155-
1156-
assert not is_sentry_url
1157-
1158-
1159-
def test_is_sentry_url_no_transport():
1160-
client = Client()
1161-
client.transport = None
1162-
test_url = "abcd1234.mywebsite.com"
1163-
1164-
is_sentry_url = client.is_sentry_url(test_url)
1165-
1166-
assert not is_sentry_url

tests/test_utils.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from sentry_sdk.utils import (
66
Components,
7+
Dsn,
78
get_error_message,
89
is_valid_sample_rate,
910
logger,
@@ -13,8 +14,11 @@
1314
safe_str,
1415
sanitize_url,
1516
serialize_frame,
17+
is_sentry_url,
1618
)
1719

20+
import sentry_sdk
21+
1822
try:
1923
from unittest import mock # python 3.3 and above
2024
except ImportError:
@@ -427,6 +431,46 @@ def test_parse_version(version, expected_result):
427431
assert parse_version(version) == expected_result
428432

429433

434+
@pytest.fixture
435+
def mock_hub_with_dsn_netloc():
436+
"""
437+
Returns a mocked hub with a DSN netloc of "abcd1234.ingest.sentry.io".
438+
"""
439+
440+
mock_hub = mock.Mock(spec=sentry_sdk.Hub)
441+
mock_hub.client = mock.Mock(spec=sentry_sdk.Client)
442+
mock_hub.client.transport = mock.Mock(spec=sentry_sdk.Transport)
443+
mock_hub.client.transport.parsed_dsn = mock.Mock(spec=Dsn)
444+
445+
mock_hub.client.transport.parsed_dsn.netloc = "abcd1234.ingest.sentry.io"
446+
447+
return mock_hub
448+
449+
450+
@pytest.mark.parametrize(
451+
["test_url", "is_sentry_url_expected"],
452+
[
453+
["https://asdf@abcd1234.ingest.sentry.io/123456789", True],
454+
["https://asdf@abcd1234.ingest.notsentry.io/123456789", False],
455+
],
456+
)
457+
def test_is_sentry_url_true(test_url, is_sentry_url_expected, mock_hub_with_dsn_netloc):
458+
ret_val = is_sentry_url(mock_hub_with_dsn_netloc, test_url)
459+
460+
assert ret_val == is_sentry_url_expected
461+
462+
463+
def test_is_sentry_url_no_client():
464+
hub = mock.Mock()
465+
hub.client = None
466+
467+
test_url = "https://asdf@abcd1234.ingest.sentry.io/123456789"
468+
469+
ret_val = is_sentry_url(hub, test_url)
470+
471+
assert not ret_val
472+
473+
430474
@pytest.mark.parametrize(
431475
"error,expected_result",
432476
[

tests/tracing/test_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def test_should_propagate_trace(
306306
hub = MagicMock()
307307
hub.client = MagicMock()
308308

309-
# This test assumes the urls are not Sentry URLs. Use test_should_propogate_trace_to_sentry for sentry URLs.
309+
# This test assumes the urls are not Sentry URLs. Use test_should_propagate_trace_to_sentry for sentry URLs.
310310
hub.is_sentry_url = lambda _: False
311311

312312
hub.client.options = {"trace_propagation_targets": trace_propagation_targets}

0 commit comments

Comments
 (0)
0