Skip to content

Commit 2df9e1a

Browse files
authored
ref(tracing): Restore ability to have tracing disabled (#991)
This partially reverts #948 and 6fc2287, to restore the ability to disable tracing, which allows it to truly be opt-in as per the spec, which is detailed here: https://develop.sentry.dev/sdk/performance/#sdk-configuration). Note that this does not change the behavior that PR was made to reinstate - the model wherein the front end makes sampling decisions, the backend has `traces_sample_rate` set to `0`, and the result is that the backend samples according to the front end decision when there is one, but otherwise does not send transactions.
1 parent 0be96f0 commit 2df9e1a

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

sentry_sdk/consts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(
7272
attach_stacktrace=False, # type: bool
7373
ca_certs=None, # type: Optional[str]
7474
propagate_traces=True, # type: bool
75-
traces_sample_rate=0.0, # type: float
75+
traces_sample_rate=None, # type: Optional[float]
7676
traces_sampler=None, # type: Optional[TracesSampler]
7777
auto_enabling_integrations=True, # type: bool
7878
_experiments={}, # type: Experiments # noqa: B006

sentry_sdk/tracing.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,23 +583,22 @@ 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-
591586
hub = self.hub or sentry_sdk.Hub.current
592587
client = hub.client
588+
options = (client and client.options) or {}
593589
transaction_description = "{op}transaction <{name}>".format(
594590
op=("<" + self.op + "> " if self.op else ""), name=self.name
595591
)
596592

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

602-
options = client.options
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
603602

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

665664

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(
673+
options.get("traces_sample_rate") is not None
674+
or options.get("traces_sampler") is not None
675+
)
676+
677+
666678
def _is_valid_sample_rate(rate):
667679
# type: (Any) -> bool
668680
"""

0 commit comments

Comments
 (0)