diff --git a/sentry_sdk/profiler.py b/sentry_sdk/profiler.py index 25c1d9d02b..edc4fc750d 100644 --- a/sentry_sdk/profiler.py +++ b/sentry_sdk/profiler.py @@ -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 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)