2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-02-11 23:33:47 +01:00
|
|
|
from django.conf import settings
|
2013-04-23 18:51:17 +02:00
|
|
|
from zephyr.decorator import RequestVariableMissingError, RequestVariableConversionError
|
2012-12-19 20:19:46 +01:00
|
|
|
from zephyr.lib.response import json_error
|
2013-02-11 23:33:47 +01:00
|
|
|
from django.db import connection
|
2013-04-16 22:58:21 +02:00
|
|
|
from zephyr.lib.utils import statsd
|
2013-04-19 00:00:33 +02:00
|
|
|
from zephyr.lib.cache import get_memcached_time, get_memcached_requests
|
2013-05-21 23:59:27 +02:00
|
|
|
from zephyr.lib.bugdown import get_bugdown_time, get_bugdown_requests
|
2012-12-19 20:19:46 +01:00
|
|
|
|
2012-10-16 23:52:10 +02:00
|
|
|
import logging
|
2012-11-14 21:00:26 +01:00
|
|
|
import time
|
2012-10-16 23:52:10 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger('humbug.requests')
|
|
|
|
|
2013-04-23 19:36:50 +02:00
|
|
|
def async_request_stop(request):
|
|
|
|
request._time_stopped = time.time()
|
2013-04-19 00:00:33 +02:00
|
|
|
request._memcached_time_stopped = get_memcached_time()
|
|
|
|
request._memcached_requests_stopped = get_memcached_requests()
|
2013-05-21 23:59:27 +02:00
|
|
|
request._bugdown_time_stopped = get_bugdown_time()
|
|
|
|
request._bugdown_requests_stopped = get_bugdown_requests()
|
2013-04-23 19:36:50 +02:00
|
|
|
|
|
|
|
def async_request_restart(request):
|
|
|
|
request._time_restarted = time.time()
|
2013-04-19 00:00:33 +02:00
|
|
|
request._memcached_time_restarted = get_memcached_time()
|
|
|
|
request._memcached_requests_restarted = get_memcached_requests()
|
2013-05-21 23:59:27 +02:00
|
|
|
request._bugdown_time_restarted = get_bugdown_time()
|
|
|
|
request._bugdown_requests_restarted = get_bugdown_requests()
|
2013-04-23 19:36:50 +02:00
|
|
|
|
2012-10-16 23:52:10 +02:00
|
|
|
class LogRequests(object):
|
2012-11-14 21:00:26 +01:00
|
|
|
def process_request(self, request):
|
|
|
|
request._time_started = time.time()
|
2013-04-19 00:00:33 +02:00
|
|
|
request._memcached_time_start = get_memcached_time()
|
|
|
|
request._memcached_requests_start = get_memcached_requests()
|
2013-05-21 23:59:27 +02:00
|
|
|
request._bugdown_time_start = get_bugdown_time()
|
|
|
|
request._bugdown_requests_start = get_bugdown_requests()
|
2012-11-14 21:00:26 +01:00
|
|
|
|
2012-10-16 23:52:10 +02:00
|
|
|
def process_response(self, request, response):
|
2013-04-16 22:58:21 +02:00
|
|
|
def timedelta_ms(timedelta):
|
|
|
|
return timedelta * 1000
|
|
|
|
|
2013-03-26 17:00:30 +01:00
|
|
|
def format_timedelta(timedelta):
|
|
|
|
if (timedelta >= 1):
|
|
|
|
return "%.1fs" % (timedelta)
|
2013-04-16 22:58:21 +02:00
|
|
|
return "%.0fms" % (timedelta_ms(timedelta),)
|
|
|
|
|
|
|
|
# For statsd timer name
|
2013-05-13 16:36:21 +02:00
|
|
|
if request.path == '/':
|
2013-04-16 22:58:21 +02:00
|
|
|
statsd_path = 'webreq'
|
|
|
|
else:
|
2013-05-13 16:36:21 +02:00
|
|
|
statsd_path = "webreq.%s" % (request.path[1:].replace('/', '.'),)
|
2013-04-16 22:58:21 +02:00
|
|
|
|
2012-10-16 23:52:10 +02:00
|
|
|
|
|
|
|
# The reverse proxy might have sent us the real external IP
|
|
|
|
remote_ip = request.META.get('HTTP_X_REAL_IP')
|
|
|
|
if remote_ip is None:
|
|
|
|
remote_ip = request.META['REMOTE_ADDR']
|
|
|
|
|
2012-11-14 21:00:26 +01:00
|
|
|
time_delta = -1
|
|
|
|
# A time duration of -1 means the StartLogRequests middleware
|
|
|
|
# didn't run for some reason
|
2013-03-26 16:22:52 +01:00
|
|
|
optional_orig_delta = ""
|
2012-11-14 21:00:26 +01:00
|
|
|
if hasattr(request, '_time_started'):
|
|
|
|
time_delta = time.time() - request._time_started
|
2013-03-26 16:22:52 +01:00
|
|
|
if hasattr(request, "_time_stopped"):
|
|
|
|
orig_time_delta = time_delta
|
|
|
|
time_delta = ((request._time_stopped - request._time_started) +
|
|
|
|
(time.time() - request._time_restarted))
|
|
|
|
optional_orig_delta = " (lp: %s)" % (format_timedelta(orig_time_delta),)
|
2013-04-19 00:00:33 +02:00
|
|
|
memcached_output = ""
|
|
|
|
if hasattr(request, '_memcached_time_start'):
|
|
|
|
memcached_time_delta = get_memcached_time() - request._memcached_time_start
|
|
|
|
memcached_count_delta = get_memcached_requests() - request._memcached_requests_start
|
|
|
|
if hasattr(request, "_memcached_requests_stopped"):
|
|
|
|
# (now - restarted) + (stopped - start) = (now - start) + (stopped - restarted)
|
|
|
|
memcached_time_delta += (request._memcached_time_stopped -
|
|
|
|
request._memcached_time_restarted)
|
|
|
|
memcached_count_delta += (request._memcached_requests_stopped -
|
|
|
|
request._memcached_requests_restarted)
|
|
|
|
|
|
|
|
if (memcached_time_delta > 0.005):
|
|
|
|
memcached_output = " (mem: %s/%s)" % (format_timedelta(memcached_time_delta),
|
|
|
|
memcached_count_delta)
|
2013-02-11 23:15:34 +01:00
|
|
|
|
2013-05-13 16:32:52 +02:00
|
|
|
statsd.timing("%s.memcached.time" % (statsd_path,), timedelta_ms(memcached_time_delta))
|
2013-05-14 15:31:04 +02:00
|
|
|
statsd.incr("%s.memcached.querycount" % (statsd_path,), memcached_count_delta)
|
2013-05-13 16:32:52 +02:00
|
|
|
|
2013-05-21 23:59:27 +02:00
|
|
|
bugdown_output = ""
|
|
|
|
if hasattr(request, '_bugdown_time_start'):
|
|
|
|
bugdown_time_delta = get_bugdown_time() - request._bugdown_time_start
|
|
|
|
bugdown_count_delta = get_bugdown_requests() - request._bugdown_requests_start
|
|
|
|
if hasattr(request, "_bugdown_requests_stopped"):
|
|
|
|
# (now - restarted) + (stopped - start) = (now - start) + (stopped - restarted)
|
|
|
|
bugdown_time_delta += (request._bugdown_time_stopped -
|
|
|
|
request._bugdown_time_restarted)
|
|
|
|
bugdown_count_delta += (request._bugdown_requests_stopped -
|
|
|
|
request._bugdown_requests_restarted)
|
|
|
|
|
|
|
|
if (bugdown_time_delta > 0.005):
|
|
|
|
bugdown_output = " (md: %s/%s)" % (format_timedelta(bugdown_time_delta),
|
|
|
|
bugdown_count_delta)
|
|
|
|
|
2013-05-31 20:09:51 +02:00
|
|
|
statsd.timing("%s.markdown.time" % (statsd_path,), timedelta_ms(bugdown_time_delta))
|
|
|
|
statsd.incr("%s.markdown.count" % (statsd_path,), bugdown_count_delta)
|
|
|
|
|
2013-02-11 23:33:47 +01:00
|
|
|
# Get the amount of time spent doing database queries
|
2013-03-26 16:51:10 +01:00
|
|
|
db_time_output = ""
|
|
|
|
if len(connection.queries) > 0:
|
|
|
|
query_time = sum(float(query.get('time', 0)) for query in connection.queries)
|
|
|
|
db_time_output = " (db: %s/%sq)" % (format_timedelta(query_time),
|
|
|
|
len(connection.queries))
|
2013-02-11 23:33:47 +01:00
|
|
|
|
2013-04-16 22:58:21 +02:00
|
|
|
# Log ms, db ms, and num queries to statsd
|
2013-05-01 19:12:53 +02:00
|
|
|
statsd.timing("%s.dbtime" % (statsd_path,), timedelta_ms(query_time))
|
2013-04-16 22:58:21 +02:00
|
|
|
statsd.incr("%s.dbq" % (statsd_path, ), len(connection.queries))
|
|
|
|
statsd.timing("%s.total" % (statsd_path,), timedelta_ms(time_delta))
|
|
|
|
|
2013-03-15 18:57:58 +01:00
|
|
|
# Get the requestor's email address and client, if available.
|
2013-02-11 23:15:34 +01:00
|
|
|
try:
|
|
|
|
email = request._email
|
|
|
|
except Exception:
|
|
|
|
email = "unauth"
|
2013-03-15 18:57:58 +01:00
|
|
|
try:
|
2013-03-26 19:40:28 +01:00
|
|
|
client = request.client.name
|
2013-03-15 18:57:58 +01:00
|
|
|
except Exception:
|
|
|
|
client = "?"
|
2013-02-11 23:15:34 +01:00
|
|
|
|
2013-05-21 23:59:27 +02:00
|
|
|
logger.info('%-15s %-7s %3d %5s%s%s%s%s %s (%s via %s)' %
|
2013-03-26 17:00:30 +01:00
|
|
|
(remote_ip, request.method, response.status_code,
|
2013-03-26 16:22:52 +01:00
|
|
|
format_timedelta(time_delta), optional_orig_delta,
|
2013-05-21 23:59:27 +02:00
|
|
|
memcached_output, bugdown_output,
|
2013-03-26 16:51:10 +01:00
|
|
|
db_time_output, request.get_full_path(), email, client))
|
2013-02-12 17:26:12 +01:00
|
|
|
|
2013-02-12 22:04:37 +01:00
|
|
|
# Log some additional data whenever we return certain 40x errors
|
|
|
|
if 400 <= response.status_code < 500 and response.status_code not in [401, 404, 405]:
|
|
|
|
content = response.content
|
|
|
|
if len(content) > 100:
|
|
|
|
content = "[content more than 100 characters]"
|
|
|
|
logger.info('status=%3d, data=%s, uid=%s' % (response.status_code, content, email))
|
2013-04-16 22:58:21 +02:00
|
|
|
|
2012-10-16 23:52:10 +02:00
|
|
|
return response
|
2012-12-19 20:19:46 +01:00
|
|
|
|
|
|
|
class JsonErrorHandler(object):
|
|
|
|
def process_exception(self, request, exception):
|
|
|
|
if hasattr(exception, 'to_json_error_msg') and callable(exception.to_json_error_msg):
|
|
|
|
return json_error(exception.to_json_error_msg())
|
|
|
|
return None
|
2013-02-12 17:26:12 +01:00
|
|
|
|
|
|
|
# Monkeypatch in time tracking to the Django non-debug cursor
|
|
|
|
# Code comes from CursorDebugWrapper
|
|
|
|
def wrapper_execute(self, action, sql, params=()):
|
|
|
|
self.set_dirty()
|
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
return action(sql, params)
|
|
|
|
finally:
|
|
|
|
stop = time.time()
|
|
|
|
duration = stop - start
|
|
|
|
self.db.queries.append({
|
|
|
|
'time': "%.3f" % duration,
|
|
|
|
})
|
|
|
|
|
|
|
|
from django.db.backends.util import CursorWrapper
|
|
|
|
def cursor_execute(self, sql, params=()):
|
|
|
|
return wrapper_execute(self, self.cursor.execute, sql, params)
|
|
|
|
CursorWrapper.execute = cursor_execute
|
|
|
|
|
|
|
|
def cursor_executemany(self, sql, params=()):
|
|
|
|
return wrapper_execute(self, self.cursor.executemany, sql, params)
|
|
|
|
CursorWrapper.executemany = cursor_executemany
|