TraceLink Python SDK
English | 简体中文
The Python SDK builds protocol-compatible spans, keeps async context with
contextvars, writes optional local files, exports events to the shared
TraceLink Receiver, and integrates incoming FastAPI/Starlette requests.
It does not ship a Python Receiver, Dashboard, or CLI. Start those from the npm
package with npx tracelink dashboard.
Install
pip install tracelink
pip install "tracelink[fastapi]"
Optional AI Agent Skill
The PyPI package provides the Python SDK but does not install the repository's AI Agent Skill. Install that separately with the Skills CLI:
npx skills add qinquan-ai/tracelink --skill tracelink
The CLI detects supported AI agents and either selects or prompts for the
installation target. Installation is project-local by default; pass --global
to make the skill available across projects.
Trace And Export
from tracelink import tracer
exporter = tracer.configure(
enabled=True,
http_endpoint="http://127.0.0.1:5174/__debug_log",
scope_sync_endpoint="http://127.0.0.1:5174/__debug_log/scopes",
file_enabled=False,
)
async def load_order():
tracer.db("orders.py:load", "load order", scope="checkout")
await tracer.span(
"BE-ENTRY",
"routes.py:checkout",
"checkout",
load_order,
scope="checkout",
)
exporter.flush(timeout=5.0)
Without enabled=True, startup follows TRACELINK_ENABLED, then the DEBUG or
DEV environment variables. TRACELINK_SCOPES initializes the local Scope
policy (* or a comma-separated list).
Custom Exporter
from tracelink import HttpExporter, tracer
off = tracer.add_exporter(HttpExporter())
off()
HttpExporter uses a bounded background queue and standard-library HTTP. Its
network failures never escape into application code. FileExporter writes
.tracelink/trace.ndjson and .tracelink/trace.log locally. It is enabled by
default; set file_enabled=False when a Receiver in the same project owns those
files, otherwise the SDK and Receiver can write duplicate rows.
FastAPI
from fastapi import FastAPI
from tracelink import TraceMiddleware
app = FastAPI()
app.add_middleware(TraceMiddleware)
The Extension extracts x-trace-id, x-parent-span-id, and x-debug-scopes,
installs request-local context, and restores the previous values after the
request. The caller's active span becomes the backend entry's parent. Middleware
does not create application spans; instrument route or service boundaries
explicitly.
Outgoing HTTP
from tracelink import create_trace_headers
requests.get(url, headers=create_trace_headers())
The helper is explicit and client-agnostic; TraceLink does not patch Requests, HTTPX, or aiohttp globally.
Context Guarantees
ContextVar preserves nested calls, await, and concurrent asyncio tasks.
Fresh OS threads do not automatically inherit context; copy or pass it
explicitly when creating a thread.
Development
From the repository root:
npm run verify:python
This runs pytest, Ruff, strict mypy, builds an isolated wheel, installs it into a temporary directory, and imports the installed artifact.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tracelink-0.6.5.tar.gz.
File metadata
- Download URL: tracelink-0.6.5.tar.gz
- Upload date:
- Size: 33.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6708b91272096d57807aca140cf9349f76bfb3a5e7100699da3bb4e7dbfa80ec
|
|
| MD5 |
0e0f12eb78bfe245a14c73f62ee4a832
|
|
| BLAKE2b-256 |
69ed59852394bfc2e411a4ac01d658d030b594daf3def7c4560e368994806fcd
|
File details
Details for the file tracelink-0.6.5-py3-none-any.whl.
File metadata
- Download URL: tracelink-0.6.5-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d7a2b9dbb805ddf19a1c21dc7e0f18dc6a06df55c6afe639bc8f969de0b9793
|
|
| MD5 |
c736898ce482f3584da28f049c05cb96
|
|
| BLAKE2b-256 |
487a37039a2fa88fe5e0dd1642f6965480d413222505ffa3771f6e71d52329f2
|