-
Notifications
You must be signed in to change notification settings - Fork 1
/
requestid.py
36 lines (31 loc) · 1.16 KB
/
requestid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from flask import make_response, request, g, has_app_context, \
has_request_context
from functools import wraps, update_wrapper
from logging import Filter
from uuid import uuid4
# Filter to add request ID to logger
class RequestIdFilter(Filter):
def filter(self, record):
if has_app_context():
if getattr(g, 'request_id', None):
record.request_id = g.request_id
else:
headers = request.headers
fetch_id = headers['X-Request-Id'] if has_request_context() \
and 'X-Request-Id' in headers.keys() else uuid4()
record.request_id = fetch_id
g.request_id = fetch_id
else:
record.request_id = 'INTERNAL'
return True
# Decorator to add request id to request output
def requestid(view):
@wraps(view)
def request_id(*args, **kwargs):
response = make_response(view(*args, **kwargs))
id = request.headers.get('X-Request-Id') or \
getattr(g, 'request_id', None)
if id:
response.headers['X-Request-Id'] = id
return response
return update_wrapper(request_id, view)