Package: chocs-middleware-trace
Chocs-TraceHttp tracing middleware for chocs library.InstallationPoetry:poetryaddchocs-middleware.tracePip:pipinstallchocs-middleware.traceFeaturesAutomatic generation and propagation of tracing headers (x-request-id, x-correlation-id, x-causation-id)Sentry integrationMore intuitive log formatting optionsStructured loggingUsageSupport tracing in your responsesfromchocs_middleware.traceimportTraceMiddlewarefromchocsimportApplication,HttpRequest,HttpResponse# id_prefix will ensure generated tracing headers to contain your prefixapp=Application(TraceMiddleware(id_prefix="service-name-"))@app.get("/hello")defsay_hello(req:HttpRequest)->HttpResponse:returnHttpResponse("Hello!")# tracing middleware will automatically attach x-request-id, x-correlation-id, x-causation-id headers to your responseTracing requestsfromchocs_middleware.traceimportTraceMiddleware,HttpStrategyfromchocsimportApplication,HttpRequest,HttpResponseimportrequests# http_strategy will try to detect requests library and use it to add tracing headers in all your requests# if it fails to detect requests library it will fallback to urllib3app=Application(TraceMiddleware(http_strategy=HttpStrategy.AUTO))@app.get("/hello")defsay_hello(req:HttpRequest)->HttpResponse:requests.get("http://example.com/test")# middleware will automatically attach x-correlation-id, x-causation-id and x-request-id headers to your requestreturnHttpResponse("Hello!")# when using the requests.request method you will need to add headers for it to use, otherwise it will generate default headers and is unable to attach x-correlation-id, x-causation-id and x-request-id headers@app.get("/test")defsay_hello(req:HttpRequest,headers=req.headers)->HttpResponse:response=requests.request("GET","http://test.com/")assertresponse.content==b"ok"returnHttpResponse("OK")Using loggerfromchocsimportApplication,HttpRequest,HttpResponsefromchocs_middleware.traceimportTraceMiddleware,Loggerapp=Application(TraceMiddleware())@app.get("/hello")defsay_hello(req:HttpRequest)->HttpResponse:logger=Logger.get("logger_name")logger.info("Hello{name}!",name="Bob")# will output to log stream Hello Bob!returnHttpResponse("Hello!")Formatting messagefromchocsimportApplication,HttpRequest,HttpResponsefromchocs_middleware.traceimportTraceMiddleware,Loggerapp=Application(TraceMiddleware())@app.get("/hello")defsay_hello(req:HttpRequest)->HttpResponse:logger=Logger.get("logger_name",message_format="[{level}] {tags.request.x-correlation-id}{msg}")logger.info("Hello{name}!",name="Bob")# will output to log stream Hello Bob!returnHttpResponse("Hello!")Available formatting optionsNameExample valueDescription{level}DEBUGLog level name{msg}Example messageLog message after interpolation{log_message}Example {name}Log message before interpolation{timestamp}2022-03-07T20:06:23.453866Time of the logged message{filename}example.pyName of the python file where message was log{funcName}example_functionName of the function where message was log{module}example_moduleName of the module where message was log{pathname}example/pathPath name of the file where message was log{tags.*}some valueCustom tag value set by callingLogger.set_tagfunction