Skip to content

Commit 3c4127c

Browse files
.
1 parent e397f65 commit 3c4127c

File tree

3 files changed

+289
-223
lines changed

3 files changed

+289
-223
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ def _set_db_data(span, cursor_or_db, db_operation=None):
715715
vendor = db.vendor
716716
span.set_data(SPANDATA.DB_SYSTEM, vendor)
717717

718+
if db_operation is not None:
719+
span.set_data(SPANDATA.DB_OPERATION, db_operation)
720+
718721
# Some custom backends override `__getattr__`, making it look like `cursor_or_db`
719722
# actually has a `connection` and the `connection` has a `get_dsn_parameters`
720723
# attribute, only to throw an error once you actually want to call it.
@@ -748,9 +751,6 @@ def _set_db_data(span, cursor_or_db, db_operation=None):
748751
if db_name is not None:
749752
span.set_data(SPANDATA.DB_NAME, db_name)
750753

751-
if db_operation is not None:
752-
span.set_data(SPANDATA.DB_OPERATION, db_operation)
753-
754754
server_address = connection_params.get("host")
755755
if server_address is not None:
756756
span.set_data(SPANDATA.SERVER_ADDRESS, server_address)

tests/integrations/django/test_db_query_data.py

Lines changed: 2 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from unittest import mock
66

77
from django import VERSION as DJANGO_VERSION
8-
from django.db import connection, connections, transaction
9-
from django.contrib.auth.models import User
8+
from django.db import connections
109

1110
try:
1211
from django.urls import reverse
@@ -16,7 +15,7 @@
1615
from werkzeug.test import Client
1716

1817
from sentry_sdk import start_transaction
19-
from sentry_sdk.consts import SPANDATA, DBOPERATION
18+
from sentry_sdk.consts import SPANDATA
2019
from sentry_sdk.integrations.django import DjangoIntegration
2120
from sentry_sdk.tracing_utils import record_sql_queries
2221

@@ -525,220 +524,3 @@ def test_db_span_origin_executemany(sentry_init, client, capture_events):
525524

