Skip to main content

Lightweight observability SDK — traces, spans, decorators, MCP server, and dashboard

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkm_traceagent-0.1.0.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tkm_traceagent-0.1.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file tkm_traceagent-0.1.0.tar.gz.

File metadata

  • Download URL: tkm_traceagent-0.1.0.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for tkm_traceagent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1776cdda3a650138ee6f39cef15f5fdb7fc237c55548e88aeb30d881b452d99a
MD5 2d32385f49fcbbc59f57a5aa8b1c135e
BLAKE2b-256 1155f18e302b1ec4df9bea38634830c2e4c93ff969b67ecedd259941fab1e41c

See more details on using hashes here.

File details

Details for the file tkm_traceagent-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tkm_traceagent-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for tkm_traceagent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5f8a03dab0f6135759da6fed99fed2c2d43a59e04bedaeda0958717a53df386
MD5 e5c5cd1697794f22429c07b76df87cef
BLAKE2b-256 4c70fb8d07b6c4e3ce05530b752fb725874e4d34f2638420c7351a2ba672f2eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page