Skip to content

Commit 1d75da5

Browse files
authored
fix: Fix sample decision propagation via headers (#948)
1 parent c277ed5 commit 1d75da5

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

sentry_sdk/tracing.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -583,22 +583,23 @@ def _set_initial_sampling_decision(self, sampling_context):
583583
decision, `traces_sample_rate` will be used.
584584
"""
585585

586+
# if the user has forced a sampling decision by passing a `sampled`
587+
# value when starting the transaction, go with that
588+
if self.sampled is not None:
589+
return
590+
586591
hub = self.hub or sentry_sdk.Hub.current
587592
client = hub.client
588-
options = (client and client.options) or {}
589593
transaction_description = "{op}transaction <{name}>".format(
590594
op=("<" + self.op + "> " if self.op else ""), name=self.name
591595
)
592596

593-
# nothing to do if there's no client or if tracing is disabled
594-
if not client or not has_tracing_enabled(options):
597+
# nothing to do if there's no client
598+
if not client:
595599
self.sampled = False
596600
return
597601

598-
# if the user has forced a sampling decision by passing a `sampled`
599-
# value when starting the transaction, go with that
600-
if self.sampled is not None:
601-
return
602+
options = client.options
602603

603604
# we would have bailed already if neither `traces_sampler` nor
604605
# `traces_sample_rate` were defined, so one of these should work; prefer
@@ -662,16 +663,6 @@ def _set_initial_sampling_decision(self, sampling_context):
662663
)
663664

664665

665-
def has_tracing_enabled(options):
666-
# type: (Dict[str, Any]) -> bool
667-
"""
668-
Returns True if either traces_sample_rate or traces_sampler is
669-
non-zero/defined, False otherwise.
670-
"""
671-
672-
return bool(options.get("traces_sample_rate") or options.get("traces_sampler"))
673-
674-
675666
def _is_valid_sample_rate(rate):
676667
# type: (Any) -> bool
677668
"""

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_file_text(file_name):
1818
with open(os.path.join(here, file_name)) as in_file:
1919
return in_file.read()
2020

21-
21+
2222
setup(
2323
name="sentry-sdk",
2424
version="0.19.4",
@@ -31,7 +31,7 @@ def get_file_text(file_name):
3131
},
3232
description="Python client for Sentry (https://sentry.io)",
3333
long_description=get_file_text("README.md"),
34-
long_description_content_type='text/markdown',
34+
long_description_content_type="text/markdown",
3535
packages=find_packages(exclude=("tests", "tests.*")),
3636
# PEP 561
3737
package_data={"sentry_sdk": ["py.typed"]},

tests/tracing/test_integration_tests.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ def test_basic(sentry_init, capture_events, sample_rate):
4747

4848

4949
@pytest.mark.parametrize("sampled", [True, False, None])
50-
def test_continue_from_headers(sentry_init, capture_events, sampled):
51-
sentry_init(traces_sample_rate=1.0)
50+
@pytest.mark.parametrize(
51+
"sample_rate", [0.0, 1.0]
52+
) # ensure sampling decision is actually passed along via headers
53+
def test_continue_from_headers(sentry_init, capture_events, sampled, sample_rate):
54+
sentry_init(traces_sample_rate=sample_rate)
5255
events = capture_events()
5356

5457
# make a parent transaction (normally this would be in a different service)
55-
with start_transaction(name="hi"):
58+
with start_transaction(name="hi", sampled=True if sample_rate == 0 else None):
5659
with start_span() as old_span:
5760
old_span.sampled = sampled
5861
headers = dict(Hub.current.iter_trace_propagation_headers())
@@ -84,7 +87,7 @@ def test_continue_from_headers(sentry_init, capture_events, sampled):
8487
scope.transaction = "ho"
8588
capture_message("hello")
8689

87-
if sampled is False:
90+
if sampled is False or (sample_rate == 0 and sampled is None):
8891
trace1, message = events
8992

9093
assert trace1["transaction"] == "hi"

0 commit comments

Comments
 (0)