Skip to content

Commit 7a9b1b7

Browse files
authored
Do not add trace headers (sentry-trace and baggage) to HTTP requests to Sentry (#2240)
1 parent 994a45b commit 7a9b1b7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,15 @@ def should_propagate_trace(hub, url):
374374
client = hub.client # type: Any
375375
trace_propagation_targets = client.options["trace_propagation_targets"]
376376

377+
if client.transport and client.transport.parsed_dsn:
378+
dsn_url = client.transport.parsed_dsn.netloc
379+
else:
380+
dsn_url = None
381+
382+
is_request_to_sentry = dsn_url and dsn_url in url
383+
if is_request_to_sentry:
384+
return False
385+
377386
return match_regex_list(url, trace_propagation_targets, substring_matching=True)
378387

379388

tests/tracing/test_misc.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from sentry_sdk.consts import MATCH_ALL
99
from sentry_sdk.tracing import Span, Transaction
1010
from sentry_sdk.tracing_utils import should_propagate_trace
11+
from sentry_sdk.utils import Dsn
1112

1213
try:
1314
from unittest import mock # python 3.3 and above
@@ -305,5 +306,50 @@ def test_should_propagate_trace(
305306
hub = MagicMock()
306307
hub.client = MagicMock()
307308
hub.client.options = {"trace_propagation_targets": trace_propagation_targets}
309+
hub.client.transport = MagicMock()
310+
hub.client.transport.parsed_dsn = Dsn("https://bla@xxx.sentry.io/12312012")
308311

309312
assert should_propagate_trace(hub, url) == expected_propagation_decision
313+
314+
315+
@pytest.mark.parametrize(
316+
"dsn,url,expected_propagation_decision",
317+
[
318+
(
319+
"https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012",
320+
"http://example.com",
321+
True,
322+
),
323+
(
324+
"https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012",
325+
"https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012",
326+
False,
327+
),
328+
(
329+
"https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012",
330+
"http://squirrelchasers.ingest.sentry.io/12312012",
331+
False,
332+
),
333+
(
334+
"https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012",
335+
"http://ingest.sentry.io/12312012",
336+
True,
337+
),
338+
(
339+
"https://abc@localsentry.example.com/12312012",
340+
"http://localsentry.example.com",
341+
False,
342+
),
343+
],
344+
)
345+
def test_should_propagate_trace_to_sentry(
346+
sentry_init, dsn, url, expected_propagation_decision
347+
):
348+
sentry_init(
349+
dsn=dsn,
350+
traces_sample_rate=1.0,
351+
)
352+
353+
Hub.current.client.transport.parsed_dsn = Dsn(dsn)
354+
355+
assert should_propagate_trace(Hub.current, url) == expected_propagation_decision

0 commit comments

Comments
 (0)