|
5 | 5 | from unittest import mock |
6 | 6 |
|
7 | 7 | from django import VERSION as DJANGO_VERSION |
8 | | -from django.db import connections |
| 8 | +from django.db import connection, connections, transaction |
9 | 9 |
|
10 | 10 | try: |
11 | 11 | from django.urls import reverse |
|
15 | 15 | from werkzeug.test import Client |
16 | 16 |
|
17 | 17 | from sentry_sdk import start_transaction |
18 | | -from sentry_sdk.consts import SPANDATA |
| 18 | +from sentry_sdk.consts import SPANDATA, DBOPERATION |
19 | 19 | from sentry_sdk.integrations.django import DjangoIntegration |
20 | 20 | from sentry_sdk.tracing_utils import record_sql_queries |
21 | 21 |
|
@@ -481,6 +481,7 @@ def test_db_span_origin_execute(sentry_init, client, capture_events): |
481 | 481 | assert event["contexts"]["trace"]["origin"] == "auto.http.django" |
482 | 482 |
|
483 | 483 | for span in event["spans"]: |
| 484 | + print("span is", span["op"], span["description"]) |
484 | 485 | if span["op"] == "db": |
485 | 486 | assert span["origin"] == "auto.db.django" |
486 | 487 | else: |
@@ -524,3 +525,190 @@ def test_db_span_origin_executemany(sentry_init, client, capture_events): |
524 | 525 |
|
525 | 526 | assert event["contexts"]["trace"]["origin"] == "manual" |
526 | 527 | assert event["spans"][0]["origin"] == "auto.db.django" |
| 528 | + |
| 529 | + commit_spans = [ |
| 530 | + span |
| 531 | + for span in event["spans"] |
| 532 | + if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT |
| 533 | + ] |
| 534 | + assert len(commit_spans) == 1 |
| 535 | + commit_span = commit_spans[0] |
| 536 | + assert commit_span["origin"] == "auto.db.django" |
| 537 | + |
| 538 | + |
| 539 | +@pytest.mark.forked |
| 540 | +@pytest_mark_django_db_decorator(transaction=True) |
| 541 | +def test_db_no_autocommit_execute(sentry_init, client, capture_events): |
| 542 | + """ |
| 543 | + Verify we record a breadcrumb when opening a new database. |
| 544 | + """ |
| 545 | + sentry_init( |
| 546 | + integrations=[DjangoIntegration()], |
| 547 | + traces_sample_rate=1.0, |
| 548 | + ) |
| 549 | + |
| 550 | + if "postgres" not in connections: |
| 551 | + pytest.skip("postgres tests disabled") |
| 552 | + |
| 553 | + # trigger Django to open a new connection by marking the existing one as None. |
| 554 | + connections["postgres"].connection = None |
| 555 | + |
| 556 | + events = capture_events() |
| 557 | + |
| 558 | + client.get(reverse("postgres_select_orm_no_autocommit")) |
| 559 | + |
| 560 | + (event,) = events |
| 561 | + |
| 562 | + assert event["contexts"]["trace"]["origin"] == "auto.http.django" |
| 563 | + |
| 564 | + for span in event["spans"]: |
| 565 | + if span["op"] == "db": |
| 566 | + assert span["origin"] == "auto.db.django" |
| 567 | + else: |
| 568 | + assert span["origin"] == "auto.http.django" |
| 569 | + |
| 570 | + commit_spans = [ |
| 571 | + span |
| 572 | + for span in event["spans"] |
| 573 | + if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT |
| 574 | + ] |
| 575 | + assert len(commit_spans) == 1 |
| 576 | + commit_span = commit_spans[0] |
| 577 | + assert commit_span["origin"] == "auto.db.django" |
| 578 | + |
| 579 | + |
| 580 | +@pytest.mark.forked |
| 581 | +@pytest_mark_django_db_decorator(transaction=True) |
| 582 | +def test_db_no_autocommit_executemany(sentry_init, client, capture_events): |
| 583 | + sentry_init( |
| 584 | + integrations=[DjangoIntegration()], |
| 585 | + traces_sample_rate=1.0, |
| 586 | + ) |
| 587 | + |
| 588 | + events = capture_events() |
| 589 | + |
| 590 | + if "postgres" not in connections: |
| 591 | + pytest.skip("postgres tests disabled") |
| 592 | + |
| 593 | + with start_transaction(name="test_transaction"): |
| 594 | + from django.db import connection, transaction |
| 595 | + |
| 596 | + cursor = connection.cursor() |
| 597 | + |
| 598 | + query = """UPDATE auth_user SET username = %s where id = %s;""" |
| 599 | + query_list = ( |
| 600 | + ( |
| 601 | + "test1", |
| 602 | + 1, |
| 603 | + ), |
| 604 | + ( |
| 605 | + "test2", |
| 606 | + 2, |
| 607 | + ), |
| 608 | + ) |
| 609 | + cursor.executemany(query, query_list) |
| 610 | + |
| 611 | + transaction.commit() |
| 612 | + |
| 613 | + (event,) = events |
| 614 | + |
| 615 | + assert event["contexts"]["trace"]["origin"] == "manual" |
| 616 | + assert event["spans"][0]["origin"] == "auto.db.django" |
| 617 | + |
| 618 | + commit_spans = [ |
| 619 | + span |
| 620 | + for span in event["spans"] |
| 621 | + if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT |
| 622 | + ] |
| 623 | + assert len(commit_spans) == 1 |
| 624 | + commit_span = commit_spans[0] |
| 625 | + assert commit_span["origin"] == "auto.db.django" |
| 626 | + |
| 627 | + |
| 628 | +@pytest.mark.forked |
| 629 | +@pytest_mark_django_db_decorator(transaction=True) |
| 630 | +def test_db_atomic_execute(sentry_init, client, capture_events): |
| 631 | + """ |
| 632 | + Verify we record a breadcrumb when opening a new database. |
| 633 | + """ |
| 634 | + sentry_init( |
| 635 | + integrations=[DjangoIntegration()], |
| 636 | + send_default_pii=True, |
| 637 | + traces_sample_rate=1.0, |
| 638 | + ) |
| 639 | + |
| 640 | + if "postgres" not in connections: |
| 641 | + pytest.skip("postgres tests disabled") |
| 642 | + |
| 643 | + # trigger Django to open a new connection by marking the existing one as None. |
| 644 | + connections["postgres"].connection = None |
| 645 | + |
| 646 | + events = capture_events() |
| 647 | + |
| 648 | + with transaction.atomic(): |
| 649 | + client.get(reverse("postgres_select_orm_atomic")) |
| 650 | + connections["postgres"].commit() |
| 651 | + |
| 652 | + (event,) = events |
| 653 | + |
| 654 | + assert event["contexts"]["trace"]["origin"] == "auto.http.django" |
| 655 | + |
| 656 | + commit_spans = [ |
| 657 | + span |
| 658 | + for span in event["spans"] |
| 659 | + if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT |
| 660 | + ] |
| 661 | + assert len(commit_spans) == 1 |
| 662 | + commit_span = commit_spans[0] |
| 663 | + assert commit_span["origin"] == "auto.db.django" |
| 664 | + |
| 665 | + |
| 666 | +@pytest.mark.forked |
| 667 | +@pytest_mark_django_db_decorator(transaction=True) |
| 668 | +def test_db_atomic_executemany(sentry_init, client, capture_events): |
| 669 | + """ |
| 670 | + Verify we record a breadcrumb when opening a new database. |
| 671 | + """ |
| 672 | + sentry_init( |
| 673 | + integrations=[DjangoIntegration()], |
| 674 | + send_default_pii=True, |
| 675 | + traces_sample_rate=1.0, |
| 676 | + ) |
| 677 | + |
| 678 | + if "postgres" not in connections: |
| 679 | + pytest.skip("postgres tests disabled") |
| 680 | + |
| 681 | + # trigger Django to open a new connection by marking the existing one as None. |
| 682 | + connections["postgres"].connection = None |
| 683 | + |
| 684 | + events = capture_events() |
| 685 | + |
| 686 | + with start_transaction(name="test_transaction"): |
| 687 | + with transaction.atomic(): |
| 688 | + cursor = connection.cursor() |
| 689 | + |
| 690 | + query = """UPDATE auth_user SET username = %s where id = %s;""" |
| 691 | + query_list = ( |
| 692 | + ( |
| 693 | + "test1", |
| 694 | + 1, |
| 695 | + ), |
| 696 | + ( |
| 697 | + "test2", |
| 698 | + 2, |
| 699 | + ), |
| 700 | + ) |
| 701 | + cursor.executemany(query, query_list) |
| 702 | + |
| 703 | + (event,) = events |
| 704 | + |
| 705 | + assert event["contexts"]["trace"]["origin"] == "manual" |
| 706 | + |
| 707 | + commit_spans = [ |
| 708 | + span |
| 709 | + for span in event["spans"] |
| 710 | + if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT |
| 711 | + ] |
| 712 | + assert len(commit_spans) == 1 |
| 713 | + commit_span = commit_spans[0] |
| 714 | + assert commit_span["origin"] == "auto.db.django" |
0 commit comments