From 2327fa7bbae8d392aab8732e7fe7d642cdb53aa5 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Fri, 29 Jan 2021 09:39:31 -0800 Subject: [PATCH] restore ability to have tracing disabled --- sentry_sdk/consts.py | 2 +- sentry_sdk/tracing.py | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index a58ac37afd..f40d2c24a6 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -72,7 +72,7 @@ def __init__( attach_stacktrace=False, # type: bool ca_certs=None, # type: Optional[str] propagate_traces=True, # type: bool - traces_sample_rate=0.0, # type: float + traces_sample_rate=None, # type: Optional[float] traces_sampler=None, # type: Optional[TracesSampler] auto_enabling_integrations=True, # type: bool _experiments={}, # type: Experiments # noqa: B006 diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 73531894ef..21269d68df 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -583,23 +583,22 @@ 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 - if not client: + # nothing to do if there's no client or if tracing is disabled + if not client or not has_tracing_enabled(options): self.sampled = False return - options = client.options + # 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 # we would have bailed already if neither `traces_sampler` nor # `traces_sample_rate` were defined, so one of these should work; prefer @@ -663,6 +662,19 @@ 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") is not None + or options.get("traces_sampler") is not None + ) + + def _is_valid_sample_rate(rate): # type: (Any) -> bool """