Changeset - 5240fbde6ddb
[Not reviewed]
default
0 2 0
Mads Kiilerich (kiilerix) - 6 years ago 2019-10-19 20:53:21
mads@kiilerich.com
Grafted from: 8e257159269a
wsgi: reintroduce the "wrapper" middleware for logging request timing, but guarded by optional use_wsgi_wrapper=true
2 files changed with 27 insertions and 8 deletions:
0 comments (0 inline, 0 general) First comment
kallithea/config/app_cfg.py
Show inline comments
 
@@ -39,6 +39,7 @@ from kallithea.lib.middleware.https_fixu
 
from kallithea.lib.middleware.permanent_repo_url import PermanentRepoUrl
 
from kallithea.lib.middleware.simplegit import SimpleGit
 
from kallithea.lib.middleware.simplehg import SimpleHg
 
from kallithea.lib.middleware.wrapper import RequestWrapper
 
from kallithea.lib.utils import check_git_version, load_rcextensions, make_ui, set_app_settings, set_indexer_config, set_vcs_config
 
from kallithea.lib.utils2 import str2bool
 

	
 
@@ -199,6 +200,11 @@ def setup_application(app):
 
        app = HttpsFixup(app, config)
 

	
 
    app = PermanentRepoUrl(app, config)
 

	
 
    # Optional and undocumented wrapper - gives more verbose request/response logging, but has a slight overhead
 
    if str2bool(config.get('use_wsgi_wrapper')):
 
        app = RequestWrapper(app, config)
 

	
 
    return app
 

	
 

	
kallithea/lib/middleware/wrapper.py
Show inline comments
 
@@ -15,7 +15,7 @@
 
kallithea.lib.middleware.wrapper
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

	
 
request time measuring app
 
Wrap app to measure request and response time ... until the response starts.
 

	
 
This file was forked by the Kallithea project in July 2014.
 
Original author and date, and relevant copyright and licensing information is below:
 
@@ -32,6 +32,18 @@ from kallithea.lib.base import _get_acce
 
from kallithea.lib.utils2 import safe_unicode
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
class Meter:
 

	
 
    def __init__(self):
 
        self._start = time.time()
 

	
 
    def duration(self):
 
        return time.time() - self._start
 

	
 

	
 
class RequestWrapper(object):
 

	
 
    def __init__(self, app, config):
 
@@ -39,12 +51,13 @@ class RequestWrapper(object):
 
        self.config = config
 

	
 
    def __call__(self, environ, start_response):
 
        start = time.time()
 
        meter = Meter()
 
        description = "Request from %s for %s" % (
 
            _get_ip_addr(environ),
 
            safe_unicode(_get_access_path(environ)),
 
        )
 
        try:
 
            return self.application(environ, start_response)
 
            result = self.application(environ, start_response)
 
        finally:
 
            log = logging.getLogger('kallithea.' + self.__class__.__name__)
 
            log.info('IP: %s Request to %s time: %.3fs' % (
 
                _get_ip_addr(environ),
 
                safe_unicode(_get_access_path(environ)), time.time() - start)
 
            )
 
            log.info("%s responding after %.3fs", description, meter.duration())
 
        return result
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now