changeset 7906:5240fbde6ddb

wsgi: reintroduce the "wrapper" middleware for logging request timing, but guarded by optional use_wsgi_wrapper=true
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 19 Oct 2019 22:53:21 +0200
parents 948db4f4254f
children b42ee1bdf082
files kallithea/config/app_cfg.py kallithea/lib/middleware/wrapper.py
diffstat 2 files changed, 27 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/config/app_cfg.py	Thu Oct 31 21:15:14 2019 +0100
+++ b/kallithea/config/app_cfg.py	Sat Oct 19 22:53:21 2019 +0200
@@ -39,6 +39,7 @@
 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 @@
         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
 
 
--- a/kallithea/lib/middleware/wrapper.py	Thu Oct 31 21:15:14 2019 +0100
+++ b/kallithea/lib/middleware/wrapper.py	Sat Oct 19 22:53:21 2019 +0200
@@ -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.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 @@
         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