|
27 | 27 |
|
28 | 28 | from asyncpg import connect, Connection |
29 | 29 |
|
30 | | -from sentry_sdk import capture_message |
| 30 | +from sentry_sdk import capture_message, start_transaction |
31 | 31 | from sentry_sdk.integrations.asyncpg import AsyncPGIntegration |
| 32 | +from sentry_sdk.consts import SPANDATA |
32 | 33 |
|
33 | 34 |
|
34 | 35 | PG_CONNECTION_URI = "postgresql://{}:{}@{}/{}".format( |
@@ -460,3 +461,85 @@ async def test_connection_pool(sentry_init, capture_events) -> None: |
460 | 461 | "type": "default", |
461 | 462 | }, |
462 | 463 | ] |
| 464 | + |
| 465 | + |
| 466 | +@pytest.mark.asyncio |
| 467 | +@pytest.mark.parametrize("enable_db_query_source", [None, False]) |
| 468 | +async def test_query_source_disabled( |
| 469 | + sentry_init, capture_events, enable_db_query_source |
| 470 | +): |
| 471 | + sentry_options = { |
| 472 | + "integrations": [AsyncPGIntegration()], |
| 473 | + "enable_tracing": True, |
| 474 | + } |
| 475 | + if enable_db_query_source is not None: |
| 476 | + sentry_options["enable_db_query_source"] = enable_db_query_source |
| 477 | + sentry_options["db_query_source_threshold_ms"] = 0 |
| 478 | + |
| 479 | + sentry_init(**sentry_options) |
| 480 | + |
| 481 | + events = capture_events() |
| 482 | + |
| 483 | + with start_transaction(name="test_transaction", sampled=True): |
| 484 | + conn: Connection = await connect(PG_CONNECTION_URI) |
| 485 | + |
| 486 | + await conn.execute( |
| 487 | + "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')", |
| 488 | + ) |
| 489 | + |
| 490 | + await conn.close() |
| 491 | + |
| 492 | + (event,) = events |
| 493 | + |
| 494 | + span = event["spans"][-1] |
| 495 | + assert span["description"].startswith("INSERT INTO") |
| 496 | + |
| 497 | + data = span.get("data", {}) |
| 498 | + |
| 499 | + assert SPANDATA.CODE_LINENO not in data |
| 500 | + assert SPANDATA.CODE_NAMESPACE not in data |
| 501 | + assert SPANDATA.CODE_FILEPATH not in data |
| 502 | + assert SPANDATA.CODE_FUNCTION not in data |
| 503 | + |
| 504 | + |
| 505 | +@pytest.mark.asyncio |
| 506 | +async def test_query_source(sentry_init, capture_events): |
| 507 | + sentry_init( |
| 508 | + integrations=[AsyncPGIntegration()], |
| 509 | + enable_tracing=True, |
| 510 | + enable_db_query_source=True, |
| 511 | + db_query_source_threshold_ms=0, |
| 512 | + ) |
| 513 | + |
| 514 | + events = capture_events() |
| 515 | + |
| 516 | + with start_transaction(name="test_transaction", sampled=True): |
| 517 | + conn: Connection = await connect(PG_CONNECTION_URI) |
| 518 | + |
| 519 | + await conn.execute( |
| 520 | + "INSERT INTO users(name, password, dob) VALUES ('Alice', 'secret', '1990-12-25')", |
| 521 | + ) |
| 522 | + |
| 523 | + await conn.close() |
| 524 | + |
| 525 | + (event,) = events |
| 526 | + |
| 527 | + span = event["spans"][-1] |
| 528 | + assert span["description"].startswith("INSERT INTO") |
| 529 | + |
| 530 | + data = span.get("data", {}) |
| 531 | + |
| 532 | + assert SPANDATA.CODE_LINENO in data |
| 533 | + assert SPANDATA.CODE_NAMESPACE in data |
| 534 | + assert SPANDATA.CODE_FILEPATH in data |
| 535 | + assert SPANDATA.CODE_FUNCTION in data |
| 536 | + |
| 537 | + assert type(data.get(SPANDATA.CODE_LINENO)) == int |
| 538 | + assert data.get(SPANDATA.CODE_LINENO) > 0 |
| 539 | + assert ( |
| 540 | + data.get(SPANDATA.CODE_NAMESPACE) == "tests.integrations.asyncpg.test_asyncpg" |
| 541 | + ) |
| 542 | + assert data.get(SPANDATA.CODE_FILEPATH).endswith( |
| 543 | + "tests/integrations/asyncpg/test_asyncpg.py" |
| 544 | + ) |
| 545 | + assert data.get(SPANDATA.CODE_FUNCTION) == "test_query_source" |
0 commit comments