Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PexMor/mytrace

Repository files navigation

πŸ” AI Trace Viewer

A lightweight, self-contained system for viewing structured logs as collapsible execution trees using OpenTelemetry spans and structlog.

Features

  • 🌳 Collapsible Trace Trees - Visualize function call hierarchies with nested spans
  • πŸ“Š Structured Logging - JSON logs with automatic trace/span ID injection
  • πŸ“ Source Location Tracking - Automatic file path and line number capture
  • πŸ”— No External Dependencies - No Zipkin, Jaeger, or other backend required
  • πŸš€ Simple Setup - Just SQLite + FastAPI + Static HTML/JS
  • 🎨 Modern UI - Clean, responsive web interface with advanced features
  • πŸ”Œ VSCode/Cursor Plugin Ready - Compatible with IDE plugins for in-editor trace viewing

Quick Start

# Install
uv sync

# Start the viewer server
uv run aitrace

# Run example (in another terminal)
uv sync --extra examples
uv run python test/02_simple.py

# View traces at http://localhost:8000

For detailed instructions, see the πŸ“– Quick Start Guide.

Basic Usage

from aitrace import setup_tracing, auto_span, BufferedLogger

# Initialize
tracer = setup_tracing("my-service")
buffered = BufferedLogger(target="http://localhost:8000/api/ingest")
log = buffered.logger

# Instrument your code
@auto_span()
def my_function(arg):
    log.info("doing_work", arg=arg)
    return result

# Auto-send logs to viewer
with buffered.trace_context(tracer, "my_operation"):
    my_function("test")
# Logs automatically sent when context exits

Documentation

User Guides

Developer Resources

Advanced Topics

Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Your App   β”‚  Uses @auto_span decorator + structlog
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”‚ JSON logs (trace_id, span_id, parent_span_id)
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   SQLite    β”‚  Stores logs indexed by trace/span
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”‚ REST API
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Web UI     β”‚  Browse traces, view collapsible trees
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

See AGENTS.md for comprehensive architectural documentation.

Project Structure

mytrace/
β”œβ”€β”€ aitrace/              # Python tracing library
β”‚   β”œβ”€β”€ buffer.py         # BufferedLogger for log ingestion
β”‚   β”œβ”€β”€ server.py         # FastAPI server + SQLite backend
β”‚   └── static/           # Web UI (simple viewer)
β”œβ”€β”€ aitrace_viewer/       # Advanced web viewer (Preact + Vite)
β”œβ”€β”€ test/                 # Example scripts
β”œβ”€β”€ docs/                 # Documentation
β”‚   β”œβ”€β”€ quickstart.md
β”‚   β”œβ”€β”€ configuration.md
β”‚   β”œβ”€β”€ deployment.md
β”‚   β”œβ”€β”€ viewer.md
β”‚   β”œβ”€β”€ development.md
β”‚   └── archive/          # Historical documentation
β”œβ”€β”€ README.md             # This file
β”œβ”€β”€ AGENTS.md             # Architecture & technical decisions
└── CHANGELOG.md          # Version history

Examples

# Simple chatbot
uv run python test/02_simple.py

# Router with multiple agents
uv run python test/03_router.py

# BufferedLogger patterns
uv run python test/04_buffered_simple.py

# Output target modes (HTTP, file, stdout)
uv run python test/05_target_modes.py

See test/README.md for detailed example documentation.

Configuration

Quick configuration examples:

# Custom port
aitrace --port 9000

# Development mode
aitrace --reload --log-level debug

# Custom database
aitrace --db-path /tmp/traces.db

# Using environment variables
export AITRACE_PORT=9000
export AITRACE_LOG_LEVEL=debug
aitrace

See Configuration Guide for complete reference.

Requirements

  • Python 3.10+
  • Modern web browser for UI
  • Node.js 18+ (optional, for advanced viewer development)

Installation Options

Using uv (recommended)

uv sync

Using pip

pip install -e .

With example dependencies

uv sync --extra examples

Use Cases

Development & Debugging

  • Trace LangGraph/LangChain execution flows
  • Debug nested function calls
  • Inspect LLM prompts and responses

Production Monitoring

  • Lightweight tracing without external services
  • Local SQLite storage for moderate volumes
  • Export logs to files for archival

Testing & CI/CD

  • Capture traces as test artifacts
  • Generate HTML traces offline
  • File-based logging for reproducibility

License

MIT - See LICENSE for details

Credits

Built on OpenTelemetry, structlog, and FastAPI. Inspired by Jaeger and Zipkin, simplified for local development and small-scale production use.


Need Help? Check the Quick Start Guide or AGENTS.md for architecture details.

About

Enhance your logging with tracing a bit and browser your logs/traces in browser

Topics

Resources

License

Stars

Watchers

Forks