Skip to content

Commit 1cf5d8d

Browse files
authored
Add transaction styling for aiohttp integration (getsentry#876)
1 parent c752e9f commit 1cf5d8d

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

sentry_sdk/integrations/aiohttp.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@
4343
from sentry_sdk._types import EventProcessor
4444

4545

46+
TRANSACTION_STYLE_VALUES = ("handler_name", "method_and_path_pattern")
47+
48+
4649
class AioHttpIntegration(Integration):
4750
identifier = "aiohttp"
4851

52+
def __init__(self, transaction_style="handler_name"):
53+
# type: (str) -> None
54+
if transaction_style not in TRANSACTION_STYLE_VALUES:
55+
raise ValueError(
56+
"Invalid value for transaction_style: %s (must be in %s)"
57+
% (transaction_style, TRANSACTION_STYLE_VALUES)
58+
)
59+
self.transaction_style = transaction_style
60+
4961
@staticmethod
5062
def setup_once():
5163
# type: () -> None
@@ -120,10 +132,18 @@ async def sentry_urldispatcher_resolve(self, request):
120132
# type: (UrlDispatcher, Request) -> AbstractMatchInfo
121133
rv = await old_urldispatcher_resolve(self, request)
122134

135+
hub = Hub.current
136+
integration = hub.get_integration(AioHttpIntegration)
137+
123138
name = None
124139

125140
try:
126-
name = transaction_from_function(rv.handler)
141+
if integration.transaction_style == "handler_name":
142+
name = transaction_from_function(rv.handler)
143+
elif integration.transaction_style == "method_and_path_pattern":
144+
route_info = rv.get_info()
145+
pattern = route_info.get("path") or route_info.get("formatter")
146+
name = "{} {}".format(request.method, pattern)
127147
except Exception:
128148
pass
129149

tests/integrations/aiohttp/test_aiohttp.py

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
from contextlib import suppress
44

5+
import pytest
56
from aiohttp import web
67
from aiohttp.client import ServerDisconnectedError
78

@@ -186,3 +187,39 @@ async def hello(request):
186187
event["transaction"]
187188
== "tests.integrations.aiohttp.test_aiohttp.test_tracing.<locals>.hello"
188189
)
190+
191+
192+
@pytest.mark.parametrize(
193+
"transaction_style,expected_transaction",
194+
[
195+
(
196+
"handler_name",
197+
"tests.integrations.aiohttp.test_aiohttp.test_transaction_style.<locals>.hello",
198+
),
199+
("method_and_path_pattern", "GET /{var}"),
200+
],
201+
)
202+
async def test_transaction_style(
203+
sentry_init, aiohttp_client, capture_events, transaction_style, expected_transaction
204+
):
205+
sentry_init(
206+
integrations=[AioHttpIntegration(transaction_style=transaction_style)],
207+
traces_sample_rate=1.0,
208+
)
209+
210+
async def hello(request):
211+
return web.Response(text="hello")
212+
213+
app = web.Application()
214+
app.router.add_get(r"/{var}", hello)
215+
216+
events = capture_events()
217+
218+
client = await aiohttp_client(app)
219+
resp = await client.get("/1")
220+
assert resp.status == 200
221+
222+
(event,) = events
223+
224+
assert event["type"] == "transaction"
225+
assert event["transaction"] == expected_transaction

0 commit comments

Comments
 (0)