Skip to content

Commit 66c8991

Browse files
committed
move variable transformation to transform encoding util
1 parent d0a94bd commit 66c8991

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

elasticapm/contrib/django/client.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import django
3535
from django.conf import settings as django_settings
3636
from django.db import DatabaseError
37-
from django.db.models.query import QuerySet
3837
from django.http import HttpRequest
3938

4039
try:
@@ -196,14 +195,6 @@ def capture(self, event_type, request=None, **kwargs):
196195

197196
return result
198197

199-
@staticmethod
200-
def _disable_querysets_evaluation(var):
201-
"""Overriding queryset result cache to avoid making any queries"""
202-
if isinstance(var, QuerySet):
203-
var._result_cache = []
204-
return var
205-
206-
207198
def _get_stack_info_for_trace(
208199
self,
209200
frames,
@@ -214,13 +205,6 @@ def _get_stack_info_for_trace(
214205
):
215206
"""If the stacktrace originates within the elasticapm module, it will skip
216207
frames until some other module comes up."""
217-
218-
# Overriding processor func for django to avoid making queries to db through QuerySet objects
219-
if locals_processor_func is not None:
220-
locals_processor_func = lambda v: self._disable_querysets_evaluation(locals_processor_func(v))
221-
else:
222-
locals_processor_func = lambda v: self._disable_querysets_evaluation(v)
223-
224208
return list(
225209
iterate_with_template_sources(
226210
frames,

elasticapm/utils/encoding.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import uuid
3737
from decimal import Decimal
3838

39+
from django.db.models import QuerySet
3940
from elasticapm.conf.constants import KEYWORD_MAX_LENGTH, LABEL_RE, LABEL_TYPES, LONG_FIELD_MAX_LENGTH
4041

4142
PROTECTED_TYPES = (int, type(None), float, Decimal, datetime.datetime, datetime.date, datetime.time)
@@ -144,6 +145,9 @@ class value_type(list):
144145
ret = float(value)
145146
elif isinstance(value, int):
146147
ret = int(value)
148+
elif isinstance(value, QuerySet):
149+
value._result_cache = []
150+
ret = repr(value)
147151
elif value is not None:
148152
try:
149153
ret = transform(repr(value))

tests/contrib/django/django_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,21 @@ def test_capture_post_errors_dict(client, django_elasticapm_client):
13181318
assert error["context"]["request"]["body"] == "[REDACTED]"
13191319

13201320

1321+
@pytest.mark.parametrize(
1322+
"django_sending_elasticapm_client",
1323+
[{"capture_body": "errors"}, {"capture_body": "transactions"}, {"capture_body": "all"}, {"capture_body": "off"}],
1324+
indirect=True,
1325+
)
1326+
def test_capture_django_orm_timeout_error(client, django_sending_elasticapm_client):
1327+
with pytest.raises(DatabaseError):
1328+
client.get(reverse("elasticapm-django-orm-exc"))
1329+
1330+
errors = django_sending_elasticapm_client.httpserver.payloads
1331+
if django_sending_elasticapm_client.config.capture_body in (constants.ERROR, "all"):
1332+
stacktrace = errors[0][1]["error"]["exception"]["stacktrace"]
1333+
assert "'qs': '[]'" in str(stacktrace)
1334+
1335+
13211336
def test_capture_body_config_is_dynamic_for_errors(client, django_elasticapm_client):
13221337
django_elasticapm_client.config.update(version="1", capture_body="all")
13231338
with pytest.raises(MyException):

tests/contrib/django/testapp/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def handler500(request):
6262
re_path(r"^trigger-500-ioerror$", views.raise_ioerror, name="elasticapm-raise-ioerror"),
6363
re_path(r"^trigger-500-decorated$", views.decorated_raise_exc, name="elasticapm-raise-exc-decor"),
6464
re_path(r"^trigger-500-django$", views.django_exc, name="elasticapm-django-exc"),
65+
re_path(r"^trigger-500-django-orm-exc$", views.django_queryset_error, name="elasticapm-django-orm-exc"),
6566
re_path(r"^trigger-500-template$", views.template_exc, name="elasticapm-template-exc"),
6667
re_path(r"^trigger-500-log-request$", views.logging_request_exc, name="elasticapm-log-request-exc"),
6768
re_path(r"^streaming$", views.streaming_view, name="elasticapm-streaming-view"),

tests/contrib/django/testapp/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
from django.http import HttpResponse, StreamingHttpResponse
3838
from django.shortcuts import get_object_or_404, render
3939
from django.views import View
40+
from django.db.models import QuerySet
41+
from django.db import DatabaseError
4042

4143
import elasticapm
4244

@@ -70,6 +72,18 @@ def django_exc(request):
7072
return get_object_or_404(MyException, pk=1)
7173

7274

75+
def django_queryset_error(request):
76+
"""Simulation of django ORM timeout"""
77+
class CustomQuerySet(QuerySet):
78+
def all(self):
79+
raise DatabaseError()
80+
81+
def __repr__(self) -> str:
82+
return str(self._result_cache)
83+
84+
qs = CustomQuerySet()
85+
list(qs.all())
86+
7387
def raise_exc(request):
7488
raise MyException(request.GET.get("message", "view exception"))
7589

0 commit comments

Comments
 (0)