Skip to content

Commit 5d8a859

Browse files
committed
fix(asyncio): treat asyncio.Tasks as trace continuation and new transactions
1 parent 499c041 commit 5d8a859

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

sentry_sdk/integrations/asyncio.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def _sentry_task_factory(
5757
coro: "Coroutine[Any, Any, Any]",
5858
**kwargs: "Any",
5959
) -> "asyncio.Future[Any]":
60+
# we get the current span here, as later it may already be closed
61+
in_span = sentry_sdk.get_current_span() is not None
62+
headers = dict(
63+
sentry_sdk.get_current_scope().iter_trace_propagation_headers()
64+
)
65+
6066
@_wrap_coroutine(coro)
6167
async def _task_with_sentry_span_creation() -> "Any":
6268
result = None
@@ -66,16 +72,16 @@ async def _task_with_sentry_span_creation() -> "Any":
6672
)
6773
task_spans = integration.task_spans if integration else False
6874

75+
if task_spans and in_span:
76+
transaction = sentry_sdk.continue_trace(
77+
headers, op="task", name="downstream"
78+
)
79+
ctx = sentry_sdk.start_transaction(transaction)
80+
else:
81+
ctx = nullcontext()
82+
6983
with sentry_sdk.isolation_scope():
70-
with (
71-
sentry_sdk.start_span(
72-
op=OP.FUNCTION,
73-
name=get_name(coro),
74-
origin=AsyncioIntegration.origin,
75-
)
76-
if task_spans
77-
else nullcontext()
78-
):
84+
with ctx:
7985
try:
8086
result = await coro
8187
except StopAsyncIteration as e:

0 commit comments

Comments
 (0)