From b95acb620289340ead8be0f347586edc6bd8d4c8 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Mon, 13 Feb 2023 16:44:04 -0600 Subject: [PATCH] Add logging for slow requests --- dmoj/settings.py | 1 + judge/middleware.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/dmoj/settings.py b/dmoj/settings.py index b445574..fbf8cbd 100644 --- a/dmoj/settings.py +++ b/dmoj/settings.py @@ -247,6 +247,7 @@ INSTALLED_APPS += ( ) MIDDLEWARE = ( + "judge.middleware.SlowRequestMiddleware", "judge.middleware.ShortCircuitMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.locale.LocaleMiddleware", diff --git a/judge/middleware.py b/judge/middleware.py index 19e579e..ba44b02 100644 --- a/judge/middleware.py +++ b/judge/middleware.py @@ -1,3 +1,6 @@ +import time +import logging + from django.conf import settings from django.http import HttpResponseRedirect, Http404 from django.urls import Resolver404, resolve, reverse @@ -138,3 +141,24 @@ class SubdomainMiddleware(object): status=404, ) 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