Add general+trackable logging to every request and add fine grain logging to the index and registry for determining possible performance regressions
This commit is contained in:
parent
a5a61576ae
commit
4da49da730
3 changed files with 122 additions and 18 deletions
|
@ -1,7 +1,9 @@
|
|||
import logging
|
||||
import logging.config
|
||||
import uuid
|
||||
|
||||
from app import app as application
|
||||
from flask import request, Request
|
||||
|
||||
from data.model import db as model_db
|
||||
|
||||
|
@ -19,6 +21,7 @@ from endpoints.callbacks import callback
|
|||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
profile = logging.getLogger('application.profiler')
|
||||
|
||||
application.register_blueprint(web)
|
||||
application.register_blueprint(callback, url_prefix='/oauth2')
|
||||
|
@ -29,6 +32,27 @@ application.register_blueprint(api_bp, url_prefix='/api')
|
|||
application.register_blueprint(webhooks, url_prefix='/webhooks')
|
||||
application.register_blueprint(realtime, url_prefix='/realtime')
|
||||
|
||||
class RequestWithId(Request):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(RequestWithId, self).__init__(*args, **kwargs)
|
||||
self.request_id = uuid.uuid4()
|
||||
|
||||
@application.before_request
|
||||
def _request_start():
|
||||
profile.debug('Starting request: %s', request.path)
|
||||
|
||||
|
||||
@application.after_request
|
||||
def _request_end(r):
|
||||
profile.debug('Ending request: %s', request.path)
|
||||
return r
|
||||
|
||||
class InjectingFilter(logging.Filter):
|
||||
def filter(self, record):
|
||||
record.msg = '[request_id = %s] %s' % (request.request_id, record.msg)
|
||||
return True
|
||||
|
||||
profile.addFilter(InjectingFilter())
|
||||
|
||||
def close_db(exc):
|
||||
db = model_db
|
||||
|
@ -37,11 +61,8 @@ def close_db(exc):
|
|||
db.close()
|
||||
|
||||
application.teardown_request(close_db)
|
||||
application.request_class = RequestWithId
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.config.fileConfig('conf/logging_local.conf', disable_existing_loggers=False)
|
||||
|
||||
profile = logging.getLogger('application.profiler')
|
||||
profile.debug('This is a profiling statement')
|
||||
|
||||
application.run(port=5000, debug=True, threaded=True, host='0.0.0.0')
|
||||
logging.config.fileConfig('conf/logging_local.conf', disable_existing_loggers=False)
|
||||
application.run(port=5000, debug=True, threaded=True, host='0.0.0.0')
|
||||
|
|
Reference in a new issue