Skip to content

Commit

Permalink
test: Introduce capture_record_lost_event_calls fixture
Browse files Browse the repository at this point in the history
`capture_record_lost_event_calls` replaces the `capture_client_reports` fixture. The fixture records calls to `Transport.record_lost_event` by noting the arguments passed to each call.

This change is being introduced in preparation for getsentry#3244, which changes `Transport.record_lost_event`'s signature and behavior.
  • Loading branch information
szokeasaurusrex authored and arjenzorgdoc committed Sep 30, 2024
1 parent 96e8bdf commit eb8e49e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
12 changes: 5 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,18 @@ def append_envelope(envelope):


@pytest.fixture
def capture_client_reports(monkeypatch):
def capture_record_lost_event_calls(monkeypatch):
def inner():
reports = []
test_client = sentry_sdk.Hub.current.client
calls = []
test_client = sentry_sdk.get_client()

def record_lost_event(reason, data_category=None, item=None):
if data_category is None:
data_category = item.data_category
return reports.append((reason, data_category))
calls.append((reason, data_category, item))

monkeypatch.setattr(
test_client.transport, "record_lost_event", record_lost_event
)
return reports
return calls

return inner

Expand Down
24 changes: 12 additions & 12 deletions tests/profiler/test_transaction_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_profiler_setup_twice(make_options, teardown_profiling):
def test_profiles_sample_rate(
sentry_init,
capture_envelopes,
capture_client_reports,
capture_record_lost_event_calls,
teardown_profiling,
profiles_sample_rate,
profile_count,
Expand All @@ -142,7 +142,7 @@ def test_profiles_sample_rate(
)

envelopes = capture_envelopes()
reports = capture_client_reports()
record_lost_event_calls = capture_record_lost_event_calls()

with mock.patch(
"sentry_sdk.profiler.transaction_profiler.random.random", return_value=0.5
Expand All @@ -158,11 +158,11 @@ def test_profiles_sample_rate(
assert len(items["transaction"]) == 1
assert len(items["profile"]) == profile_count
if profiles_sample_rate is None or profiles_sample_rate == 0:
assert reports == []
assert record_lost_event_calls == []
elif profile_count:
assert reports == []
assert record_lost_event_calls == []
else:
assert reports == [("sample_rate", "profile")]
assert record_lost_event_calls == [("sample_rate", "profile", None)]


@pytest.mark.parametrize(
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_profiles_sample_rate(
def test_profiles_sampler(
sentry_init,
capture_envelopes,
capture_client_reports,
capture_record_lost_event_calls,
teardown_profiling,
profiles_sampler,
profile_count,
Expand All @@ -213,7 +213,7 @@ def test_profiles_sampler(
)

envelopes = capture_envelopes()
reports = capture_client_reports()
record_lost_event_calls = capture_record_lost_event_calls()

with mock.patch(
"sentry_sdk.profiler.transaction_profiler.random.random", return_value=0.5
Expand All @@ -229,15 +229,15 @@ def test_profiles_sampler(
assert len(items["transaction"]) == 1
assert len(items["profile"]) == profile_count
if profile_count:
assert reports == []
assert record_lost_event_calls == []
else:
assert reports == [("sample_rate", "profile")]
assert record_lost_event_calls == [("sample_rate", "profile", None)]


def test_minimum_unique_samples_required(
sentry_init,
capture_envelopes,
capture_client_reports,
capture_record_lost_event_calls,
teardown_profiling,
):
sentry_init(
Expand All @@ -246,7 +246,7 @@ def test_minimum_unique_samples_required(
)

envelopes = capture_envelopes()
reports = capture_client_reports()
record_lost_event_calls = capture_record_lost_event_calls()

with start_transaction(name="profiling"):
pass
Expand All @@ -260,7 +260,7 @@ def test_minimum_unique_samples_required(
# because we dont leave any time for the profiler to
# take any samples, it should be not be sent
assert len(items["profile"]) == 0
assert reports == [("insufficient_data", "profile")]
assert record_lost_event_calls == [("insufficient_data", "profile", None)]


@pytest.mark.forked
Expand Down
25 changes: 15 additions & 10 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import time
from collections import Counter

import pytest
from sentry_sdk.client import Client
Expand Down Expand Up @@ -544,7 +545,7 @@ def test_capture_event_with_scope_kwargs(sentry_init, capture_events):


def test_dedupe_event_processor_drop_records_client_report(
sentry_init, capture_events, capture_client_reports
sentry_init, capture_events, capture_record_lost_event_calls
):
"""
DedupeIntegration internally has an event_processor that filters duplicate exceptions.
Expand All @@ -553,7 +554,7 @@ def test_dedupe_event_processor_drop_records_client_report(
"""
sentry_init()
events = capture_events()
reports = capture_client_reports()
record_lost_event_calls = capture_record_lost_event_calls()

try:
raise ValueError("aha!")
Expand All @@ -565,19 +566,19 @@ def test_dedupe_event_processor_drop_records_client_report(
capture_exception()

(event,) = events
(report,) = reports
(lost_event_call,) = record_lost_event_calls

assert event["level"] == "error"
assert "exception" in event
assert report == ("event_processor", "error")
assert lost_event_call == ("event_processor", "error", None)


def test_event_processor_drop_records_client_report(
sentry_init, capture_events, capture_client_reports
sentry_init, capture_events, capture_record_lost_event_calls
):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
reports = capture_client_reports()
record_lost_event_calls = capture_record_lost_event_calls()

# Ensure full idempotency by restoring the original global event processors list object, not just a copy.
old_processors = sentry_sdk.scope.global_event_processors
Expand All @@ -597,10 +598,14 @@ def foo(event, hint):
pass

assert len(events) == 0
assert reports == [
("event_processor", "error"),
("event_processor", "transaction"),
]

# Using Counter because order of record_lost_event calls does not matter
assert Counter(record_lost_event_calls) == Counter(
[
("event_processor", "error", None),
("event_processor", "transaction", None),
]
)

finally:
sentry_sdk.scope.global_event_processors = old_processors
Expand Down
6 changes: 3 additions & 3 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ def test_monitor_unhealthy(sentry_init):


def test_transaction_uses_downsampled_rate(
sentry_init, capture_client_reports, monkeypatch
sentry_init, capture_record_lost_event_calls, monkeypatch
):
sentry_init(
traces_sample_rate=1.0,
transport=UnhealthyTestTransport(),
)

reports = capture_client_reports()
record_lost_event_calls = capture_record_lost_event_calls()

monitor = sentry_sdk.get_client().monitor
monitor.interval = 0.1
Expand All @@ -79,7 +79,7 @@ def test_transaction_uses_downsampled_rate(
assert transaction.sampled is False
assert transaction.sample_rate == 0.5

assert reports == [("backpressure", "transaction")]
assert record_lost_event_calls == [("backpressure", "transaction", None)]


def test_monitor_no_thread_on_shutdown_no_errors(sentry_init):
Expand Down

0 comments on commit eb8e49e

Please sign in to comment.