Skip to content

feat(django): Add db.system #2040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import weakref

from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.consts import OP
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk.scope import add_global_event_processor
from sentry_sdk.serializer import add_global_repr_processor
Expand Down Expand Up @@ -64,6 +64,7 @@
from django.http.request import QueryDict
from django.utils.datastructures import MultiValueDict

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

with record_sql_queries(
hub, self.cursor, sql, params, paramstyle="format", executemany=False
):
) as span:
_set_db_system_on_span(span, self.db.vendor)
return real_execute(self, sql, params)

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

with record_sql_queries(
hub, self.cursor, sql, param_list, paramstyle="format", executemany=True
):
) as span:
_set_db_system_on_span(span, self.db.vendor)
return real_executemany(self, sql, param_list)

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

with hub.start_span(op=OP.DB, description="connect"):
with hub.start_span(op=OP.DB, description="connect") as span:
_set_db_system_on_span(span, self.vendor)
return real_connect(self)

CursorWrapper.execute = execute
CursorWrapper.executemany = executemany
BaseDatabaseWrapper.connect = connect
ignore_logger("django.db.backends")


# https://github.com/django/django/blob/6a0dc2176f4ebf907e124d433411e52bba39a28e/django/db/backends/base/base.py#L29
# Avaliable in Django 1.8+
def _set_db_system_on_span(span, vendor):
# type: (Span, str) -> None
span.set_data(SPANDATA.DB_SYSTEM, vendor)
10 changes: 9 additions & 1 deletion tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from sentry_sdk._compat import PY2, PY310
from sentry_sdk import capture_message, capture_exception, configure_scope
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
from sentry_sdk.integrations.executing import ExecutingIntegration
Expand Down Expand Up @@ -447,7 +448,14 @@ def test_django_connect_trace(sentry_init, client, capture_events, render_span_t
content, status, headers = client.get(reverse("postgres_select"))
assert status == "200 OK"

assert '- op="db": description="connect"' in render_span_tree(events[0])
(event,) = events

for span in event["spans"]:
if span.get("op") == "db":
data = span.get("data")
assert data.get(SPANDATA.DB_SYSTEM) == "postgresql"

assert '- op="db": description="connect"' in render_span_tree(event)


@pytest.mark.forked
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ deps =
py3.8-common: hypothesis

linters: -r linter-requirements.txt
linters: werkzeug<2.3.0

# Common
{py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-common: pytest-asyncio
Expand Down Expand Up @@ -503,6 +504,8 @@ basepython =
linters: python3.11

commands =
{py3.7,py3.8}-boto3: pip install urllib3<2.0.0

; https://github.com/pytest-dev/pytest/issues/5532
{py3.5,py3.6,py3.7,py3.8,py3.9}-flask-v{0.11,0.12}: pip install pytest<5
{py3.6,py3.7,py3.8,py3.9}-flask-v{0.11}: pip install Werkzeug<2
Expand Down