|
| 1 | +import sys |
| 2 | + |
1 | 3 | from werkzeug.test import Client |
2 | 4 |
|
3 | 5 | import pytest |
4 | 6 |
|
5 | 7 | import sentry_sdk |
6 | 8 | from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware |
7 | | -from sentry_sdk.profiler import teardown_profiler |
8 | 9 | from collections import Counter |
9 | | -from sentry_sdk.utils import PY33 |
10 | 10 |
|
11 | 11 | try: |
12 | 12 | from unittest import mock # python 3.3 and above |
@@ -284,38 +284,42 @@ def sample_app(environ, start_response): |
284 | 284 | assert len(session_aggregates) == 1 |
285 | 285 |
|
286 | 286 |
|
287 | | -if PY33: |
288 | | - |
289 | | - @pytest.fixture |
290 | | - def profiling(): |
291 | | - yield |
292 | | - teardown_profiler() |
| 287 | +@pytest.mark.skipif( |
| 288 | + sys.version_info < (3, 3), reason="Profiling is only supported in Python >= 3.3" |
| 289 | +) |
| 290 | +@pytest.mark.parametrize( |
| 291 | + "profiles_sample_rate,profile_count", |
| 292 | + [ |
| 293 | + pytest.param(1.0, 1, id="profiler sampled at 1.0"), |
| 294 | + pytest.param(0.75, 1, id="profiler sampled at 0.75"), |
| 295 | + pytest.param(0.25, 0, id="profiler not sampled at 0.25"), |
| 296 | + pytest.param(None, 0, id="profiler not enabled"), |
| 297 | + ], |
| 298 | +) |
| 299 | +def test_profile_sent( |
| 300 | + capture_envelopes, |
| 301 | + sentry_init, |
| 302 | + teardown_profiling, |
| 303 | + profiles_sample_rate, |
| 304 | + profile_count, |
| 305 | +): |
| 306 | + def test_app(environ, start_response): |
| 307 | + start_response("200 OK", []) |
| 308 | + return ["Go get the ball! Good dog!"] |
293 | 309 |
|
294 | | - @pytest.mark.parametrize( |
295 | | - "profiles_sample_rate,should_send", |
296 | | - [(1.0, True), (0.75, True), (0.25, False), (None, False)], |
| 310 | + sentry_init( |
| 311 | + traces_sample_rate=1.0, |
| 312 | + _experiments={"profiles_sample_rate": profiles_sample_rate}, |
297 | 313 | ) |
298 | | - def test_profile_sent_when_profiling_enabled( |
299 | | - capture_envelopes, sentry_init, profiling, profiles_sample_rate, should_send |
300 | | - ): |
301 | | - def test_app(environ, start_response): |
302 | | - start_response("200 OK", []) |
303 | | - return ["Go get the ball! Good dog!"] |
304 | | - |
305 | | - sentry_init( |
306 | | - traces_sample_rate=1.0, |
307 | | - _experiments={"profiles_sample_rate": profiles_sample_rate}, |
308 | | - ) |
309 | | - app = SentryWsgiMiddleware(test_app) |
310 | | - envelopes = capture_envelopes() |
311 | | - |
312 | | - with mock.patch("sentry_sdk.profiler.random.random", return_value=0.5): |
313 | | - client = Client(app) |
314 | | - client.get("/") |
315 | | - |
316 | | - profile_sent = False |
317 | | - for item in envelopes[0].items: |
318 | | - if item.headers["type"] == "profile": |
319 | | - profile_sent = True |
320 | | - break |
321 | | - assert profile_sent == should_send |
| 314 | + app = SentryWsgiMiddleware(test_app) |
| 315 | + envelopes = capture_envelopes() |
| 316 | + |
| 317 | + with mock.patch("sentry_sdk.profiler.random.random", return_value=0.5): |
| 318 | + client = Client(app) |
| 319 | + client.get("/") |
| 320 | + |
| 321 | + count_item_types = Counter() |
| 322 | + for envelope in envelopes: |
| 323 | + for item in envelope.items: |
| 324 | + count_item_types[item.type] += 1 |
| 325 | + assert count_item_types["profile"] == profile_count |
0 commit comments