NewsBlur/utils/request_introspection_middleware.py

117 lines
4.5 KiB
Python
Raw Permalink Normal View History

import base64
2024-04-24 09:50:42 -04:00
import pickle
2021-07-15 17:03:51 -04:00
import time
2024-04-24 09:50:42 -04:00
import redis
2024-04-24 09:50:42 -04:00
from django.conf import settings
from apps.statistics.rstats import round_time
from utils import log as logging
2021-02-24 12:00:12 -05:00
IGNORE_PATHS = [
2021-02-24 12:01:12 -05:00
"/_haproxychk",
2021-02-24 12:00:12 -05:00
]
RECORD_SLOW_REQUESTS_ABOVE_SECONDS = 10
2024-04-24 09:43:56 -04:00
class DumpRequestMiddleware:
def process_request(self, request):
2021-02-24 12:05:19 -05:00
if settings.DEBUG and request.path not in IGNORE_PATHS:
request_data = request.POST or request.GET
2021-02-24 12:07:44 -05:00
request_items = dict(request_data).items()
if request_items:
request_items_str = f"{dict(request_items)}"
if len(request_items_str) > 500:
2024-04-24 09:43:56 -04:00
request_items_str = (
request_items_str[:100]
+ "...["
+ str(len(request_items_str) - 200)
+ " bytes]..."
+ request_items_str[-100:]
)
logging.debug(
" ---> ~FC%s ~SN~FK~BC%s~BT~ST ~FC%s~BK~FC"
% (request.method, request.path, request_items_str)
)
2021-02-24 12:05:19 -05:00
else:
2020-12-16 22:55:21 -05:00
logging.debug(" ---> ~FC%s ~SN~FK~BC%s~BT~ST" % (request.method, request.path))
2020-06-17 03:24:16 -04:00
def process_response(self, request, response):
2024-04-24 09:43:56 -04:00
if hasattr(request, "sql_times_elapsed"):
redis_log = (
"~FCuser:%s%.6f~SNs ~FCstory:%s%.6f~SNs ~FCsession:%s%.6f~SNs ~FCpubsub:%s%.6f~SNs"
% (
self.color_db(request.sql_times_elapsed["redis_user"], "~FC"),
request.sql_times_elapsed["redis_user"],
self.color_db(request.sql_times_elapsed["redis_story"], "~FC"),
request.sql_times_elapsed["redis_story"],
self.color_db(request.sql_times_elapsed["redis_session"], "~FC"),
request.sql_times_elapsed["redis_session"],
self.color_db(request.sql_times_elapsed["redis_pubsub"], "~FC"),
request.sql_times_elapsed["redis_pubsub"],
)
)
logging.user(
request,
"~SN~FCDB times ~SB~FK%s~SN~FC: ~FYsql: %s%.4f~SNs ~SN~FMmongo: %s%.5f~SNs ~SN~FCredis: %s"
% (
request.path,
self.color_db(request.sql_times_elapsed["sql"], "~FY"),
request.sql_times_elapsed["sql"],
self.color_db(request.sql_times_elapsed["mongo"], "~FM"),
request.sql_times_elapsed["mongo"],
redis_log,
),
)
2024-04-24 09:43:56 -04:00
if hasattr(request, "start_time"):
2021-07-15 17:03:51 -04:00
seconds = time.time() - request.start_time
if seconds > RECORD_SLOW_REQUESTS_ABOVE_SECONDS:
r = redis.Redis(connection_pool=settings.REDIS_STATISTICS_POOL)
pipe = r.pipeline()
minute = round_time(round_to=60)
name = f"SLOW:{minute.strftime('%s')}"
user_id = request.user.pk if request.user.is_authenticated else "0"
data_string = None
if request.method == "GET":
2024-04-24 09:43:56 -04:00
data_string = " ".join([f"{key}={value}" for key, value in request.GET.items()])
elif request.method == "GET":
2024-04-24 09:43:56 -04:00
data_string = " ".join([f"{key}={value}" for key, value in request.POST.items()])
data = {
"user_id": user_id,
"time": round(seconds, 2),
"path": request.path,
"method": request.method,
"data": data_string,
}
2024-04-24 09:43:56 -04:00
pipe.lpush(name, base64.b64encode(pickle.dumps(data)).decode("utf-8"))
pipe.expire(name, 60 * 60 * 12) # 12 hours
pipe.execute()
2024-04-24 09:43:56 -04:00
return response
2024-04-24 09:43:56 -04:00
2021-07-15 17:03:51 -04:00
def color_db(self, seconds, default):
color = default
2024-04-24 09:43:56 -04:00
if seconds >= 0.25:
color = "~SB~FR"
elif seconds > 0.1:
color = "~FW"
# elif seconds == 0:
# color = '~FK~SB'
2021-07-15 17:03:51 -04:00
return color
2020-07-01 18:38:37 -04:00
def __init__(self, get_response=None):
2020-06-17 03:24:16 -04:00
self.get_response = get_response
def __call__(self, request):
response = None
2024-04-24 09:43:56 -04:00
if hasattr(self, "process_request"):
response = self.process_request(request)
if not response:
response = self.get_response(request)
2024-04-24 09:43:56 -04:00
if hasattr(self, "process_response"):
response = self.process_response(request, response)
2020-06-17 03:24:16 -04:00
return response