2012-07-28 13:16:18 -07:00
|
|
|
from django.conf import settings
|
|
|
|
from utils import log as logging
|
2022-09-11 21:25:03 -04:00
|
|
|
from apps.statistics.rstats import round_time
|
|
|
|
import pickle
|
|
|
|
import base64
|
2021-07-15 17:03:51 -04:00
|
|
|
import time
|
2022-09-11 21:25:03 -04:00
|
|
|
import redis
|
2012-07-28 13:16:18 -07:00
|
|
|
|
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
|
|
|
]
|
|
|
|
|
2022-09-11 21:54:06 -04:00
|
|
|
RECORD_SLOW_REQUESTS_ABOVE_SECONDS = 10
|
|
|
|
|
2012-07-28 13:16:18 -07: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:
|
2020-06-07 08:04:23 -04:00
|
|
|
request_data = request.POST or request.GET
|
2021-02-24 12:07:44 -05:00
|
|
|
request_items = dict(request_data).items()
|
|
|
|
if request_items:
|
2021-07-15 18:59:30 -04:00
|
|
|
request_items_str = f"{dict(request_items)}"
|
2022-05-16 17:21:46 -04:00
|
|
|
if len(request_items_str) > 500:
|
2021-07-15 18:59:30 -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
|
|
|
|
2021-07-15 16:50:54 -04:00
|
|
|
def process_response(self, request, response):
|
|
|
|
if hasattr(request, 'sql_times_elapsed'):
|
2021-12-06 10:49:37 -05:00
|
|
|
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'],
|
|
|
|
)
|
2022-03-04 12:49:01 -05:00
|
|
|
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,
|
2021-07-15 17:03:51 -04:00
|
|
|
self.color_db(request.sql_times_elapsed['sql'], '~FY'),
|
2021-07-15 16:50:54 -04:00
|
|
|
request.sql_times_elapsed['sql'],
|
2021-07-15 17:03:51 -04:00
|
|
|
self.color_db(request.sql_times_elapsed['mongo'], '~FM'),
|
2021-07-15 16:50:54 -04:00
|
|
|
request.sql_times_elapsed['mongo'],
|
2021-08-03 16:05:48 -04:00
|
|
|
redis_log
|
2021-07-15 16:50:54 -04:00
|
|
|
))
|
|
|
|
|
2021-07-15 17:03:51 -04:00
|
|
|
if hasattr(request, 'start_time'):
|
|
|
|
seconds = time.time() - request.start_time
|
2022-09-11 21:54:06 -04:00
|
|
|
if seconds > RECORD_SLOW_REQUESTS_ABOVE_SECONDS:
|
2022-09-11 21:25:03 -04:00
|
|
|
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":
|
2022-09-11 21:54:06 -04:00
|
|
|
data_string = ' '.join([f"{key}={value}" for key, value in request.GET.items()])
|
2022-09-11 21:25:03 -04:00
|
|
|
elif request.method == "GET":
|
2022-09-11 21:54:06 -04:00
|
|
|
data_string = ' '.join([f"{key}={value}" for key, value in request.POST.items()])
|
2022-09-11 21:25:03 -04:00
|
|
|
data = {
|
|
|
|
"user_id": user_id,
|
|
|
|
"time": round(seconds, 2),
|
|
|
|
"path": request.path,
|
|
|
|
"method": request.method,
|
|
|
|
"data": data_string,
|
|
|
|
}
|
|
|
|
pipe.lpush(name, base64.b64encode(pickle.dumps(data)).decode('utf-8'))
|
|
|
|
pipe.expire(name, 60*60*12) # 12 hours
|
|
|
|
pipe.execute()
|
|
|
|
|
|
|
|
return response
|
2021-07-15 17:03:51 -04:00
|
|
|
|
|
|
|
def color_db(self, seconds, default):
|
|
|
|
color = default
|
2021-07-15 17:05:58 -04:00
|
|
|
if seconds >= .25:
|
2021-07-15 17:03:51 -04:00
|
|
|
color = '~SB~FR'
|
2021-07-15 17:05:58 -04:00
|
|
|
elif seconds > .1:
|
2021-07-15 17:03:51 -04:00
|
|
|
color = '~FW'
|
2022-03-04 12:51:43 -05:00
|
|
|
# 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):
|
2021-07-15 16:50:54 -04:00
|
|
|
response = None
|
|
|
|
if hasattr(self, 'process_request'):
|
|
|
|
response = self.process_request(request)
|
|
|
|
if not response:
|
|
|
|
response = self.get_response(request)
|
|
|
|
if hasattr(self, 'process_response'):
|
|
|
|
response = self.process_response(request, response)
|
2020-06-17 03:24:16 -04:00
|
|
|
|
2020-06-30 12:34:59 -04:00
|
|
|
return response
|