diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 110a6952db..762dca1723 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -374,6 +374,15 @@ 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: + return False + return match_regex_list(url, trace_propagation_targets, substring_matching=True) diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index 0c9d114793..49b1f53015 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -8,6 +8,7 @@ from sentry_sdk.consts import MATCH_ALL from sentry_sdk.tracing import Span, Transaction from sentry_sdk.tracing_utils import should_propagate_trace +from sentry_sdk.utils import Dsn try: from unittest import mock # python 3.3 and above @@ -305,5 +306,50 @@ def test_should_propagate_trace( hub = MagicMock() hub.client = MagicMock() hub.client.options = {"trace_propagation_targets": trace_propagation_targets} + hub.client.transport = MagicMock() + hub.client.transport.parsed_dsn = Dsn("https://bla@xxx.sentry.io/12312012") assert should_propagate_trace(hub, url) == expected_propagation_decision + + +@pytest.mark.parametrize( + "dsn,url,expected_propagation_decision", + [ + ( + "https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012", + "http://example.com", + True, + ), + ( + "https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012", + "https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012", + False, + ), + ( + "https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012", + "http://squirrelchasers.ingest.sentry.io/12312012", + False, + ), + ( + "https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012", + "http://ingest.sentry.io/12312012", + True, + ), + ( + "https://abc@localsentry.example.com/12312012", + "http://localsentry.example.com", + False, + ), + ], +) +def test_should_propagate_trace_to_sentry( + sentry_init, dsn, url, expected_propagation_decision +): + sentry_init( + dsn=dsn, + traces_sample_rate=1.0, + ) + + Hub.current.client.transport.parsed_dsn = Dsn(dsn) + + assert should_propagate_trace(Hub.current, url) == expected_propagation_decision