Change the request ids to use urns. Add the logger name to the log formatter.
This commit is contained in:
parent
4da49da730
commit
4556ff52b3
3 changed files with 18 additions and 3 deletions
|
@ -4,6 +4,7 @@ import uuid
|
||||||
|
|
||||||
from app import app as application
|
from app import app as application
|
||||||
from flask import request, Request
|
from flask import request, Request
|
||||||
|
from util.names import urn_generator
|
||||||
|
|
||||||
from data.model import db as model_db
|
from data.model import db as model_db
|
||||||
|
|
||||||
|
@ -33,9 +34,11 @@ application.register_blueprint(webhooks, url_prefix='/webhooks')
|
||||||
application.register_blueprint(realtime, url_prefix='/realtime')
|
application.register_blueprint(realtime, url_prefix='/realtime')
|
||||||
|
|
||||||
class RequestWithId(Request):
|
class RequestWithId(Request):
|
||||||
|
request_gen = staticmethod(urn_generator(['request']))
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(RequestWithId, self).__init__(*args, **kwargs)
|
super(RequestWithId, self).__init__(*args, **kwargs)
|
||||||
self.request_id = uuid.uuid4()
|
self.request_id = self.request_gen()
|
||||||
|
|
||||||
@application.before_request
|
@application.before_request
|
||||||
def _request_start():
|
def _request_start():
|
||||||
|
@ -49,7 +52,7 @@ def _request_end(r):
|
||||||
|
|
||||||
class InjectingFilter(logging.Filter):
|
class InjectingFilter(logging.Filter):
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
record.msg = '[request_id = %s] %s' % (request.request_id, record.msg)
|
record.msg = '[%s] %s' % (request.request_id, record.msg)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
profile.addFilter(InjectingFilter())
|
profile.addFilter(InjectingFilter())
|
||||||
|
|
|
@ -35,5 +35,5 @@ formatter=generic
|
||||||
args=(sys.stdout, )
|
args=(sys.stdout, )
|
||||||
|
|
||||||
[formatter_generic]
|
[formatter_generic]
|
||||||
format=%(asctime)s [%(process)d] [%(levelname)s] %(message)s
|
format=%(asctime)s [%(process)d] [%(levelname)s] [%(name)s] %(message)s
|
||||||
class=logging.Formatter
|
class=logging.Formatter
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
|
||||||
def parse_namespace_repository(repository):
|
def parse_namespace_repository(repository):
|
||||||
|
@ -25,8 +26,19 @@ def parse_repository_name(f):
|
||||||
def format_robot_username(parent_username, robot_shortname):
|
def format_robot_username(parent_username, robot_shortname):
|
||||||
return '%s+%s' % (parent_username, robot_shortname)
|
return '%s+%s' % (parent_username, robot_shortname)
|
||||||
|
|
||||||
|
|
||||||
def parse_robot_username(robot_username):
|
def parse_robot_username(robot_username):
|
||||||
if not '+' in robot_username:
|
if not '+' in robot_username:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return robot_username.split('+', 2)
|
return robot_username.split('+', 2)
|
||||||
|
|
||||||
|
|
||||||
|
uuid_generator = lambda: str(uuid4())
|
||||||
|
|
||||||
|
|
||||||
|
def urn_generator(namespace_portions, id_generator=uuid_generator):
|
||||||
|
prefix = 'urn:%s:' % ':'.join(namespace_portions)
|
||||||
|
def generate_urn():
|
||||||
|
return prefix + id_generator()
|
||||||
|
return generate_urn
|
||||||
|
|
Reference in a new issue