Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(profiling): Add client reports for profiles #2207

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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"
)
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
Loading