TraceId is a trace ID generator based on Python contextvars. It automatically passes the trace ID in asynchronous tasks, enabling cross-coroutine tracing.
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.1.tar.gz
(4.7 kB
view details)
Built Distribution
File details
Details for the file traceid-1.1.1.tar.gz
.
File metadata
- Download URL: traceid-1.1.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22fa37ad6f2a19419ce8778d365a1d5d4399c10bc9c1d41bb474a759705f30b5 |
|
MD5 | 692b1ffbcf10cbf21f76947b625e392d |
|
BLAKE2b-256 | c8d72772a347fc0e98549224177dbbe11b96fb1112519a239b097bda9c7ad43a |
File details
Details for the file traceid-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: traceid-1.1.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ea21e7b92686ea00e29c36e55cdeb51d6bcb78f6259a6e9586fdfd68a8f9544 |
|
MD5 | ecaaec8dd007e2945c1c7b38f527b923 |
|
BLAKE2b-256 | 9e3c8d9a4dd219a9c5ef63179397309d1f9d2b5746c3b86c258be83f52632f17 |