Skip to content

Commit

Permalink
feat(tracing): Add aiohttp request object to sampling context (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie authored Oct 30, 2020
1 parent ba1e550 commit 617c516
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ async def sentry_app_handle(self, request, *args, **kwargs):
# URL resolver did not find a route or died trying.
name="generic AIOHTTP request",
)

with hub.start_transaction(transaction):
with hub.start_transaction(
transaction, custom_sampling_context={"aiohttp_request": request}
):
try:
response = await old_handle(self, request)
except HTTPException as e:
Expand Down
38 changes: 38 additions & 0 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
import pytest
from aiohttp import web
from aiohttp.client import ServerDisconnectedError
from aiohttp.web_request import Request

from sentry_sdk.integrations.aiohttp import AioHttpIntegration

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3


async def test_basic(sentry_init, aiohttp_client, loop, capture_events):
sentry_init(integrations=[AioHttpIntegration()])
Expand Down Expand Up @@ -223,3 +229,35 @@ async def hello(request):

assert event["type"] == "transaction"
assert event["transaction"] == expected_transaction


async def test_traces_sampler_gets_request_object_in_sampling_context(
sentry_init,
aiohttp_client,
DictionaryContaining, # noqa:N803
ObjectDescribedBy, # noqa:N803
):
traces_sampler = mock.Mock()
sentry_init(
integrations=[AioHttpIntegration()],
traces_sampler=traces_sampler,
)

async def kangaroo_handler(request):
return web.Response(text="dogs are great")

app = web.Application()
app.router.add_get("/tricks/kangaroo", kangaroo_handler)

client = await aiohttp_client(app)
await client.get("/tricks/kangaroo")

traces_sampler.assert_any_call(
DictionaryContaining(
{
"aiohttp_request": ObjectDescribedBy(
type=Request, attrs={"method": "GET", "path": "/tricks/kangaroo"}
)
}
)
)

0 comments on commit 617c516

Please sign in to comment.