Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions sentry_sdk/integrations/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
61 changes: 61 additions & 0 deletions tests/integrations/asyncio/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading