Add logging for slow requests

This commit is contained in:
cuom1999 2023-02-13 16:44:04 -06:00
parent fc852d1bc7
commit b95acb6202
2 changed files with 25 additions and 0 deletions

View file

@ -247,6 +247,7 @@ INSTALLED_APPS += (
) )
MIDDLEWARE = ( MIDDLEWARE = (
"judge.middleware.SlowRequestMiddleware",
"judge.middleware.ShortCircuitMiddleware", "judge.middleware.ShortCircuitMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware", "django.middleware.locale.LocaleMiddleware",

View file

@ -1,3 +1,6 @@
import time
import logging
from django.conf import settings from django.conf import settings
from django.http import HttpResponseRedirect, Http404 from django.http import HttpResponseRedirect, Http404
from django.urls import Resolver404, resolve, reverse from django.urls import Resolver404, resolve, reverse
@ -138,3 +141,24 @@ class SubdomainMiddleware(object):
status=404, status=404,
) )
return self.get_response(request) return self.get_response(request)
class SlowRequestMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
logger = logging.getLogger("judge.slow_request")
start_time = time.time()
response = self.get_response(request)
response_time = time.time() - start_time
if response_time > 9:
message = {
"message": "Slow request",
"path": request.path,
"response_time": response_time * 1000,
"method": request.method,
"profile": request.profile,
}
logger.info(message)
return response