In my Django application, I want to count the number of queries made by each endpoint. I want to run this in production, so I cannot rely on any of the techniques I found with regular Django SQL logging that is only enabled when DEBUG=True, like adding a SQL logger or relying on django.db.connection.queries
. Is there a context manager that works similar to self.assertNumQueries()
in test cases that can be run in production like this?
def my_view(request):
with count_sql_queries() as nr_queries:
response = business_logic()
logger.debug(f'There were {nr_queries} queries made')
return response