Skip to content

Commit

Permalink
feat(profiling): Add client reports for profiles (#2207)
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 29, 2023
1 parent 0245011 commit 7db2f97
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,26 @@ 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

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"
)
return False

if self.unique_samples < PROFILE_MINIMUM_SAMPLES:
if client.transport:
client.transport.record_lost_event(
"insufficient_data", data_category="profile"
)
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 7db2f97

Please sign in to comment.