Skip to content

Commit 06381de

Browse files
Add db.system data to Django db span data (#2040)
--------- Co-authored-by: Anton Pirker <[email protected]>
1 parent 40bef90 commit 06381de

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import weakref
77

88
from sentry_sdk._types import TYPE_CHECKING
9-
from sentry_sdk.consts import OP
9+
from sentry_sdk.consts import OP, SPANDATA
1010
from sentry_sdk.hub import Hub, _should_send_default_pii
1111
from sentry_sdk.scope import add_global_event_processor
1212
from sentry_sdk.serializer import add_global_repr_processor
@@ -64,6 +64,7 @@
6464
from django.http.request import QueryDict
6565
from django.utils.datastructures import MultiValueDict
6666

67+
from sentry_sdk.tracing import Span
6768
from sentry_sdk.scope import Scope
6869
from sentry_sdk.integrations.wsgi import _ScopedResponse
6970
from sentry_sdk._types import Event, Hint, EventProcessor, NotImplementedType
@@ -578,7 +579,8 @@ def execute(self, sql, params=None):
578579

579580
with record_sql_queries(
580581
hub, self.cursor, sql, params, paramstyle="format", executemany=False
581-
):
582+
) as span:
583+
_set_db_system_on_span(span, self.db.vendor)
582584
return real_execute(self, sql, params)
583585

584586
def executemany(self, sql, param_list):
@@ -589,7 +591,8 @@ def executemany(self, sql, param_list):
589591

590592
with record_sql_queries(
591593
hub, self.cursor, sql, param_list, paramstyle="format", executemany=True
592-
):
594+
) as span:
595+
_set_db_system_on_span(span, self.db.vendor)
593596
return real_executemany(self, sql, param_list)
594597

595598
def connect(self):
@@ -601,10 +604,18 @@ def connect(self):
601604
with capture_internal_exceptions():
602605
hub.add_breadcrumb(message="connect", category="query")
603606

604-
with hub.start_span(op=OP.DB, description="connect"):
607+
with hub.start_span(op=OP.DB, description="connect") as span:
608+
_set_db_system_on_span(span, self.vendor)
605609
return real_connect(self)
606610

607611
CursorWrapper.execute = execute
608612
CursorWrapper.executemany = executemany
609613
BaseDatabaseWrapper.connect = connect
610614
ignore_logger("django.db.backends")
615+
616+
617+
# https://github.com/django/django/blob/6a0dc2176f4ebf907e124d433411e52bba39a28e/django/db/backends/base/base.py#L29
618+
# Avaliable in Django 1.8+
619+
def _set_db_system_on_span(span, vendor):
620+
# type: (Span, str) -> None
621+
span.set_data(SPANDATA.DB_SYSTEM, vendor)

tests/integrations/django/test_basic.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from sentry_sdk._compat import PY2, PY310
2020
from sentry_sdk import capture_message, capture_exception, configure_scope
21+
from sentry_sdk.consts import SPANDATA
2122
from sentry_sdk.integrations.django import DjangoIntegration
2223
from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
2324
from sentry_sdk.integrations.executing import ExecutingIntegration
@@ -447,7 +448,14 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t
447448
content, status, headers = client.get(reverse("postgres_select"))
448449
assert status == "200 OK"
449450

450-
assert '- op="db": description="connect"' in render_span_tree(events[0])
451+
(event,) = events
452+
453+
for span in event["spans"]:
454+
if span.get("op") == "db":
455+
data = span.get("data")
456+
assert data.get(SPANDATA.DB_SYSTEM) == "postgresql"
457+
458+
assert '- op="db": description="connect"' in render_span_tree(event)
451459

452460

453461
@pytest.mark.forked

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ deps =
166166
py3.8-common: hypothesis
167167

168168
linters: -r linter-requirements.txt
169+
linters: werkzeug<2.3.0
169170

170171
# Common
171172
{py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-common: pytest-asyncio
@@ -503,6 +504,8 @@ basepython =
503504
linters: python3.11
504505

505506
commands =
507+
{py3.7,py3.8}-boto3: pip install urllib3<2.0.0
508+
506509
; https://github.com/pytest-dev/pytest/issues/5532
507510
{py3.5,py3.6,py3.7,py3.8,py3.9}-flask-v{0.11,0.12}: pip install pytest<5
508511
{py3.6,py3.7,py3.8,py3.9}-flask-v{0.11}: pip install Werkzeug<2

0 commit comments

Comments
 (0)