Skip to main content

TraceId

codecov

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)

Metadata

Release files for traceid 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for traceid 1.1.1
File Size Uploaded Provenance Actions
traceid-1.1.1.tar.gz 4.7 kB Without provenance Details for traceid-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for traceid 1.1.1
File Interpreter ABI Platform Actions
traceid-1.1.1-py3-none-any.whl Python 3 none any Details for traceid-1.1.1-py3-none-any.whl

Total release size: 10.3 kB

Release files / traceid-1.1.1.tar.gz

Download URL traceid-1.1.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
22fa37ad6f2a19419ce8778d365a1d5d4399c10bc9c1d41bb474a759705f30b5
BLAKE2b-256 checksum
How to use checksums
c8d72772a347fc0e98549224177dbbe11b96fb1112519a239b097bda9c7ad43a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1019-azure

Release files / traceid-1.1.1-py3-none-any.whl

Download URL traceid-1.1.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ea21e7b92686ea00e29c36e55cdeb51d6bcb78f6259a6e9586fdfd68a8f9544
BLAKE2b-256 checksum
How to use checksums
9e3c8d9a4dd219a9c5ef63179397309d1f9d2b5746c3b86c258be83f52632f17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1019-azure

Security for traceid 1.1.1

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.

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page