8000 fix(client-reports): Record lost `sample_rate` events only if tracing… · anilktechie/sentry-python@d09221d · GitHub
[go: up one dir, main page]

Skip to content

Commit d09221d

Browse files
authored
fix(client-reports): Record lost sample_rate events only if tracing is enabled (getsentry#1268)
1 parent 3a7943b commit d09221d

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ A major release `N` implies the previous release `N-1` will no longer receive up
2323
## Unreleased
2424

2525
- Fix django legacy url resolver regex substitution due to upstream CVE-2021-44420 fix #1272
26+
- Record lost `sample_rate` events only if tracing is enabled
2627

2728
## 1.5.0
2829

sentry_sdk/tracing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,24 +543,24 @@ def finish(self, hub=None):
543543
hub = hub or self.hub or sentry_sdk.Hub.current
544544
client = hub.client
545545

546+
if client is None:
547+
# We have no client and therefore nowhere to send this transaction.
548+
return None
549+
546550
# This is a de facto proxy for checking if sampled = False
547551
if self._span_recorder is None:
548552
logger.debug("Discarding transaction because sampled = False")
549553

550554
# This is not entirely accurate because discards here are not
551555
# exclusively based on sample rate but also traces sampler, but
552556
# we handle this the same here.
553-
if client and client.transport:
557+
if client.transport and has_tracing_enabled(client.options):
554558
client.transport.record_lost_event(
555559
"sample_rate", data_category="transaction"
556560
)
557561

558562
return None
559563

560-
if client is None:
561-
# We have no client and therefore nowhere to send this transaction.
562-
return None
563-
564564
if not self.name:
565565
logger.warning(
566566
"Transaction has no name, falling back to `<unlabeled transaction>`."

sentry_sdk/tracing_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def has_tracing_enabled(options):
109109
# type: (Dict[str, Any]) -> bool
110110
"""
111111
Returns True if either traces_sample_rate or traces_sampler is
112-
non-zero/defined, False otherwise.
112+
defined, False otherwise.
113113
"""
114114

115115
return bool(

tests/tracing/test_sampling.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,61 @@ def test_warns_and_sets_sampled_to_false_on_invalid_traces_sampler_return_value(
284284
transaction = start_transaction(name="dogpark")
285285
logger.warning.assert_any_call(StringContaining("Given sample rate is invalid"))
286286
assert transaction.sampled is False
287+
288+
289+
@pytest.mark.parametrize(
290+
"traces_sample_rate,sampled_output,reports_output",
291+
[
292+
(None, False, []),
293+
(0.0, False, [("sample_rate", "transaction")]),
294+
(1.0, True, []),
295+
],
296+
)
297+
def test_records_lost_event_only_if_traces_sample_rate_enabled(
298+
sentry_init, traces_sample_rate, sampled_output, reports_output, monkeypatch
299+
):
300+
reports = []
301+
302+
def record_lost_event(reason, data_category=None, item=None):
303+
reports.append((reason, data_category))
304+
305+
sentry_init(traces_sample_rate=traces_sample_rate)
306+
307+
monkeypatch.setattr(
308+
Hub.current.client.transport, "record_lost_event", record_lost_event
309+
)
310+
311+
transaction = start_transaction(name="dogpark")
312+
assert transaction.sampled is sampled_output
313+
transaction.finish()
314+
315+
assert reports == reports_output
316+
317+
318+
@pytest.mark.parametrize(
319+
"traces_sampler,sampled_output,reports_output",
320+
[
321+
(None, False, []),
322+
(lambda _x: 0.0, False, [("sample_rate", "transaction")]),
323+
(lambda _x: 1.0, True, []),
324+
],
325+
)
326+
def test_records_lost_event_only_if_traces_sampler_enabled(
327+
sentry_init, traces_sampler, sampled_output, reports_output, monkeypatch
328+
):
329+
reports = []
330+
331+
def record_lost_event(reason, data_category=None, item=None):
332+
reports.append((reason, data_category))
333+
334+
sentry_init(traces_sampler=traces_sampler)
335+
336+
monkeypatch.setattr(
337+
Hub.current.client.transport, "record_lost_event", record_lost_event
338+
)
339+
340+
transaction = start_transaction(name="dogpark")
341+
assert transaction.sampled is sampled_output
342+
transaction.finish()
343+
344+
assert reports == reports_output

0 commit comments

Comments
 (0)
0