class ProfileMiddleware(object): def __init__(self, app, conf): self.app = app self.logger = get_logger(conf, log_route='profile') self.log_filename_prefix = conf.get('log_filename_prefix', DEFAULT_PROFILE_PREFIX) dirname = os.path.dirname(self.log_filename_prefix) # Notes: this effort may fail due to permission denied. # it is better to be created and authorized to current # user in advance. if not os.path.exists(dirname): os.makedirs(dirname) self.dump_interval = float(conf.get('dump_interval', 5.0)) self.dump_timestamp = config_true_value( conf.get('dump_timestamp', 'no')) self.flush_at_shutdown = config_true_value( conf.get('flush_at_shutdown', 'no')) self.path = conf.get('path', '__profile__').replace('/', '') self.unwind = config_true_value(conf.get('unwind', 'no')) self.profile_module = conf.get('profile_module', 'eventlet.green.profile') self.profiler = get_profiler(self.profile_module) self.profile_log = ProfileLog(self.log_filename_prefix, self.dump_timestamp) self.viewer = HTMLViewer(self.path, self.profile_module, self.profile_log) self.dump_pool = GreenPool(1000) self.last_dump_at = None def __del__(self): if self.flush_at_shutdown: self.profile_log.clear(str(os.getpid())) def _combine_body_qs(self, request): wsgi_input = request.environ['wsgi.input'] query_dict = request.params qs_in_body = wsgi_input.read() query_dict.update( parse_qs(qs_in_body, keep_blank_values=True, strict_parsing=False)) return query_dict def dump_checkpoint(self): current_time = time.time() if self.last_dump_at is None or self.last_dump_at +\ self.dump_interval < current_time: self.dump_pool.spawn_n(self.profile_log.dump_profile, self.profiler, os.getpid()) self.last_dump_at = current_time def __call__(self, environ, start_response): request = Request(environ) path_entry = request.path_info.split('/') # hijack favicon request sent by browser so that it doesn't # invoke profiling hook and contaminate the data. if path_entry[1] == 'favicon.ico': start_response('200 OK', []) return '' elif path_entry[1] == self.path: try: self.dump_checkpoint() query_dict = self._combine_body_qs(request) content, headers = self.viewer.render(request.url, request.method, path_entry, query_dict, self.renew_profile) start_response('200 OK', headers) return [bytes_(content)] except MethodNotAllowed as mx: start_response('405 Method Not Allowed', []) return '%s' % mx except NotFoundException as nx: start_response('404 Not Found', []) return '%s' % nx except ProfileException as pf: start_response('500 Internal Server Error', []) return '%s' % pf except Exception as ex: start_response('500 Internal Server Error', []) return _('Error on render profiling results: %s') % ex else: try: _locals = locals() code = self.unwind and PROFILE_EXEC_EAGER or\ PROFILE_EXEC_LAZY self.profiler.runctx(code, globals(), _locals) app_iter = _locals['app_iter_'] self.dump_checkpoint() return app_iter except: self.logger.exception(_('Error profiling code')) finally: pass def renew_profile(self): self.profiler = get_profiler(self.profile_module)
class ProfileMiddleware(object): def __init__(self, app, conf): self.app = app self.logger = get_logger(conf, log_route='profile') self.log_filename_prefix = conf.get('log_filename_prefix', DEFAULT_PROFILE_PREFIX) dirname = os.path.dirname(self.log_filename_prefix) # Notes: this effort may fail due to permission denied. # it is better to be created and authorized to current # user in advance. if not os.path.exists(dirname): os.makedirs(dirname) self.dump_interval = float(conf.get('dump_interval', 5.0)) self.dump_timestamp = config_true_value(conf.get( 'dump_timestamp', 'no')) self.flush_at_shutdown = config_true_value(conf.get( 'flush_at_shutdown', 'no')) self.path = conf.get('path', '__profile__').replace('/', '') self.unwind = config_true_value(conf.get('unwind', 'no')) self.profile_module = conf.get('profile_module', 'eventlet.green.profile') self.profiler = get_profiler(self.profile_module) self.profile_log = ProfileLog(self.log_filename_prefix, self.dump_timestamp) self.viewer = HTMLViewer(self.path, self.profile_module, self.profile_log) self.dump_pool = GreenPool(1000) self.last_dump_at = None def __del__(self): if self.flush_at_shutdown: self.profile_log.clear(str(os.getpid())) def _combine_body_qs(self, request): wsgi_input = request.environ['wsgi.input'] query_dict = request.params qs_in_body = wsgi_input.read() query_dict.update(parse_qs(qs_in_body, keep_blank_values=True, strict_parsing=False)) return query_dict def dump_checkpoint(self): current_time = time.time() if self.last_dump_at is None or self.last_dump_at +\ self.dump_interval < current_time: self.dump_pool.spawn_n(self.profile_log.dump_profile, self.profiler, os.getpid()) self.last_dump_at = current_time def __call__(self, environ, start_response): request = Request(environ) path_entry = request.path_info.split('/') # hijack favicon request sent by browser so that it doesn't # invoke profiling hook and contaminate the data. if path_entry[1] == 'favicon.ico': start_response('200 OK', []) return '' elif path_entry[1] == self.path: try: self.dump_checkpoint() query_dict = self._combine_body_qs(request) content, headers = self.viewer.render(request.url, request.method, path_entry, query_dict, self.renew_profile) start_response('200 OK', headers) return [bytes_(content)] except MethodNotAllowed as mx: start_response('405 Method Not Allowed', []) return '%s' % mx except NotFoundException as nx: start_response('404 Not Found', []) return '%s' % nx except ProfileException as pf: start_response('500 Internal Server Error', []) return '%s' % pf except Exception as ex: start_response('500 Internal Server Error', []) return _('Error on render profiling results: %s') % ex else: _locals = locals() code = self.unwind and PROFILE_EXEC_EAGER or\ PROFILE_EXEC_LAZY self.profiler.runctx(code, globals(), _locals) app_iter = _locals['app_iter_'] self.dump_checkpoint() return app_iter def renew_profile(self): self.profiler = get_profiler(self.profile_module)