526525
assert event["contexts"]["trace"]["origin"] == "manual"
527526
assert event["spans"][0]["origin"] == "auto.db.django"
528-
529-
530-
@pytest.mark.forked
531-
@pytest_mark_django_db_decorator(transaction=True)
532-
def test_db_no_autocommit_execute(sentry_init, client, capture_events):
533-
sentry_init(
534-
integrations=[DjangoIntegration()],
535-
traces_sample_rate=1.0,
536-
)
537-
538-
if "postgres" not in connections:
539-
pytest.skip("postgres tests disabled")
540-
541-
# trigger Django to open a new connection by marking the existing one as None.
542-
connections["postgres"].connection = None
543-
544-
events = capture_events()
545-
546-
client.get(reverse("postgres_insert_orm_no_autocommit"))
547-
548-
(event,) = events
549-
550-
# Ensure operation is persisted
551-
assert User.objects.using("postgres").exists()
552-
553-
assert event["contexts"]["trace"]["origin"] == "auto.http.django"
554-
555-
for span in event["spans"]:
556-
if span["op"] == "db":
557-
assert span["origin"] == "auto.db.django"
558-
else:
559-
assert span["origin"] == "auto.http.django"
560-
561-
commit_spans = [
562-
span
563-
for span in event["spans"]
564-
if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT
565-
]
566-
assert len(commit_spans) == 1
567-
commit_span = commit_spans[0]
568-
assert commit_span["origin"] == "auto.db.django"
569-
570-
571-
@pytest.mark.forked
572-
@pytest_mark_django_db_decorator(transaction=True)
573-
def test_db_no_autocommit_executemany(sentry_init, client, capture_events):
574-
sentry_init(
575-
integrations=[DjangoIntegration()],
576-
traces_sample_rate=1.0,
577-
)
578-
579-
events = capture_events()
580-
581-
if "postgres" not in connections:
582-
pytest.skip("postgres tests disabled")
583-
584-
with start_transaction(name="test_transaction"):
585-
from django.db import connection, transaction
586-
587-
cursor = connection.cursor()
588-
589-
query = """INSERT INTO auth_user (
590-
password,
591-
is_superuser,
592-
username,
593-
first_name,
594-
last_name,
595-
email,
596-
is_staff,
597-
is_active,
598-
date_joined
599-
)
600-
VALUES ('password', false, %s, %s, %s, %s, false, true, %s);"""
601-
602-
query_list = (
603-
(
604-
"user1",
605-
"John",
606-
"Doe",
607-
608-
datetime(1970, 1, 1),
609-
),
610-
(
611-
"user2",
612-
"Max",
613-
"Mustermann",
614-
615-
datetime(1970, 1, 1),
616-
),
617-
)
618-
619-
transaction.set_autocommit(False)
620-
cursor.executemany(query, query_list)
621-
transaction.commit()
622-
transaction.set_autocommit(True)
623-
624-
(event,) = events
625-
626-
# Ensure operation is persisted
627-
assert User.objects.exists()
628-
629-
assert event["contexts"]["trace"]["origin"] == "manual"
630-
assert event["spans"][0]["origin"] == "auto.db.django"
631-
632-
commit_spans = [
633-
span
634-
for span in event["spans"]
635-
if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT
636-
]
637-
assert len(commit_spans) == 1
638-
commit_span = commit_spans[0]
639-
assert commit_span["origin"] == "auto.db.django"
640-
641-
642-
@pytest.mark.forked
643-
@pytest_mark_django_db_decorator(transaction=True)
644-
def test_db_atomic_execute(sentry_init, client, capture_events):
645-
sentry_init(
646-
integrations=[DjangoIntegration()],
647-
send_default_pii=True,
648-
traces_sample_rate=1.0,
649-
)
650-
651-
if "postgres" not in connections:
652-
pytest.skip("postgres tests disabled")
653-
654-
# trigger Django to open a new connection by marking the existing one as None.
655-
connections["postgres"].connection = None
656-
657-
events = capture_events()
658-
659-
client.get(reverse("postgres_insert_orm_atomic"))
660-
661-
(event,) = events
662-
663-
# Ensure operation is persisted
664-
assert User.objects.using("postgres").exists()
665-
666-
assert event["contexts"]["trace"]["origin"] == "auto.http.django"
667-
668-
commit_spans = [
669-
span
670-
for span in event["spans"]
671-
if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT
672-
]
673-
assert len(commit_spans) == 1
674-
commit_span = commit_spans[0]
675-
assert commit_span["origin"] == "auto.db.django"
676-
677-
678-
@pytest.mark.forked
679-
@pytest_mark_django_db_decorator(transaction=True)
680-
def test_db_atomic_executemany(sentry_init, client, capture_events):
681-
sentry_init(
682-
integrations=[DjangoIntegration()],
683-
send_default_pii=True,
684-
traces_sample_rate=1.0,
685-
)
686-
687-
if "postgres" not in connections:
688-
pytest.skip("postgres tests disabled")
689-
690-
# trigger Django to open a new connection by marking the existing one as None.
691-
connections["postgres"].connection = None
692-
693-
events = capture_events()
694-
695-
with start_transaction(name="test_transaction"):
696-
with transaction.atomic():
697-
cursor = connection.cursor()
698-
699-
query = """INSERT INTO auth_user (
700-
password,
701-
is_superuser,
702-
username,
703-
first_name,
704-
last_name,
705-
email,
706-
is_staff,
707-
is_active,
708-
date_joined
709-
)
710-
VALUES ('password', false, %s, %s, %s, %s, false, true, %s);"""
711-
712-
query_list = (
713-
(
714-
"user1",
715-
"John",
716-
"Doe",
717-
718-
datetime(1970, 1, 1),
719-
),
720-
(
721-
"user2",
722-
"Max",
723-
"Mustermann",
724-
725-
datetime(1970, 1, 1),
726-
),
727-
)
728-
cursor.executemany(query, query_list)
729-
730-
(event,) = events
731-
732-
# Ensure operation is persisted
733-
assert User.objects.exists()
734-
735-
assert event["contexts"]["trace"]["origin"] == "manual"
736-
737-
commit_spans = [
738-
span
739-
for span in event["spans"]
740-
if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT
741-
]
742-
assert len(commit_spans) == 1
743-
commit_span = commit_spans[0]
744-
assert commit_span["origin"] == "auto.db.django"

0 commit comments

Comments
 (0)