diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 5e8a21e027..73531894ef 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -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 @@ -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 """ diff --git a/setup.py b/setup.py index 59aef3600c..074a80eebb 100644 --- a/setup.py +++ b/setup.py @@ -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", @@ -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"]}, diff --git a/tests/tracing/test_integration_tests.py b/tests/tracing/test_integration_tests.py index 298f460d59..c4c316be96 100644 --- a/tests/tracing/test_integration_tests.py +++ b/tests/tracing/test_integration_tests.py @@ -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()) @@ -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"