8000 Fix ASGI tests · getsentry/sentry-python@98a7241 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98a7241

Browse files
committed
Fix ASGI tests
1 parent fb66e1a commit 98a7241

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,31 @@ async def _run_app(self, scope, receive, send, asgi_version):
176176
_asgi_middleware_applied.set(True)
177177
try:
178178
with sentry_sdk.isolation_scope() as sentry_scope:
179+
(
180+
transaction_name,
181+
transaction_source,
182+
) = self._get_transaction_name_and_source(
183+
self.transaction_style,
184+
scope,
185+
)
186+
sentry_scope.set_transaction_name(
187+
transaction_name,
188+
source=transaction_source,
189+
)
190+
179191
with track_session(sentry_scope, session_mode="request"):
180192
sentry_scope.clear_breadcrumbs()
181193
sentry_scope._name = "asgi"
182194
processor = partial(self.event_processor, asgi_scope=scope)
183195
sentry_scope.add_event_processor(processor)
184196

185197
ty = scope["type"]
186-
(
187-
transaction_name,
188-
transaction_source,
189-
) = self._get_transaction_name_and_source(
190-
self.transaction_style,
191-
scope,
192-
)
193198

194199
method = scope.get("method", "").upper()
195200
should_trace = method in self.http_methods_to_capture
196201
with sentry_sdk.continue_trace(_get_headers(scope)):
197202
with (
198-
sentry_sdk.start_transaction(
203+
sentry_sdk.start_span(
199204
op=(
200205
OP.WEBSOCKET_SERVER
201206
if ty == "websocket"
@@ -251,13 +256,18 @@ def event_processor(self, event, hint, asgi_scope):
251256
event["request"] = deepcopy(request_data)
252257

253258
# Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)
254-
already_set = event["transaction"] != _DEFAULT_TRANSACTION_NAME and event[
255-
"transaction_info"
256-
].get("source") in [
257-
TRANSACTION_SOURCE_COMPONENT,
258-
TRANSACTION_SOURCE_ROUTE,
259-
TRANSACTION_SOURCE_CUSTOM,
260-
]
259+
already_set = (
260+
"transaction" in event
261+
and event["transaction"] != _DEFAULT_TRANSACTION_NAME
262+
and "transaction_info" in event
263+
and "source" in event["transaction_info"]
264+
and event["transaction_info"]["source"]
265+
in [
266+
TRANSACTION_SOURCE_COMPONENT,
267+
TRANSACTION_SOURCE_ROUTE,
268+
TRANSACTION_SOURCE_CUSTOM,
269+
]
270+
)
261271
if not already_set:
262272
name, source = self._get_transaction_name_and_source(
263273
self.transaction_style, asgi_scope

sentry_sdk/integrations/wsgi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ def __call__(self, environ, start_response):
9898
_wsgi_middleware_applied.set(True)
9999
try:
100100
with sentry_sdk.isolation_scope() as scope:
101-
scope.set_transaction_name(DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_ROUTE)
101+
scope.set_transaction_name(
102+
DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_ROUTE
103+
)
102104

103105
with track_session(scope, session_mode="request"):
104106
with capture_internal_exceptions():

sentry_sdk/tracing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,9 @@ def get_trace_context(self):
683683
rv["status"] = self.status
684684

685685
if self.containing_transaction:
686-
rv["dynamic_sampling_context"] = (
687-
self.containing_transaction.get_baggage().dynamic_sampling_context()
688-
)
686+
rv[
687+
"dynamic_sampling_context"
688+
] = self.containing_transaction.get_baggage().dynamic_sampling_context()
689689

690690
data = {}
691691

@@ -1203,6 +1203,7 @@ def __init__(
12031203
start_timestamp=None, # type: Optional[Union[datetime, float]]
12041204
origin=None, # type: Optional[str]
12051205
name=None, # type: Optional[str]
1206+
source=TRANSACTION_SOURCE_CUSTOM, # type: str
12061207
otel_span=None, # type: Optional[OtelSpan]
12071208
**_, # type: dict[str, object]
12081209
):
@@ -1232,6 +1233,7 @@ def __init__(
12321233
self.op = op
12331234
self.description = description
12341235
self.name = span_name
1236+
self.source = source
12351237

12361238
if status is not None:
12371239
self.set_status(status)

0 commit comments

Comments
 (0)
0