Add setting to enable profiling of all requests.

This is useful for the occasional case where we cannot figure out what
is causing a particular problem, but it can be easily reproduced on
staging.

(imported from commit 8b51184a8b686814f2c6ff103ba355538463ceb0)
This commit is contained in:
Tim Abbott 2013-11-18 12:55:19 -05:00
parent e2c388c49c
commit d44c6636c6
2 changed files with 17 additions and 0 deletions

View File

@ -12,6 +12,7 @@ from zerver.exceptions import RateLimited
import logging
import time
import cProfile
logger = logging.getLogger('zulip.requests')
@ -21,11 +22,15 @@ def record_request_stop_data(log_data):
log_data['memcached_requests_stopped'] = get_memcached_requests()
log_data['bugdown_time_stopped'] = get_bugdown_time()
log_data['bugdown_requests_stopped'] = get_bugdown_requests()
if settings.PROFILE_ALL_REQUESTS:
log_data["prof"].disable()
def async_request_stop(request):
record_request_stop_data(request._log_data)
def record_request_restart_data(log_data):
if settings.PROFILE_ALL_REQUESTS:
log_data["prof"].enable()
log_data['time_restarted'] = time.time()
log_data['memcached_time_restarted'] = get_memcached_time()
log_data['memcached_requests_restarted'] = get_memcached_requests()
@ -36,6 +41,10 @@ def async_request_restart(request):
record_request_restart_data(request._log_data)
def record_request_start_data(log_data):
if settings.PROFILE_ALL_REQUESTS:
log_data["prof"] = cProfile.Profile()
log_data["prof"].enable()
log_data['time_started'] = time.time()
log_data['memcached_time_start'] = get_memcached_time()
log_data['memcached_requests_start'] = get_memcached_requests()
@ -144,6 +153,11 @@ def write_log_line(log_data, path, method, remote_ip, email, client_name,
if time_delta >= 1:
queue_json_publish("slow_queries", "%s (%s)" % (logger_timing, email), lambda e: None)
if settings.PROFILE_ALL_REQUESTS:
log_data["prof"].disable()
profile_path = "/tmp/profile.data.%s.%s" % (path.split("/")[-1], int(time_delta * 1000),)
log_data["prof"].dump_stats(profile_path)
# Log some additional data whenever we return certain 40x errors
if 400 <= status_code < 500 and status_code not in [401, 404, 405]:
if len(error_content) > 100:

View File

@ -760,3 +760,6 @@ FILE_UPLOAD_MAX_MEMORY_SIZE = 0
# for running the tests, or you will need to ensure that embedly_client.is_supported()
# gets called before the tests run.
USING_EMBEDLY = False
# This is a debugging option only
PROFILE_ALL_REQUESTS = False