|
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 |
@@ -386,3 +390,113 @@ async def test_span_origin( |
386 | 390 |
|
387 | 391 | assert event["contexts"]["trace"]["origin"] == "manual" |
388 | 392 | assert event["spans"][0]["origin"] == "auto.function.asyncio" |
| 393 | + |
| 394 | + |
| 395 | +@minimum_python_38 |
| 396 | +@pytest.mark.asyncio |
| 397 | +async def test_delayed_enable_integration(sentry_init, capture_events): |
| 398 | + sentry_init(traces_sample_rate=1.0) |
| 399 | + |
| 400 | + assert "asyncio" not in sentry_sdk.get_client().integrations |
| 401 | + |
| 402 | + events = capture_events() |
| 403 | + |
| 404 | + with sentry_sdk.start_transaction(name="test"): |
| 405 | + await asyncio.create_task(foo()) |
| 406 | + |
| 407 | + assert len(events) == 1 |
| 408 | + (transaction,) = events |
| 409 | + assert not transaction["spans"] |
| 410 | + |
| 411 | + enable_asyncio_integration() |
| 412 | + |
| 413 | + events = capture_events() |
| 414 | + |
| 415 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 416 | + |
| 417 | + with sentry_sdk.start_transaction(name="test"): |
| 418 | + await asyncio.create_task(foo()) |
| 419 | + |
| 420 | + assert len(events) == 1 |
| 421 | + (transaction,) = events |
| 422 | + assert transaction["spans"] |
| 423 | + assert transaction["spans"][0]["origin"] == "auto.function.asyncio" |
| 424 | + |
| 425 | + |
| 426 | +@minimum_python_38 |
| 427 | +@pytest.mark.asyncio |
| 428 | +async def test_delayed_enable_integration_with_options(sentry_init, capture_events): |
| 429 | + sentry_init(traces_sample_rate=1.0) |
| 430 | + |
| 431 | + assert "asyncio" not in sentry_sdk.get_client().integrations |
| 432 | + |
| 433 | + events = capture_events() |
| 434 | + |
| 435 | + with sentry_sdk.start_transaction(name="test"): |
| 436 | + await asyncio.create_task(foo()) |
| 437 | + |
| 438 | + assert len(events) == 1 |
| 439 | + (transaction,) = events |
| 440 | + assert not transaction["spans"] |
| 441 | + |
| 442 | + mock_init = MagicMock(return_value=None) |
| 443 | + mock_setup_once = MagicMock() |
| 444 | + with patch( |
| 445 | + "sentry_sdk.integrations.asyncio.AsyncioIntegration.__init__", mock_init |
| 446 | + ): |
| 447 | + with patch( |
| 448 | + "sentry_sdk.integrations.asyncio.AsyncioIntegration.setup_once", |
| 449 | + mock_setup_once, |
| 450 | + ): |
| 451 | + enable_asyncio_integration("arg", kwarg="kwarg") |
| 452 | + |
| 453 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 454 | + mock_init.assert_called_once_with("arg", kwarg="kwarg") |
| 455 | + mock_setup_once.assert_called_once() |
| 456 | + |
| 457 | + |
| 458 | +@minimum_python_38 |
| 459 | +@pytest.mark.asyncio |
| 460 | +async def test_delayed_enable_enabled_integration(sentry_init): |
| 461 | + sentry_init(integrations=[AsyncioIntegration()], traces_sample_rate=1.0) |
| 462 | + |
| 463 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 464 | + |
| 465 | + original_integration = sentry_sdk.get_client().integrations["asyncio"] |
| 466 | + enable_asyncio_integration() |
| 467 | + |
| 468 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 469 | + |
| 470 | + # The new asyncio integration should override the old one |
| 471 | + assert sentry_sdk.get_client().integrations["asyncio"] is not original_integration |
| 472 | + |
| 473 | + |
| 474 | +@minimum_python_38 |
| 475 | +@pytest.mark.asyncio |
| 476 | +async def test_delayed_enable_integration_after_disabling(sentry_init, capture_events): |
| 477 | + sentry_init(disabled_integrations=[AsyncioIntegration()], traces_sample_rate=1.0) |
| 478 | + |
| 479 | + assert "asyncio" not in sentry_sdk.get_client().integrations |
| 480 | + |
| 481 | + events = capture_events() |
| 482 | + |
| 483 | + with sentry_sdk.start_transaction(name="test"): |
| 484 | + await asyncio.create_task(foo()) |
| 485 | + |
| 486 | + assert len(events) == 1 |
| 487 | + (transaction,) = events |
| 488 | + assert not transaction["spans"] |
| 489 | + |
| 490 | + enable_asyncio_integration() |
| 491 | + |
| 492 | + events = capture_events() |
| 493 | + |
| 494 | + assert "asyncio" in sentry_sdk.get_client().integrations |
| 495 | + |
| 496 | + with sentry_sdk.start_transaction(name="test"): |
| 497 | + await asyncio.create_task(foo()) |
| 498 | + |
| 499 | + assert len(events) == 1 |
| 500 | + (transaction,) = events |
| 501 | + assert transaction["spans"] |
| 502 | + assert transaction["spans"][0]["origin"] == "auto.function.asyncio" |
0 commit comments