From 54a121eeda01d58c8c8d35bd044cfc7af4117ba1 Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Tue, 27 Jun 2023 17:48:55 -0400 Subject: [PATCH] feat(profiling): Add client reports for profiles To help understand the client discard in profiles better. --- sentry_sdk/profiler.py | 12 ++++++++++++ tests/test_profiler.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/sentry_sdk/profiler.py b/sentry_sdk/profiler.py index 25c1d9d02b..9b16d87528 100644 --- a/sentry_sdk/profiler.py +++ b/sentry_sdk/profiler.py @@ -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 + + 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 diff --git a/tests/test_profiler.py b/tests/test_profiler.py index 8ddbc333da..70110e19ce 100644 --- a/tests/test_profiler.py +++ b/tests/test_profiler.py @@ -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, @@ -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"): @@ -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) @@ -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, @@ -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"): @@ -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( @@ -250,6 +265,7 @@ def test_minimum_unique_samples_required( ) envelopes = capture_envelopes() + reports = capture_client_reports() with start_transaction(name="profiling"): pass @@ -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)