|
7 | 7 |
|
8 | 8 | import sentry_sdk |
9 | 9 | from sentry_sdk.consts import OP |
10 | | -from sentry_sdk.integrations.asyncio import AsyncioIntegration, patch_asyncio |
| 10 | +from sentry_sdk.integrations.asyncio import ( |
| 11 | + AsyncioIntegration, |
| 12 | + patch_asyncio, |
| 13 | + enable_asyncio_integration, |
| 14 | +) |
11 | 15 |
|
12 | 16 | try: |
13 | 17 | from contextvars import Context, ContextVar |
@@ -229,6 +233,7 @@ def test_patch_asyncio(mock_get_running_loop): |
229 | 233 | Test that the patch_asyncio function will patch the task factory. |
230 | 234 | """ |
231 | 235 | mock_loop = mock_get_running_loop.return_value |
| 236 | + mock_loop.get_task_factory.return_value._is_sentry_task_factory = False |
232 | 237 |
|
233 | 238 | patch_asyncio() |
234 | 239 |
|
@@ -278,6 +283,7 @@ def test_sentry_task_factory_with_factory(mock_get_running_loop): |
278 | 283 |
|
279 | 284 | # The original task factory will be mocked out here, let's retrieve the value for later |
280 | 285 | orig_task_factory = mock_loop.get_task_factory.return_value |
| 286 | + orig_task_factory._is_sentry_task_factory = False |
281 | 287 |
|
282 | 288 | # Retieve sentry task factory (since it is an inner function within patch_asyncio) |
283 | 289 | sentry_task_factory = get_sentry_task_factory(mock_get_running_loop) |
@@ -340,6 +346,7 @@ def test_sentry_task_factory_context_with_factory(mock_get_running_loop): |
340 | 346 |
|
341 | 347 | # The original task factory will be mocked out here, let's retrieve the value for later |
342 | 348 | orig_task_factory = mock_loop.get_task_factory.return_value |
| 349 | + orig_task_factory._is_sentry_task_factory = False |
343 | 350 |
|
344 | 351 | # Retieve sentry task factory (since it is an inner function within patch_asyncio) |
345 | 352 | sentry_task_factory = get_sentry_task_factory(mock_get_running_loop) |
@@ -386,3 +393,113 @@ async def test_span_origin( |
386 | 393 |
|
387 | 394 | assert event["contexts"]["trace"]["origin"] == "manual" |
388 | 395 | assert event["spans"][0]["origin"] == "auto.function.asyncio" |
| 396 | + |
| 397 | + |
| 398 | +@minimum_python_38 |
| 399 | +@pytest.mark.asyncio |
| 400 | +async def test_delayed_enable_integration(sentry_init, capture_events): |
| 401 | + sentry_init(traces_sample_rate=1.0) |
| 402 | + |
| 403 | + assert "asyncio" not in sentry_sdk.get_client().integrations |
| 404 | + |
| 405 | + events = capture_events() |
| 406 | + |
| 407 | + with sentry_sdk.start_transaction(name="test"): |
| 408 | + await asyncio.create_task(foo()) |
| 409 | + |
| 410 | + assert len(events) == 1 |
| 411 | + (transaction,) = events |
| 412 | + assert not transaction["spans"] |
| 413 | + |
| 414 | + enable_asyncio_integration() |
| 415 | + |
| 416 | + events = capture_events() |
| 417 | + |
| 418 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 419 | + |
| 420 | + with sentry_sdk.start_transaction(name="test"): |
| 421 | + await asyncio.create_task(foo()) |
| 422 | + |
| 423 | + assert len(events) == 1 |
| 424 | + (transaction,) = events |
| 425 | + assert transaction["spans"] |
| 426 | + assert transaction["spans"][0]["origin"] == "auto.function.asyncio" |
| 427 | + |
| 428 | + |
| 429 | +@minimum_python_38 |
| 430 | +@pytest.mark.asyncio |
| 431 | +async def test_delayed_enable_integration_with_options(sentry_init, capture_events): |
| 432 | + sentry_init(traces_sample_rate=1.0) |
| 433 | + |
| 434 | + assert "asyncio" not in sentry_sdk.get_client().integrations |
| 435 | + |
| 436 | + mock_init = MagicMock(return_value=None) |
| 437 | + mock_setup_once = MagicMock() |
| 438 | + with patch( |
| 439 | + "sentry_sdk.integrations.asyncio.AsyncioIntegration.__init__", mock_init |
| 440 | + ): |
| 441 | + with patch( |
| 442 | + "sentry_sdk.integrations.asyncio.AsyncioIntegration.setup_once", |
| 443 | + mock_setup_once, |
| 444 | + ): |
| 445 | + enable_asyncio_integration("arg", kwarg="kwarg") |
| 446 | + |
| 447 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 448 | + mock_init.assert_called_once_with("arg", kwarg="kwarg") |
| 449 | + mock_setup_once.assert_called_once() |
| 450 | + |
| 451 | + |
| 452 | +@minimum_python_38 |
| 453 | +@pytest.mark.asyncio |
| 454 | +async def test_delayed_enable_enabled_integration(sentry_init, uninstall_integration): |
| 455 | + # Ensure asyncio integration is not already installed from previous tests |
| 456 | + uninstall_integration("asyncio") |
| 457 | + |
| 458 | + integration = AsyncioIntegration() |
| 459 | + sentry_init(integrations=[integration], traces_sample_rate=1.0) |
| 460 | + |
| 461 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 462 | + |
| 463 | + # Get the task factory after initial setup - it should be Sentry's |
| 464 | + loop = asyncio.get_running_loop() |
| 465 | + task_factory_before = loop.get_task_factory() |
| 466 | + assert getattr(task_factory_before, "_is_sentry_task_factory", False) is True |
| 467 | + |
| 468 | + enable_asyncio_integration() |
| 469 | + |
| 470 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 471 | + |
| 472 | + # The task factory should be the same (loop not re-patched) |
| 473 | + task_factory_after = loop.get_task_factory() |
| 474 | + assert task_factory_before is task_factory_after |
| 475 | + |
| 476 | + |
| 477 | +@minimum_python_38 |
| 478 | +@pytest.mark.asyncio |
| 479 | +async def test_delayed_enable_integration_after_disabling(sentry_init, capture_events): |
| 480 | + sentry_init(disabled_integrations=[AsyncioIntegration()], traces_sample_rate=1.0) |
| 481 | + |
| 482 | + assert "asyncio" not in sentry_sdk.get_client().integrations |
| 483 | + |
| 484 | + events = capture_events() |
| 485 | + |
| 486 | + with sentry_sdk.start_transaction(name="test"): |
| 487 | + await asyncio.create_task(foo()) |
| 488 | + |
| 489 | + assert len(events) == 1 |
| 490 | + (transaction,) = events |
| 491 | + assert not transaction["spans"] |
| 492 | + |
| 493 | + enable_asyncio_integration() |
| 494 | + |
| 495 | + events = capture_events() |
| 496 | + |
| 497 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 498 | + |
| 499 | + with sentry_sdk.start_transaction(name="test"): |
| 500 | + await asyncio.create_task(foo()) |
| 501 | + |
| 502 | + assert len(events) == 1 |
| 503 | + (transaction,) = events |
| 504 | + assert transaction["spans"] |
| 505 | + assert transaction["spans"][0]["origin"] == "auto.function.asyncio" |
0 commit comments