Skip to main content

TraceAgent

MIT License Python 3.12 Tests

Lightweight Python observability SDK for distributed tracing and span management. No external collectors required.


Features

  • Context-manager spans — Start and close spans with with tracer.start_span(...), automatically capturing start/end time and duration.
  • Decorator instrumentation — Wrap any sync or async function with @trace / @trace_async; exceptions are captured and the span marked as ERROR.
  • Pluggable storage — Ships with InMemoryStorage (default) and FileStorage for persistent, file-backed traces.
  • MCP server — Expose live trace data over the Model Context Protocol via list_traces, get_trace, and get_stats tools.
  • Rich terminal dashboard — Render traces and aggregate statistics in the terminal using the dashboard module.
  • Thread- and async-safe — Storage is protected by threading locks; active-span tracking uses contextvars.ContextVar for correct async isolation.

Installation

pip install traceagent          # production
pip install "traceagent[dev]"   # includes pytest, pytest-asyncio, ruff

Or from source:

git clone https://github.com/techknowmad/trace-agent.git
cd trace-agent
pip install -e ".[dev]"

Quick Start

from traceagent import get_tracer, trace, trace_async

# --- Context manager ---
tracer = get_tracer()

with tracer.start_span("db.query", attributes={"table": "users"}) as span:
    span.add_event("cache_miss")
    rows = fetch_rows()          # your code here

# --- Sync decorator ---
@trace(name="process-request")
def handle(request):
    return {"ok": True}

# --- Async decorator ---
@trace_async(name="fetch-data")
async def fetch(url: str):
    async with httpx.AsyncClient() as client:
        return await client.get(url)

# --- Persistent storage ---
from traceagent import FileStorage, Tracer

tracer = Tracer(storage=FileStorage("/tmp/traces"))

with tracer.start_span("batch-job") as span:
    span.set_attribute("records", 1_000)

MCP Server

from traceagent.mcp_server import MCPServer

server = MCPServer()

# List all recorded traces
traces = server.call_tool("list_traces")

# Retrieve a specific trace by ID
trace  = server.call_tool("get_trace", {"trace_id": "<id>"})

# Aggregate statistics
stats  = server.call_tool("get_stats")

Architecture

traceagent/
├── models.py        # Span, Trace, SpanStatus — pure dataclasses, no I/O
├── tracer.py        # Tracer — span lifecycle, ContextVar active-span tracking
├── storage.py       # InMemoryStorage, FileStorage — thread-safe backends
├── decorators.py    # @trace, @trace_async — wraps functions, captures errors
├── mcp_server.py    # MCPServer — MCP-protocol tool surface over live storage
└── dashboard.py     # Rich-powered terminal renderer for traces and stats

Data flow:

caller
  └─ Tracer.start_span()
       ├─ creates Span (model)
       ├─ sets ContextVar (active span)
       └─ on __exit__ / exception
            ├─ records end_time, duration_ms, SpanStatus
            └─ Storage.save_span()
                  ├─ InMemoryStorage  →  dict in process memory
                  └─ FileStorage      →  JSON files on disk

MCPServer
  └─ reads from Storage → serves list_traces / get_trace / get_stats

Development

# Run all tests
pytest -v

# Lint
ruff check .

# Run a single test module
pytest tests/test_tracer.py -v

Contributing

See CONTRIBUTING.md for branching conventions, code style, and pull-request guidelines.


License

MIT © 2026 TechKnowMad Labs Private Limited


Built by TechKnowMad Labs

Metadata

Release files for tkm-traceagent 0.1.0

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

Source distribution (sdist)

Source distribution for tkm-traceagent 0.1.0
File Size Uploaded Provenance Actions
tkm_traceagent-0.1.0.tar.gz 30.0 kB Without provenance Details for tkm_traceagent-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tkm-traceagent 0.1.0
File Interpreter ABI Platform Actions
tkm_traceagent-0.1.0-py3-none-any.whl Python 3 none any Details for tkm_traceagent-0.1.0-py3-none-any.whl

Total release size: 46.3 kB

Release files / tkm_traceagent-0.1.0.tar.gz

Download URL tkm_traceagent-0.1.0.tar.gz
Size 30.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1776cdda3a650138ee6f39cef15f5fdb7fc237c55548e88aeb30d881b452d99a
BLAKE2b-256 checksum
How to use checksums
1155f18e302b1ec4df9bea38634830c2e4c93ff969b67ecedd259941fab1e41c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / tkm_traceagent-0.1.0-py3-none-any.whl

Download URL tkm_traceagent-0.1.0-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5f8a03dab0f6135759da6fed99fed2c2d43a59e04bedaeda0958717a53df386
BLAKE2b-256 checksum
How to use checksums
4c70fb8d07b6c4e3ce05530b752fb725874e4d34f2638420c7351a2ba672f2eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Security for tkm-traceagent 0.1.0

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

0.1.0 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