From 1362c96b2367326139ff17f7268b057f2cf59f2e Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 27 Oct 2020 17:13:30 +0100 Subject: [PATCH 1/3] update custom sampling context --- sentry_sdk/integrations/asgi.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 7a0d0bd339..c5ec4097bc 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -139,7 +139,10 @@ async def _run_app(self, scope, callback): transaction.name = _DEFAULT_TRANSACTION_NAME transaction.set_tag("asgi.type", ty) - with hub.start_transaction(transaction): + with hub.start_transaction( + transaction, + custom_sampling_context={"scope": scope} + ): # XXX: Would be cool to have correct span status, but we # would have to wrap send(). That is a bit hard to do with # the current abstraction over ASGI 2/3. From 53a72f3d8f8ed8889fcafa2f72ba97ba524a565b Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 27 Oct 2020 21:09:04 +0100 Subject: [PATCH 2/3] add test method --- tests/integrations/asgi/test_asgi.py | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index 521c7c8302..62cb89227e 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -8,6 +8,10 @@ from starlette.testclient import TestClient from starlette.websockets import WebSocket +try: + from unittest import mock # python 3.3 and above +except ImportError: + import mock # python < 3.3 @pytest.fixture def app(): @@ -202,3 +206,42 @@ def handler(*args, **kwargs): (exception,) = event["exception"]["values"] assert exception["type"] == "ValueError" assert exception["value"] == "oh no" + +async def test_traces_sampler_gets_scope_object_in_sampling_context( + sentry_init, + app, + capture_events, + DictionaryContaining, + ObjectDescribedBy +): + test_url = "/sync-message" + + traces_sampler = mock.Mock() + sentry_init( + send_default_pii=True, + traces_sampler=traces_sampler + ) + + events = capture_events() + + client = TestClient(app) + response = client.get(test_url) + + assert response.status_code == 200 + + traces_sampler.assert_any_call( + DictionaryContaining( + { + "scope": ObjectDescribedBy( + type=dict, attrs={ + 'type': 'http', + 'http_version': '1.1', + 'method': 'GET', + 'path': test_url, + 'root_path': '', + 'scheme': 'http' + } + ) + } + ) + ) From 9a2e0d9d99d31164485e8ee529e7ff1d98fc3662 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Mon, 2 Nov 2020 14:01:45 +0100 Subject: [PATCH 3/3] fix formatting --- sentry_sdk/integrations/asgi.py | 3 +- tests/integrations/asgi/test_asgi.py | 42 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index c5ec4097bc..0a6884c795 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -140,8 +140,7 @@ async def _run_app(self, scope, callback): transaction.set_tag("asgi.type", ty) with hub.start_transaction( - transaction, - custom_sampling_context={"scope": scope} + transaction, custom_sampling_context={"scope": scope} ): # XXX: Would be cool to have correct span status, but we # would have to wrap send(). That is a bit hard to do with diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index 62cb89227e..6b0a68a244 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -13,6 +13,7 @@ except ImportError: import mock # python < 3.3 + @pytest.fixture def app(): app = Starlette() @@ -207,20 +208,18 @@ def handler(*args, **kwargs): assert exception["type"] == "ValueError" assert exception["value"] == "oh no" + async def test_traces_sampler_gets_scope_object_in_sampling_context( - sentry_init, - app, - capture_events, - DictionaryContaining, - ObjectDescribedBy + sentry_init, + app, + capture_events, + DictionaryContaining, # noqa:N803 + ObjectDescribedBy, # noqa:N803 ): test_url = "/sync-message" traces_sampler = mock.Mock() - sentry_init( - send_default_pii=True, - traces_sampler=traces_sampler - ) + sentry_init(send_default_pii=True, traces_sampler=traces_sampler) events = capture_events() @@ -231,17 +230,18 @@ async def test_traces_sampler_gets_scope_object_in_sampling_context( traces_sampler.assert_any_call( DictionaryContaining( - { - "scope": ObjectDescribedBy( - type=dict, attrs={ - 'type': 'http', - 'http_version': '1.1', - 'method': 'GET', - 'path': test_url, - 'root_path': '', - 'scheme': 'http' - } - ) - } + { + "scope": ObjectDescribedBy( + type=dict, + attrs={ + "type": "http", + "http_version": "1.1", + "method": "GET", + "path": test_url, + "root_path": "", + "scheme": "http", + }, + ) + } ) )