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
25 changes: 8 additions & 17 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,22 +583,23 @@ def _set_initial_sampling_decision(self, sampling_context):
decision, `traces_sample_rate` will be used.
"""

# if the user has forced a sampling decision by passing a `sampled`
# value when starting the transaction, go with that
if self.sampled is not None:
return

hub = self.hub or sentry_sdk.Hub.current
client = hub.client
options = (client and client.options) or {}
transaction_description = "{op}transaction <{name}>".format(
op=("<" + self.op + "> " if self.op else ""), name=self.name
)

# nothing to do if there's no client or if tracing is disabled
if not client or not has_tracing_enabled(options):
# nothing to do if there's no client
if not client:
self.sampled = False
return

# if the user has forced a sampling decision by passing a `sampled`
# value when starting the transaction, go with that
if self.sampled is not None:
return
options = client.options

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


def has_tracing_enabled(options):
# type: (Dict[str, Any]) -> bool
"""
Returns True if either traces_sample_rate or traces_sampler is
non-zero/defined, False otherwise.
"""

return bool(options.get("traces_sample_rate") or options.get("traces_sampler"))


def _is_valid_sample_rate(rate):
# type: (Any) -> bool
"""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_file_text(file_name):
with open(os.path.join(here, file_name)) as in_file:
return in_file.read()


setup(
name="sentry-sdk",
version="0.19.4",
Expand All @@ -31,7 +31,7 @@ def get_file_text(file_name):
},
description="Python client for Sentry (https://sentry.io)",
long_description=get_file_text("README.md"),
long_description_content_type='text/markdown',
long_description_content_type="text/markdown",
packages=find_packages(exclude=("tests", "tests.*")),
# PEP 561
package_data={"sentry_sdk": ["py.typed"]},
Expand Down
11 changes: 7 additions & 4 deletions tests/tracing/test_integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ def test_basic(sentry_init, capture_events, sample_rate):


@pytest.mark.parametrize("sampled", [True, False, None])
def test_continue_from_headers(sentry_init, capture_events, sampled):
sentry_init(traces_sample_rate=1.0)
@pytest.mark.parametrize(
"sample_rate", [0.0, 1.0]
) # ensure sampling decision is actually passed along via headers
def test_continue_from_headers(sentry_init, capture_events, sampled, sample_rate):
sentry_init(traces_sample_rate=sample_rate)
events = capture_events()

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

if sampled is False:
if sampled is False or (sample_rate == 0 and sampled is None):
trace1, message = events

assert trace1["transaction"] == "hi"
Expand Down