Chocs-Trace

Http tracing middleware for chocs library.
Installation
Poetry:
poetry add chocs-middleware.trace
Pip:
pip install chocs-middleware.trace
Features
- Automatic generation and propagation of tracing headers (x-request-id, x-correlation-id, x-causation-id)
- Sentry integration
- More intuitive log formatting options
- Structured logging
Usage
Support tracing in your responses
from chocs_middleware.trace import TraceMiddleware
from chocs import Application, HttpRequest, HttpResponse
# id_prefix will ensure generated tracing headers to contain your prefix
app = Application(TraceMiddleware(id_prefix="service-name-"))
@app.get("/hello")
def say_hello(req: HttpRequest) -> HttpResponse:
return HttpResponse("Hello!") # tracing middleware will automatically attach x-request-id, x-correlation-id, x-causation-id headers to your response
Tracing requests
from chocs_middleware.trace import TraceMiddleware, HttpStrategy
from chocs import Application, HttpRequest, HttpResponse
import requests
# 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 urllib3
app = Application(TraceMiddleware(http_strategy=HttpStrategy.AUTO))
@app.get("/hello")
def say_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 request
return HttpResponse("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")
def say_hello(req: HttpRequest, headers=req.headers) -> HttpResponse:
response = requests.request("GET", "http://test.com/")
assert response.content == b"ok"
return HttpResponse("OK")
Using logger
from chocs import Application, HttpRequest, HttpResponse
from chocs_middleware.trace import TraceMiddleware, Logger
app = Application(TraceMiddleware())
@app.get("/hello")
def say_hello(req: HttpRequest) -> HttpResponse:
logger = Logger.get("logger_name")
logger.info("Hello {name}!", name="Bob") # will output to log stream Hello Bob!
return HttpResponse("Hello!")
Formatting message
from chocs import Application, HttpRequest, HttpResponse
from chocs_middleware.trace import TraceMiddleware, Logger
app = Application(TraceMiddleware())
@app.get("/hello")
def say_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!
return HttpResponse("Hello!")
Available formatting options
| Name | Example value | Description |
|---|---|---|
{level} |
DEBUG | Log level name |
{msg} |
Example message | Log message after interpolation |
{log_message} |
Example {name} | Log message before interpolation |
{timestamp} |
2022-03-07T20:06:23.453866 | Time of the logged message |
{filename} |
example.py | Name of the python file where message was log |
{funcName} |
example_function | Name of the function where message was log |
{module} |
example_module | Name of the module where message was log |
{pathname} |
example/path | Path name of the file where message was log |
{tags.*} |
some value | Custom tag value set by calling Logger.set_tag function |
Metadata
Release files for chocs-middleware-trace 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| chocs_middleware_trace-0.4.3.tar.gz | 6.1 kB | Without provenance | Details for chocs_middleware_trace-0.4.3.tar.gz |
Release files / chocs_middleware_trace-0.4.3.tar.gz
| Download URL | chocs_middleware_trace-0.4.3.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe19bd104e9ff95a1044618c6a4dc81fba3ef9356f40057b6781e51bf062ca45
|
|
BLAKE2b-256 checksum How to use checksums |
c16d2ededaad91dabe4602095fee005da85be23643600c01439e1ef7c7cfc2d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.8.16 Linux/5.15.0-1034-azure
|
Security for chocs-middleware-trace 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.