Last active
December 31, 2015 16:17
-
-
Save dplepage/9489129 to your computer and use it in GitHub Desktop.
Request-id middleware sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logbook | |
class LogRequestIDMiddleware(object): | |
def __init__(self, application): | |
self.application = application | |
def make_processor(self, request_id): | |
@logbook.Processor | |
def processor(log_record): | |
log_record.extra['request_id'] = request_id | |
return processor | |
def __call__(self, environ, start_response): | |
req_id = environ.get("HTTP_X_REQUEST_ID", "no-request-id") | |
with self.make_processor(req_id).threadbound(): | |
return self.application(environ, start_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
class RequestIDMiddleware(object): | |
def __init__(self, application): | |
self.application = application | |
def __call__(self, environ, start_response): | |
req_id = unicode(uuid.uuid4()) | |
environ["HTTP_X_REQUEST_ID"] = req_id | |
def new_start_response(status, response_headers, exc_info=None): | |
response_headers.append(("X-Request-ID", req_id)) | |
return start_response(status, response_headers, exc_info) | |
return self.application(environ, new_start_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask, logbook | |
from request_id import RequestIDMiddleware | |
from log_request_id import LogRequestIDMiddleware | |
app = flask.Flask("testapp") | |
fstr = ( | |
"{record.extra[request_id]} [{record.time:%Y-%m-%d %H:%M:%S}] {record.level_name}: " | |
"{record.channel}: {record.message}" | |
) | |
# Is there a better place to do global configuration like this? | |
logbook.StderrHandler(format_string=fstr, bubble=False).push_application() | |
@app.route("/") | |
def root(): | |
logbook.Logger("root").info("This is a message") | |
return "hi! " + flask.request.headers.get("X-Request-ID", "No Id Set!") | |
# Plain app: no request ids, logs show no request ids | |
wsgi_app = app | |
# Log app: no request ids, logs show request id of "no-request-id" | |
lid_app = LogRequestIDMiddleware(wsgi_app) | |
# Request Id app: Request ids are set, but don't appear in logs | |
rid_app = RequestIDMiddleware(wsgi_app) | |
# Full app: Request ids are set, and appear in logs | |
lrid_app = RequestIDMiddleware(LogRequestIDMiddleware(wsgi_app)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick correction:
Plain app: no request ids, logs show no request ids
wsgi_app = app.wsgi_app
Log app: no request ids, logs show request id of "no-request-id"
app.wsgi_app = LogRequestIDMiddleware(wsgi_app)
Request Id app: Request ids are set, but don't appear in logs
app.wsgi_app = RequestIDMiddleware(wsgi_app)
Full app: Request ids are set, and appear in logs
app.wsgi_app = RequestIDMiddleware(LogRequestIDMiddleware(wsgi_app))