Name | traceid JSON |
Version |
1.1.1
JSON |
| download |
home_page | |
Summary | TraceId is a trace ID generator based on Python contextvars. It automatically passes the trace ID in asynchronous tasks, enabling cross-coroutine tracing. |
upload_time | 2024-02-15 09:53:51 |
maintainer | |
docs_url | None |
author | Yibu Ma |
requires_python | >=3.11,<4.0 |
license | BSD 3-Clause |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# TraceId
[![codecov](https://codecov.io/gh/yibuma/traceid/graph/badge.svg?token=D0xBb4lMqA)](https://codecov.io/gh/yibuma/traceid)
TraceId is a trace ID generator based on [Python contextvars](https://docs.python.org/3/library/contextvars.html). 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
```python
from traceid import TraceId
TraceId.gen()
```
### Set trace ID
```python
from traceid import TraceId
TraceId.set('your trace id')
```
### Get trace ID
```python
from traceid import TraceId
TraceId.get()
```
### Clear trace ID
```python
from traceid import TraceId
TraceId.clear()
```
### Check if trace ID has been set/generated
```python
from traceid import TraceId
TraceId.is_set()
```
### Integrate with Fastapi
```python
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
```python
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)
```
Raw data
{
"_id": null,
"home_page": "",
"name": "traceid",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Yibu Ma",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/c8/d7/2772a347fc0e98549224177dbbe11b96fb1112519a239b097bda9c7ad43a/traceid-1.1.1.tar.gz",
"platform": null,
"description": "# TraceId\n[![codecov](https://codecov.io/gh/yibuma/traceid/graph/badge.svg?token=D0xBb4lMqA)](https://codecov.io/gh/yibuma/traceid) \n\nTraceId is a trace ID generator based on [Python contextvars](https://docs.python.org/3/library/contextvars.html). It automatically passes the trace ID in asynchronous tasks, enabling cross-coroutine tracing.\n\n## Installation\n\n### Install via pip\n```\npip install traceid\n```\n\n### Install via poetry\n\n```\npoetry add traceid\n```\n\n## Usage\n\n### Generate trace ID\n```python\nfrom traceid import TraceId\nTraceId.gen()\n```\n\n### Set trace ID\n```python\nfrom traceid import TraceId\nTraceId.set('your trace id')\n```\n\n### Get trace ID\n```python\nfrom traceid import TraceId\nTraceId.get()\n```\n\n### Clear trace ID\n```python\nfrom traceid import TraceId\nTraceId.clear()\n```\n\n### Check if trace ID has been set/generated\n```python\nfrom traceid import TraceId\nTraceId.is_set()\n```\n\n### Integrate with Fastapi\n```python\nimport typing\n\nfrom fastapi import FastAPI, Request, Response\nfrom traceid import TraceId\nfrom aiorow import Connection, DSN\n\n\napp = FastAPI()\n\n@app.middleware(\"http\")\nasync def add_trace_id(\n request: Request, call_next: typing.Callable[[Request], typing.Awaitable[Response]]\n) -> Response:\n trace_id = request.headers.get(\"X-Request-ID\", None)\n if trace_id is None:\n TraceId.gen()\n else:\n TraceId.set(trace_id)\n response = await call_next(request)\n response.headers[\"X-Request-ID\"] = str(TraceId.get())\n return response\n@app.get(\"/\")\ndef read_root():\n return {\"Hello\": \"World\"}\n```\n\n### Integrate with logging\n```python\nimport logging\nimport logging.config\nimport os\n\nfrom traceid import JSONFormatter\n\n\nLOG_SETTINGS = {\n \"version\": 1,\n \"disable_existing_loggers\": False,\n \"handlers\": {\n \"console\": {\n \"class\": \"logging.StreamHandler\",\n \"level\": logging.INFO,\n \"formatter\": \"json\",\n },\n },\n \"formatters\": {\n \"json\": {\n \"()\": JSONFormatter,\n },\n },\n \"loggers\": {\n \"\": {\"level\": logging.INFO, \"handlers\": [\"console\"], \"propagate\": True},\n },\n}\n\nlogging.config.dictConfig(LOG_SETTINGS)\n```",
"bugtrack_url": null,
"license": "BSD 3-Clause",
"summary": "TraceId is a trace ID generator based on Python contextvars. It automatically passes the trace ID in asynchronous tasks, enabling cross-coroutine tracing.",
"version": "1.1.1",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9e3c8d9a4dd219a9c5ef63179397309d1f9d2b5746c3b86c258be83f52632f17",
"md5": "ecaaec8dd007e2945c1c7b38f527b923",
"sha256": "6ea21e7b92686ea00e29c36e55cdeb51d6bcb78f6259a6e9586fdfd68a8f9544"
},
"downloads": -1,
"filename": "traceid-1.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ecaaec8dd007e2945c1c7b38f527b923",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11,<4.0",
"size": 5630,
"upload_time": "2024-02-15T09:53:50",
"upload_time_iso_8601": "2024-02-15T09:53:50.421421Z",
"url": "https://files.pythonhosted.org/packages/9e/3c/8d9a4dd219a9c5ef63179397309d1f9d2b5746c3b86c258be83f52632f17/traceid-1.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c8d72772a347fc0e98549224177dbbe11b96fb1112519a239b097bda9c7ad43a",
"md5": "692b1ffbcf10cbf21f76947b625e392d",
"sha256": "22fa37ad6f2a19419ce8778d365a1d5d4399c10bc9c1d41bb474a759705f30b5"
},
"downloads": -1,
"filename": "traceid-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "692b1ffbcf10cbf21f76947b625e392d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11,<4.0",
"size": 4718,
"upload_time": "2024-02-15T09:53:51",
"upload_time_iso_8601": "2024-02-15T09:53:51.388202Z",
"url": "https://files.pythonhosted.org/packages/c8/d7/2772a347fc0e98549224177dbbe11b96fb1112519a239b097bda9c7ad43a/traceid-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-15 09:53:51",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "traceid"
}