Skip to content

Commit d0a94bd

Browse files
committed
avoid making extensive queries by overriding queryset cache
1 parent 8ca7422 commit d0a94bd

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

elasticapm/contrib/django/client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
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
3738
from django.http import HttpRequest
3839

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

196197
return result
197198

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+
198207
def _get_stack_info_for_trace(
199208
self,
200209
frames,
@@ -205,6 +214,13 @@ def _get_stack_info_for_trace(
205214
):
206215
"""If the stacktrace originates within the elasticapm module, it will skip
207216
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+
208224
return list(
209225
iterate_with_template_sources(
210226
frames,

0 commit comments

Comments
 (0)