Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ api/
build/
treblle_test_upload.egg-info/
.DS_Store
.idea
16 changes: 12 additions & 4 deletions treblle/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from django.conf import settings
from django.urls import resolve
from functools import cached_property
Expand Down Expand Up @@ -67,6 +68,9 @@ def default(self, obj):


class TreblleMiddleware(object):
async_capable = True
sync_capable = True

# Class-level cached server info (computed once, shared across instances)
_server_info_cache = None
_cache_lock = threading.Lock()
Expand Down Expand Up @@ -316,6 +320,10 @@ def cleanup_resources(cls):

def __init__(self, get_response):
self.get_response = get_response

# Run as coroutine if response is a coroutine, to avoid bottlenecks
if iscoroutinefunction(self.get_response):
markcoroutinefunction(self)

# Initialize instance variables for thread safety
self.start_time = None
Expand Down Expand Up @@ -381,22 +389,22 @@ def create_payload_structure(self):
}
}

def __call__(self, request):
async def __call__(self, request):
"""
Default function to handle requests and responses
"""
if not self.is_valid:
return self.get_response(request)
return await self.get_response(request)

# Check if route should be excluded from tracking
if self.should_skip_route(request.path_info):
if self.treblle_debug:
self.treblle_print(f"Skipping route: {request.path_info}")
return self.get_response(request)
return await self.get_response(request)

self.start_time = time.time()
request_body = request.body
response = self.get_response(request)
response = await self.get_response(request)
self.end_time = time.time()

# Skip tracking redirect responses (301, 302, etc.) to avoid duplicate entries
Expand Down