diff --git a/sentry_sdk/integrations/asyncio.py b/sentry_sdk/integrations/asyncio.py index afaca73d33..b7aa0a7202 100644 --- a/sentry_sdk/integrations/asyncio.py +++ b/sentry_sdk/integrations/asyncio.py @@ -4,6 +4,7 @@ import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations import Integration, DidNotEnable +from sentry_sdk.integrations._wsgi_common import nullcontext from sentry_sdk.utils import event_from_exception, logger, reraise try: @@ -60,11 +61,20 @@ def _sentry_task_factory( async def _task_with_sentry_span_creation() -> "Any": result = None + integration = sentry_sdk.get_client().get_integration( + AsyncioIntegration + ) + task_spans = integration.task_spans if integration else False + with sentry_sdk.isolation_scope(): - with sentry_sdk.start_span( - op=OP.FUNCTION, - name=get_name(coro), - origin=AsyncioIntegration.origin, + with ( + sentry_sdk.start_span( + op=OP.FUNCTION, + name=get_name(coro), + origin=AsyncioIntegration.origin, + ) + if task_spans + else nullcontext() ): try: result = await coro @@ -140,6 +150,9 @@ class AsyncioIntegration(Integration): identifier = "asyncio" origin = f"auto.function.{identifier}" + def __init__(self, task_spans: bool = True) -> None: + self.task_spans = task_spans + @staticmethod def setup_once() -> None: patch_asyncio() diff --git a/tests/integrations/asyncio/test_asyncio.py b/tests/integrations/asyncio/test_asyncio.py index 58072b0071..b41aa244cb 100644 --- a/tests/integrations/asyncio/test_asyncio.py +++ b/tests/integrations/asyncio/test_asyncio.py @@ -395,6 +395,67 @@ async def test_span_origin( assert event["spans"][0]["origin"] == "auto.function.asyncio" +@minimum_python_38 +@pytest.mark.asyncio +async def test_task_spans_false( + sentry_init, + capture_events, + uninstall_integration, +): + uninstall_integration("asyncio") + + sentry_init( + traces_sample_rate=1.0, + integrations=[ + AsyncioIntegration(task_spans=False), + ], + ) + + events = capture_events() + + with sentry_sdk.start_transaction(name="test_no_spans"): + tasks = [asyncio.create_task(foo()), asyncio.create_task(bar())] + await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION) + + sentry_sdk.flush() + + (transaction_event,) = events + + assert not transaction_event["spans"] + + +@minimum_python_38 +@pytest.mark.asyncio +async def test_enable_asyncio_integration_with_task_spans_false( + sentry_init, + capture_events, + uninstall_integration, +): + """ + Test that enable_asyncio_integration() helper works with task_spans=False. + """ + uninstall_integration("asyncio") + + sentry_init(traces_sample_rate=1.0) + + assert "asyncio" not in sentry_sdk.get_client().integrations + + enable_asyncio_integration(task_spans=False) + + assert "asyncio" in sentry_sdk.get_client().integrations + assert sentry_sdk.get_client().integrations["asyncio"].task_spans is False + + events = capture_events() + + with sentry_sdk.start_transaction(name="test"): + await asyncio.create_task(foo()) + + sentry_sdk.flush() + + (transaction,) = events + assert not transaction["spans"] + + @minimum_python_38 @pytest.mark.asyncio async def test_delayed_enable_integration(sentry_init, capture_events):