From 21b3f4bceae40d107eb2083846512a406c98021a Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 12 Mar 2019 20:27:33 +0100 Subject: [PATCH] Use app stored on self during operation If current_app is used directly, it fails to recognize configuration changes. I spent last hour investigating it in our setup where we use Flask with uwsgi and app instance is created later, in a function, not on global level code. This means that 'current_app' pointer is instantiated before QueryInspect object is created. Our code creates QueryInspect object with app object passed to init. And during after_request() call, self.app is not current_app (more precisely: `self.app is current_app` evaluates to False). The fix is very small and I can see no reason it could break QueryInspect - actually it makes it more explicit in using app instance passed in init, instead of relying on current_app. --- flask_queryinspect.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/flask_queryinspect.py b/flask_queryinspect.py index a8fcdf5..2bd3639 100644 --- a/flask_queryinspect.py +++ b/flask_queryinspect.py @@ -18,6 +18,8 @@ class QueryInspect(object): def __init__(self, app=None): + if app is None: + app = current_app self.app = app if app is not None: self.init_app(app) @@ -36,17 +38,17 @@ def init_app(self, app): listen(Engine, 'after_cursor_execute', self.after_cursor_execute) def connect(self, dbapi_connection, connection_record): - if not current_app or not current_app.config.get('QUERYINSPECT_ENABLED'): + if not self.app or not self.app.config.get('QUERYINSPECT_ENABLED'): return stack.queryinspect['conns'] += 1 def before_cursor_execute(self, conn, cursor, statement, parameters, context, executemany): - if not current_app or not current_app.config.get('QUERYINSPECT_ENABLED'): + if not self.app or not self.app.config.get('QUERYINSPECT_ENABLED'): return stack.queryinspect['q_start'] = time.time() def after_cursor_execute(self, conn, cursor, statement, parameters, context, executemany): - if not current_app or not current_app.config.get('QUERYINSPECT_ENABLED'): + if not self.app or not self.app.config.get('QUERYINSPECT_ENABLED'): return stack.queryinspect['q_time'] += time.time() - stack.queryinspect['q_start'] if statement.lower().startswith('select'): @@ -55,7 +57,7 @@ def after_cursor_execute(self, conn, cursor, statement, parameters, context, exe stack.queryinspect['writes'] += 1 def before_request(self, *kw1, **kw2): - if not current_app or not current_app.config.get('QUERYINSPECT_ENABLED'): + if not self.app or not self.app.config.get('QUERYINSPECT_ENABLED'): return stack.queryinspect = { 'r_start': time.time(), @@ -68,7 +70,7 @@ def before_request(self, *kw1, **kw2): } def after_request(self, response, *kw1, **kw2): - if not current_app or not current_app.config.get('QUERYINSPECT_ENABLED'): + if not self.app or not self.app.config.get('QUERYINSPECT_ENABLED'): return qi = stack.queryinspect @@ -76,12 +78,12 @@ def after_request(self, response, *kw1, **kw2): qi['q_time_ms'] = round(qi['q_time'] * 1000, 1) qi['r_time_ms'] = round(qi['r_time'] * 1000, 1) - if current_app.config.get('QUERYINSPECT_LOG'): + if self.app.config.get('QUERYINSPECT_LOG'): log.info('measure#qi.r_time=%(r_time_ms).1fms, measure#qi.q_time=%(q_time_ms).1fms,' + 'count#qi.reads=%(reads)d, count#qi.writes=%(writes)d, count#qi.conns=%(conns)d', qi) - if current_app.config.get('QUERYINSPECT_HEADERS'): - if current_app.config.get('QUERYINSPECT_HEADERS_COMBINED'): + if self.app.config.get('QUERYINSPECT_HEADERS'): + if self.app.config.get('QUERYINSPECT_HEADERS_COMBINED'): combo = ('reads=%(reads)d,writes=%(writes)d,conns=%(conns)d,q_time=%(q_time_ms).1fms,' + 'r_time=%(r_time_ms).1fms') % qi response.headers['X-QueryInspect-Combined'] = combo