Skip to content

Commit e8f99b3

Browse files
authored
Enable tracing without performance by default (#4240)
Changing the default of `traces_sample_rate` to `0`. This means incoming traces will be continued, but we will not start traces on our own. (It used to be set to: never start or continue traces by default) Refs #4102
1 parent 9e48965 commit e8f99b3

File tree

12 files changed

+327
-23
lines changed

12 files changed

+327
-23
lines changed

MIGRATION_GUIDE.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
99
### Changed
1010

1111
- The SDK now supports Python 3.7 and higher.
12+
- The default of `traces_sample_rate` changed to `0`. Meaning: Incoming traces will be continued by default. For example, if your frontend sends a `sentry-trace/baggage` headers pair, your SDK will create Spans and send them to Sentry. (The default used to be `None` meaning by default no Spans where created, no matter what headers the frontend sent to your project.) See also: https://docs.sentry.io/platforms/python/configuration/options/#traces_sample_rate
1213
- `sentry_sdk.start_span` now only takes keyword arguments.
1314
- `sentry_sdk.start_transaction`/`sentry_sdk.start_span` no longer takes the following arguments: `span`, `parent_sampled`, `trace_id`, `span_id` or `parent_span_id`.
1415
- You can no longer change the sampled status of a span with `span.sampled = False` after starting it.

sentry_sdk/consts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def __init__(
511511
debug=None, # type: Optional[bool]
512512
attach_stacktrace=False, # type: bool
513513
ca_certs=None, # type: Optional[str]
514-
traces_sample_rate=None, # type: Optional[float]
514+
traces_sample_rate=0, # type: Optional[float]
515515
traces_sampler=None, # type: Optional[TracesSampler]
516516
profiles_sample_rate=None, # type: Optional[float]
517517
profiles_sampler=None, # type: Optional[TracesSampler]

tests/integrations/aiohttp/test_aiohttp.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,10 @@ async def hello(request):
448448
async def test_trace_from_headers_if_performance_disabled(
449449
sentry_init, aiohttp_client, capture_events
450450
):
451-
sentry_init(integrations=[AioHttpIntegration()])
451+
sentry_init(
452+
integrations=[AioHttpIntegration()],
453+
traces_sample_rate=None, # disable all performance monitoring
454+
)
452455

453456
async def hello(request):
454457
capture_message("It's a good day to try dividing by 0")

tests/integrations/asgi/test_asgi.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ async def test_has_trace_if_performance_disabled(
269269
asgi3_app_with_error_and_msg,
270270
capture_events,
271271
):
272-
sentry_init()
272+
sentry_init(
273+
traces_sample_rate=None, # disable all performance monitoring
274+
)
273275
app = SentryAsgiMiddleware(asgi3_app_with_error_and_msg)
274276

275277
with pytest.raises(ZeroDivisionError):
@@ -325,7 +327,9 @@ async def test_trace_from_headers_if_performance_disabled(
325327
asgi3_app_with_error_and_msg,
326328
capture_events,
327329
):
328-
sentry_init()
330+
sentry_init(
331+
traces_sample_rate=None, # disable all performance monitoring
332+
)
329333
app = SentryAsgiMiddleware(asgi3_app_with_error_and_msg)
330334

331335
trace_id = "582b43a4192642f0b136d5159a501701"

tests/integrations/django/asgi/test_asgi.py

+2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ async def test_has_trace_if_performance_enabled(sentry_init, capture_events):
321321
async def test_has_trace_if_performance_disabled(sentry_init, capture_events):
322322
sentry_init(
323323
integrations=[DjangoIntegration()],
324+
traces_sample_rate=None, # disable all performance monitoring
324325
)
325326

326327
events = capture_events()
@@ -386,6 +387,7 @@ async def test_trace_from_headers_if_performance_enabled(sentry_init, capture_ev
386387
async def test_trace_from_headers_if_performance_disabled(sentry_init, capture_events):
387388
sentry_init(
388389
integrations=[DjangoIntegration()],
390+
traces_sample_rate=None, # disable all performance monitoring
389391
)
390392

391393
events = capture_events()

tests/integrations/django/test_basic.py

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def test_trace_from_headers_if_performance_disabled(
241241
http_methods_to_capture=("HEAD",),
242242
)
243243
],
244+
traces_sample_rate=None, # disable all performance monitoring
244245
)
245246

246247
events = capture_events()

tests/integrations/opentelemetry/test_sampler.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
import sentry_sdk
77

88

9+
USE_DEFAULT_TRACES_SAMPLE_RATE = -1
10+
911
tracer = trace.get_tracer(__name__)
1012

1113

1214
@pytest.mark.parametrize(
1315
"traces_sample_rate, expected_num_of_envelopes",
1416
[
15-
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=None will be used)
16-
(-1, 0),
17+
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
18+
(USE_DEFAULT_TRACES_SAMPLE_RATE, 0),
1719
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
1820
(None, 0),
1921
# traces_sample_rate=0 means do not create new traces (0% of the requests), but continue incoming traces. So envelopes will be created only if there is an incoming trace.
@@ -29,7 +31,7 @@ def test_sampling_traces_sample_rate_0_or_100(
2931
expected_num_of_envelopes,
3032
):
3133
kwargs = {}
32-
if traces_sample_rate != -1:
34+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
3335
kwargs["traces_sample_rate"] = traces_sample_rate
3436

3537
sentry_init(**kwargs)
@@ -176,8 +178,8 @@ def keep_only_a(sampling_context):
176178
@pytest.mark.parametrize(
177179
"traces_sample_rate, expected_num_of_envelopes",
178180
[
179-
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=None will be used)
180-
(-1, 0),
181+
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
182+
(USE_DEFAULT_TRACES_SAMPLE_RATE, 1),
181183
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
182184
(None, 0),
183185
# traces_sample_rate=0 means do not create new traces (0% of the requests), but continue incoming traces. So envelopes will be created only if there is an incoming trace.
@@ -193,7 +195,7 @@ def test_sampling_parent_sampled(
193195
capture_envelopes,
194196
):
195197
kwargs = {}
196-
if traces_sample_rate != -1:
198+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
197199
kwargs["traces_sample_rate"] = traces_sample_rate
198200

199201
sentry_init(**kwargs)
@@ -227,9 +229,11 @@ def test_sampling_parent_sampled(
227229
@pytest.mark.parametrize(
228230
"traces_sample_rate, upstream_sampled, expected_num_of_envelopes",
229231
[
230-
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=None will be used)
231-
(-1, 0, 0),
232+
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
233+
(USE_DEFAULT_TRACES_SAMPLE_RATE, 0, 0),
234+
(USE_DEFAULT_TRACES_SAMPLE_RATE, 1, 1),
232235
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
236+
(None, 0, 0),
233237
(None, 1, 0),
234238
# traces_sample_rate=0 means do not create new traces (0% of the requests), but continue incoming traces. So envelopes will be created only if there is an incoming trace.
235239
(0, 0, 0),
@@ -247,7 +251,7 @@ def test_sampling_parent_dropped(
247251
capture_envelopes,
248252
):
249253
kwargs = {}
250-
if traces_sample_rate != -1:
254+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
251255
kwargs["traces_sample_rate"] = traces_sample_rate
252256

253257
sentry_init(**kwargs)
@@ -281,8 +285,8 @@ def test_sampling_parent_dropped(
281285
@pytest.mark.parametrize(
282286
"traces_sample_rate, expected_num_of_envelopes",
283287
[
284-
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=None will be used)
285-
(-1, 0),
288+
# special case for testing, do not pass any traces_sample_rate to init() (the default traces_sample_rate=0 will be used)
289+
(USE_DEFAULT_TRACES_SAMPLE_RATE, 0),
286290
# traces_sample_rate=None means do not create new traces, and also do not continue incoming traces. So, no envelopes at all.
287291
(None, 0),
288292
# traces_sample_rate=0 means do not create new traces (0% of the requests), but continue incoming traces. So envelopes will be created only if there is an incoming trace.
@@ -298,7 +302,7 @@ def test_sampling_parent_deferred(
298302
capture_envelopes,
299303
):
300304
kwargs = {}
301-
if traces_sample_rate != -1:
305+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
302306
kwargs["traces_sample_rate"] = traces_sample_rate
303307

304308
sentry_init(**kwargs)

tests/integrations/wsgi/test_wsgi.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ def dogpark(environ, start_response):
238238
capture_message("Attempting to fetch the ball")
239239
raise ValueError("Fetch aborted. The ball was not returned.")
240240

241-
sentry_init()
241+
sentry_init(
242+
traces_sample_rate=None, # disable all performance monitoring
243+
)
242244
app = SentryWsgiMiddleware(dogpark)
243245
client = Client(app)
244246
events = capture_events()
@@ -301,7 +303,9 @@ def dogpark(environ, start_response):
301303
capture_message("Attempting to fetch the ball")
302304
raise ValueError("Fetch aborted. The ball was not returned.")
303305

304-
sentry_init()
306+
sentry_init(
307+
traces_sample_rate=None, # disable all performance monitoring
308+
)
305309
app = SentryWsgiMiddleware(dogpark)
306310
client = Client(app)
307311
events = capture_events()

tests/test_dsc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ def my_traces_sampler(sampling_context):
287287
"local_traces_sampler_result": None,
288288
"local_traces_sample_rate": None,
289289
},
290-
None, # expected_sample_rate
291-
"tracing-disabled-no-transactions-should-be-sent", # expected_sampled (traces_sample_rate=None disables all transaction creation)
290+
1.0, # expected_sample_rate
291+
"true", # expected_sampled
292292
),
293293
( # 6 traces_sampler overrides incoming (traces_sample_rate not set)
294294
{

tests/tracing/test_sample_rand_propagation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_continue_trace_with_sample_rand(sentry_init):
1818
sentry_init()
1919

2020
headers = {
21-
"sentry-trace": "00000000000000000000000000000000-0000000000000000-0",
21+
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef-0",
2222
"baggage": "sentry-sample_rand=0.1,sentry-sample_rate=0.5",
2323
}
2424

@@ -34,7 +34,7 @@ def test_continue_trace_missing_sample_rand(sentry_init):
3434
sentry_init()
3535

3636
headers = {
37-
"sentry-trace": "00000000000000000000000000000000-0000000000000000",
37+
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef",
3838
"baggage": "sentry-placeholder=asdf",
3939
}
4040

tests/tracing/test_sampling.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ def test_records_lost_event_only_if_traces_sampler_enabled(
310310
sampled_output,
311311
expected_record_lost_event_calls,
312312
):
313-
sentry_init(traces_sampler=traces_sampler)
313+
sentry_init(
314+
traces_sample_rate=None,
315+
traces_sampler=traces_sampler,
316+
)
314317
record_lost_event_calls = capture_record_lost_event_calls()
315318

316319
with start_span(name="dogpark") as span:

0 commit comments

Comments
 (0)