Skip to content

Commit

Permalink
asynchornise instrumentaion internally in panel logic rather than mid…
Browse files Browse the repository at this point in the history
…dleware
  • Loading branch information
salty-ivy committed Aug 27, 2024
1 parent 1c39baa commit 7e1993f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import socket
from functools import lru_cache

from asgiref.sync import iscoroutinefunction, markcoroutinefunction, sync_to_async
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from django.conf import settings
from django.utils.module_loading import import_string

Expand Down Expand Up @@ -112,8 +112,8 @@ async def __acall__(self, request):

# Activate instrumentation ie. monkey-patch.
for panel in toolbar.enabled_panels:
if panel.panel_id == "SQLPanel":
await sync_to_async(panel.enable_instrumentation)()
if hasattr(panel, "aenable_instrumentation"):
await panel.aenable_instrumentation()
else:
panel.enable_instrumentation()
try:
Expand Down
3 changes: 3 additions & 0 deletions debug_toolbar/panels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ def enable_instrumentation(self):
Unless the toolbar or this panel is disabled, this method will be
called early in ``DebugToolbarMiddleware``. It should be idempotent.
Add aenable_instrumaentation to the Panel class to enable async
instrumentation.
"""

def disable_instrumentation(self):
Expand Down
8 changes: 8 additions & 0 deletions debug_toolbar/panels/sql/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import defaultdict
from copy import copy

from asgiref.sync import sync_to_async
from django.db import connections
from django.urls import path
from django.utils.translation import gettext_lazy as _, ngettext
Expand Down Expand Up @@ -192,6 +193,13 @@ def get_urls(cls):
path("sql_profile/", views.sql_profile, name="sql_profile"),
]

async def aenable_instrumentation(self):
"""
Async version of enable_instrumentation.
For async-capable panels that has async logic for instrumentation
"""
await sync_to_async(self.enable_instrumentation)()

def enable_instrumentation(self):
# This is thread-safe because database connections are thread-local.
for connection in connections.all():
Expand Down

0 comments on commit 7e1993f

Please sign in to comment.