Git Product home page Git Product logo

traceid's Introduction

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)

traceid's People

Contributors

yibuma avatar dependabot[bot] avatar github-actions[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.