Skip to content
Merged
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
29 changes: 4 additions & 25 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from sentry_sdk.traces import (
BAGGAGE_HEADER_NAME,
SENTRY_TRACE_HEADER_NAME,
SOURCE_FOR_STYLE,
SegmentNameSource,
Span,
SpanStatus,
Expand All @@ -47,7 +46,6 @@
parse_url,
parse_version,
reraise,
transaction_from_function,
)

try:
Expand All @@ -74,25 +72,15 @@
from sentry_sdk.utils import ExcInfo


TRANSACTION_STYLE_VALUES = ("handler_name", "method_and_path_pattern")


class AioHttpIntegration(Integration):
identifier = "aiohttp"
origin = f"auto.http.{identifier}"

def __init__(
self,
transaction_style: str = "handler_name",
*,
failed_request_status_codes: "Set[int]" = _DEFAULT_FAILED_REQUEST_STATUS_CODES,
) -> None:
if transaction_style not in TRANSACTION_STYLE_VALUES:
raise ValueError(
"Invalid value for transaction_style: %s (must be in %s)"
% (transaction_style, TRANSACTION_STYLE_VALUES)
)
self.transaction_style = transaction_style
self._failed_request_status_codes = failed_request_status_codes

@staticmethod
Expand Down Expand Up @@ -265,21 +253,12 @@ async def sentry_urldispatcher_resolve(
if server_span is not None and pattern is not None:
server_span.set_attribute(SPANDATA.HTTP_ROUTE, pattern)

name = None

try:
if integration.transaction_style == "handler_name":
name = transaction_from_function(rv.handler)
elif integration.transaction_style == "method_and_path_pattern":
name = "{} {}".format(request.method, pattern)
except Exception:
pass
current_scope = sentry_sdk.get_current_scope()

if name is not None:
current_scope = sentry_sdk.get_current_scope()
if pattern is not None:
current_scope.set_transaction_name(
name,
source=SOURCE_FOR_STYLE[integration.transaction_style],
pattern,
source=SegmentNameSource.ROUTE,
)

return rv
Expand Down
49 changes: 9 additions & 40 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ async def hello(request):

(event,) = events

assert (
event["transaction"]
== "tests.integrations.aiohttp.test_aiohttp.test_basic.<locals>.hello"
)
assert event["transaction"] == "/"

(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
Expand Down Expand Up @@ -325,10 +322,7 @@ async def hello(request):

(span,) = [item.payload for item in items]

assert (
span["name"]
== "tests.integrations.aiohttp.test_aiohttp.test_tracing_unparseable_url.<locals>.hello"
)
assert span["name"] == "/"


@pytest.mark.asyncio
Expand Down Expand Up @@ -1340,14 +1334,11 @@ async def hello(request):
(server_span,) = [item.payload for item in items]

assert server_span["is_segment"] is True
assert (
server_span["name"]
== "tests.integrations.aiohttp.test_aiohttp.test_tracing.<locals>.hello"
)
assert server_span["name"] == "/"
assert server_span["attributes"]["sentry.op"] == "http.server"
assert server_span["attributes"]["sentry.origin"] == "auto.http.aiohttp"
assert server_span["attributes"]["http.response.status_code"] == 200
assert server_span["attributes"]["sentry.segment.name.source"] == "component"
assert server_span["attributes"]["sentry.segment.name.source"] == "route"
assert server_span["status"] == "ok"
# No query string on the request, so the attribute should be omitted.
assert "url.query" not in server_span["attributes"]
Expand Down Expand Up @@ -1450,35 +1441,13 @@ async def hello(request):


@pytest.mark.asyncio
@pytest.mark.parametrize(
"url,transaction_style,expected_name,expected_source",
[
(
"/message",
"handler_name",
"tests.integrations.aiohttp.test_aiohttp."
"test_transaction_style.<locals>.hello",
"component",
),
(
"/message",
"method_and_path_pattern",
"GET /{var}",
"route",
),
],
)
async def test_transaction_style(
sentry_init,
aiohttp_client,
capture_items,
url,
transaction_style,
expected_name,
expected_source,
):
sentry_init(
integrations=[AioHttpIntegration(transaction_style=transaction_style)],
integrations=[AioHttpIntegration()],
traces_sample_rate=1.0,
data_collection={},
)
Expand All @@ -1492,17 +1461,17 @@ async def hello(request):
items = capture_items("span")

client = await aiohttp_client(app)
resp = await client.get(url)
resp = await client.get("/message")
assert resp.status == 200

sentry_sdk.flush()

assert len(items) == 1
(server_segment,) = [item.payload for item in items]

assert server_segment["name"] == expected_name
assert server_segment["name"] == "/{var}"
assert server_segment["is_segment"]
assert server_segment["attributes"]["sentry.segment.name.source"] == expected_source
assert server_segment["attributes"]["sentry.segment.name.source"] == "route"


@pytest.mark.asyncio
Expand Down Expand Up @@ -1781,7 +1750,7 @@ async def hello(request):

child_span, server_span = [item.payload for item in items]

assert server_span["attributes"]["sentry.segment.name.source"] == "component"
assert server_span["attributes"]["sentry.segment.name.source"] == "route"
assert "sentry.segment.name.source" not in child_span["attributes"]

if expect_user_info:
Expand Down
Loading