Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,17 @@ 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: 4 additions & 0 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,10 @@ 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)
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ def _install_httplib():
def putrequest(self, method, url, *args, **kwargs):
# type: (HTTPConnection, str, str, *Any, **Any) -> Any
hub = Hub.current
if hub.get_integration(StdlibIntegration) is None:
return real_putrequest(self, method, url, *args, **kwargs)

host = self.host
port = self.port
default_port = self.default_port

if hub.get_integration(StdlibIntegration) is None or hub.is_sentry_url(host):
return real_putrequest(self, method, url, *args, **kwargs)

real_url = url
if real_url is None or not real_url.startswith(("http://", "https://")):
real_url = "%s://%s%s%s" % (
Expand Down
8 changes: 1 addition & 7 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,7 @@ def should_propagate_trace(hub, url):
client = hub.client # type: Any
trace_propagation_targets = client.options["trace_propagation_targets"]

if client.transport and client.transport.parsed_dsn:
dsn_url = client.transport.parsed_dsn.netloc
else:
dsn_url = None

is_request_to_sentry = dsn_url and dsn_url in url
if is_request_to_sentry:
if hub.is_sentry_url(url):
return False

return match_regex_list(url, trace_propagation_targets, substring_matching=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/stdlib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def before_breadcrumb(crumb, hint):
}


def test_empty_realurl(sentry_init, capture_events):
def test_empty_realurl(sentry_init):
"""
Ensure that after using sentry_sdk.init you can putrequest a
None url.
Expand Down
28 changes: 28 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,3 +1136,31 @@ 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://[email protected]/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://[email protected]/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
4 changes: 4 additions & 0 deletions tests/tracing/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ 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.
hub.is_sentry_url = lambda _: False

hub.client.options = {"trace_propagation_targets": trace_propagation_targets}
hub.client.transport = MagicMock()
hub.client.transport.parsed_dsn = Dsn("https://[email protected]/12312012")
Expand Down