Skip to content

Commit d24d2ec

Browse files
cursoragentclaude
andcommitted
Refactor ASGI naming helpers and remove redundant check
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 77b9298 commit d24d2ec

File tree

1 file changed

+3
-37
lines changed

1 file changed

+3
-37
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from sentry_sdk.traces import (
2828
StreamedSpan,
2929
SegmentSource,
30-
SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE,
3130
)
3231
from sentry_sdk.tracing import (
3332
SOURCE_FOR_STYLE,
@@ -45,7 +44,6 @@
4544
_get_installed_modules,
4645
reraise,
4746
capture_internal_exceptions,
48-
qualname_from_function,
4947
)
5048

5149
from typing import TYPE_CHECKING
@@ -364,8 +362,7 @@ async def _sentry_wrapped_send(
364362
finally:
365363
if isinstance(span, StreamedSpan):
366364
already_set = (
367-
span is not None
368-
and span.name != _DEFAULT_TRANSACTION_NAME
365+
span.name != _DEFAULT_TRANSACTION_NAME
369366
and span.get_attributes().get("sentry.span.source")
370367
in [
371368
SegmentSource.COMPONENT.value,
@@ -460,36 +457,5 @@ def _get_transaction_name_and_source(
460457
def _get_segment_name_and_source(
461458
self: "SentryAsgiMiddleware", segment_style: str, asgi_scope: "Any"
462459
) -> "Tuple[str, str]":
463-
name = None
464-
source = SEGMENT_SOURCE_FOR_STYLE[segment_style].value
465-
ty = asgi_scope.get("type")
466-
467-
if segment_style == "endpoint":
468-
endpoint = asgi_scope.get("endpoint")
469-
# Webframeworks like Starlette mutate the ASGI env once routing is
470-
# done, which is sometime after the request has started. If we have
471-
# an endpoint, overwrite our generic transaction name.
472-
if endpoint:
473-
name = qualname_from_function(endpoint) or ""
474-
else:
475-
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
476-
source = SegmentSource.URL.value
477-
478-
elif segment_style == "url":
479-
# FastAPI includes the route object in the scope to let Sentry extract the
480-
# path from it for the transaction name
481-
route = asgi_scope.get("route")
482-
if route:
483-
path = getattr(route, "path", None)
484-
if path is not None:
485-
name = path
486-
else:
487-
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
488-
source = SegmentSource.URL.value
489-
490-
if name is None:
491-
name = _DEFAULT_TRANSACTION_NAME
492-
source = SegmentSource.ROUTE.value
493-
return name, source
494-
495-
return name, source
460+
name, source = self._get_transaction_name_and_source(segment_style, asgi_scope)
461+
return name, str(source)

0 commit comments

Comments
 (0)