No project description provided
Project description
TraceId
TraceId is a trace ID generator based on Python contextvars. It automatically passes the trace ID in asynchronous tasks, enabling cross-coroutine tracing.
Installation
Install via pip
pip install traceid
Install via poetry
poetry add traceid
Usage
Generate trace ID
from traceid import TraceId
TraceId.gen()
Set trace ID
from traceid import TraceId
TraceId.set('your trace id')
Get trace ID
from traceid import TraceId
TraceId.get()
Clear trace ID
from traceid import TraceId
TraceId.clear()
Check if trace ID has been set/generated
from traceid import TraceId
TraceId.is_set()
Integrate with Fastapi
import typing
from fastapi import FastAPI, Request, Response
from traceid import TraceId
from aiorow import Connection, DSN
app = FastAPI()
@app.middleware("http")
async def add_trace_id(
request: Request, call_next: typing.Callable[[Request], typing.Awaitable[Response]]
) -> Response:
trace_id = request.headers.get("X-Request-ID", None)
if trace_id is None:
TraceId.gen()
else:
TraceId.set(trace_id)
response = await call_next(request)
response.headers["X-Request-ID"] = str(TraceId.get())
return response
@app.get("/")
def read_root():
return {"Hello": "World"}
Integrate with logging
import logging
import logging.config
import os
from traceid import JSONFormatter
LOG_SETTINGS = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": logging.INFO,
"formatter": "json",
},
},
"formatters": {
"json": {
"()": JSONFormatter,
},
},
"loggers": {
"": {"level": logging.INFO, "handlers": ["console"], "propagate": True},
},
}
logging.config.dictConfig(LOG_SETTINGS)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
traceid-1.1.0.tar.gz
(4.6 kB
view details)
Built Distribution
File details
Details for the file traceid-1.1.0.tar.gz
.
File metadata
- Download URL: traceid-1.1.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dff1fde3e5211d0a3b9c7637d9f2084584f89e715684dc0e6b8b6a1b30cec66 |
|
MD5 | 7ee76c4751cf61421fe32ef9518956f9 |
|
BLAKE2b-256 | 74ed70ef6736720bc0a6754b5cd2c80e560adba1d198c143983c74ef060dcc72 |
File details
Details for the file traceid-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: traceid-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af5bba272d87ae40e70a85466a74c78c066dc2c8e1fedeb5b10cd3af29b3edbc |
|
MD5 | fd5fe42b1c203bac1134ef23ba60bbda |
|
BLAKE2b-256 | f5005720a334111785f0ac88c96db61c2e40066334ad616c9dc608c7e1dab43e |