Skip to content

Commit

Permalink
feat(profiling): Add client reports for profiles
Browse files Browse the repository at this point in the history
To help understand the client discard in profiles better.
  • Loading branch information
Zylphrex committed Jun 27, 2023
1 parent 625e1b3 commit 54a121e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,22 @@ def to_json(self, event_opt, options):

def valid(self):
# type: () -> bool
hub = self.hub or sentry_sdk.Hub.current
client = hub.client
if client is None:
return False

Check warning on line 734 in sentry_sdk/profiler.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/profiler.py#L734

Added line #L734 was not covered by tests

if not has_profiling_enabled(client.options):
return False

if self.sampled is None or not self.sampled:
if client.transport:
client.transport.record_lost_event("sample_rate", data_category="profile")

Check warning on line 741 in sentry_sdk/profiler.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/profiler.py#L741

Added line #L741 was not covered by tests
return False

if self.unique_samples < PROFILE_MINIMUM_SAMPLES:
if client.transport:
client.transport.record_lost_event("insufficient_data", data_category="profile")

Check warning on line 746 in sentry_sdk/profiler.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/profiler.py#L746

Added line #L746 was not covered by tests
logger.debug("[Profiling] Discarding profile because insufficient samples.")
return False

Expand Down
17 changes: 17 additions & 0 deletions tests/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_profiler_setup_twice(make_options, teardown_profiling):
def test_profiles_sample_rate(
sentry_init,
capture_envelopes,
capture_client_reports,
teardown_profiling,
profiles_sample_rate,
profile_count,
Expand All @@ -162,6 +163,7 @@ def test_profiles_sample_rate(
)

envelopes = capture_envelopes()
reports = capture_client_reports()

with mock.patch("sentry_sdk.profiler.random.random", return_value=0.5):
with start_transaction(name="profiling"):
Expand All @@ -174,6 +176,12 @@ 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 == []
elif profile_count:
assert reports == []
else:
assert reports == [("sample_rate", "profile")]


@requires_python_version(3, 3)
Expand Down Expand Up @@ -213,6 +221,7 @@ def test_profiles_sample_rate(
def test_profiles_sampler(
sentry_init,
capture_envelopes,
capture_client_reports,
teardown_profiling,
profiles_sampler,
profile_count,
Expand All @@ -224,6 +233,7 @@ def test_profiles_sampler(
)

envelopes = capture_envelopes()
reports = capture_client_reports()

with mock.patch("sentry_sdk.profiler.random.random", return_value=0.5):
with start_transaction(name="profiling"):
Expand All @@ -236,12 +246,17 @@ def test_profiles_sampler(

assert len(items["transaction"]) == 1
assert len(items["profile"]) == profile_count
if profile_count:
assert reports == []
else:
assert reports == [("sample_rate", "profile")]


@requires_python_version(3, 3)
def test_minimum_unique_samples_required(
sentry_init,
capture_envelopes,
capture_client_reports,
teardown_profiling,
):
sentry_init(
Expand All @@ -250,6 +265,7 @@ def test_minimum_unique_samples_required(
)

envelopes = capture_envelopes()
reports = capture_client_reports()

with start_transaction(name="profiling"):
pass
Expand All @@ -263,6 +279,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")]


@requires_python_version(3, 3)
Expand Down

0 comments on commit 54a121e

Please sign in to comment.