Skip to content
Closed
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
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ 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.
Expand Down
43 changes: 43 additions & 0 deletions tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
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():
Expand Down Expand Up @@ -202,3 +207,41 @@ 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, # noqa:N803
ObjectDescribedBy, # noqa:N803
):
test_url = "/sync-message"

traces_sampler = mock.Mock()
sentry_init(send_default_pii=True, traces_sampler=traces_sampler)

events = capture_events()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do something with this var, such as asserting the event did/did not get sent?


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",
},
)
}
)
)