Skip to content

Commit 5d21d1d

Browse files
authored
feat(asyncio): Allow to turn task spans off (#5367)
### Description Allow to turn off task spans in asyncio integration #### Issues Closes #5366 #### Reminders - Please add tests to validate your changes, and lint your code using `tox -e linters`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent 7b380c6 commit 5d21d1d

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

sentry_sdk/integrations/asyncio.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sentry_sdk
55
from sentry_sdk.consts import OP
66
from sentry_sdk.integrations import Integration, DidNotEnable
7+
from sentry_sdk.integrations._wsgi_common import nullcontext
78
from sentry_sdk.utils import event_from_exception, logger, reraise
89

910
try:
@@ -60,11 +61,20 @@ def _sentry_task_factory(
6061
async def _task_with_sentry_span_creation() -> "Any":
6162
result = None
6263

64+
integration = sentry_sdk.get_client().get_integration(
65+
AsyncioIntegration
66+
)
67+
task_spans = integration.task_spans if integration else False
68+
6369
with sentry_sdk.isolation_scope():
64-
with sentry_sdk.start_span(
65-
op=OP.FUNCTION,
66-
name=get_name(coro),
67-
origin=AsyncioIntegration.origin,
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()
6878
):
6979
try:
7080
result = await coro
@@ -140,6 +150,9 @@ class AsyncioIntegration(Integration):
140150
identifier = "asyncio"
141151
origin = f"auto.function.{identifier}"
142152

153+
def __init__(self, task_spans: bool = True) -> None:
154+
self.task_spans = task_spans
155+
143156
@staticmethod
144157
def setup_once() -> None:
145158
patch_asyncio()

tests/integrations/asyncio/test_asyncio.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,67 @@ async def test_span_origin(
395395
assert event["spans"][0]["origin"] == "auto.function.asyncio"
396396

397397

398+
@minimum_python_38
399+
@pytest.mark.asyncio
400+
async def test_task_spans_false(
401+
sentry_init,
402+
capture_events,
403+
uninstall_integration,
404+
):
405+
uninstall_integration("asyncio")
406+
407+
sentry_init(
408+
traces_sample_rate=1.0,
409+
integrations=[
410+
AsyncioIntegration(task_spans=False),
411+
],
412+
)
413+
414+
events = capture_events()
415+
416+
with sentry_sdk.start_transaction(name="test_no_spans"):
417+
tasks = [asyncio.create_task(foo()), asyncio.create_task(bar())]
418+
await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
419+
420+
sentry_sdk.flush()
421+
422+
(transaction_event,) = events
423+
424+
assert not transaction_event["spans"]
425+
426+
427+
@minimum_python_38
428+
@pytest.mark.asyncio
429+
async def test_enable_asyncio_integration_with_task_spans_false(
430+
sentry_init,
431+
capture_events,
432+
uninstall_integration,
433+
):
434+
"""
435+
Test that enable_asyncio_integration() helper works with task_spans=False.
436+
"""
437+
uninstall_integration("asyncio")
438+
439+
sentry_init(traces_sample_rate=1.0)
440+
441+
assert "asyncio" not in sentry_sdk.get_client().integrations
442+
443+
enable_asyncio_integration(task_spans=False)
444+
445+
assert "asyncio" in sentry_sdk.get_client().integrations
446+
assert sentry_sdk.get_client().integrations["asyncio"].task_spans is False
447+
448+
events = capture_events()
449+
450+
with sentry_sdk.start_transaction(name="test"):
451+
await asyncio.create_task(foo())
452+
453+
sentry_sdk.flush()
454+
455+
(transaction,) = events
456+
assert not transaction["spans"]
457+
458+
398459
@minimum_python_38
399460
@pytest.mark.asyncio
400461
async def test_delayed_enable_integration(sentry_init, capture_events):

0 commit comments

Comments
 (0)