A Python logging tool that automatically traces code execution and generates Mermaid JS sequence diagrams via decorators, with async support, distributed tracing, FastAPI and LangChain integration.
Core Positioning#
MermaidTrace is an execution flow visualization tool designed for Python. It captures function calls at runtime via decorators (@trace, @trace_class, @trace_interaction) and automatically converts them to standard Mermaid JS sequence diagrams, addressing the limitations of traditional text logs in understanding complex logic.
Key Features#
Tracing Capabilities#
- Decorator-driven:
@trace,@trace_interaction,@trace_class(covers all class public methods) - Auto Sequence Diagram Generation: Function calls automatically converted to Mermaid syntax
- Native Async Support: Seamless support for asyncio coroutines and concurrent scenarios
- Smart Folding: Auto-detect and fold repeated calls (e.g.,
func (x10)), preventing diagram explosion - Third-party Library Patching:
patch_objecttraces external library methods without modifying source code
Privacy & Security#
- Built-in
DataMaskerfor automatic data masking - Configurable sensitive field masks (password, token, etc.)
capture_args=Falseprevents sensitive parameters from being logged
Performance Control#
- Configurable
sample_rateto control tracing percentage - Async mode (
async_mode=True) offloads log writing to background thread - Queue size limit (
queue_size=1000) prevents memory overflow in high-traffic scenarios
Distributed Tracing#
- W3C Trace Context standard support
- B3 and custom X-Trace-ID headers support
- FastAPI middleware for cross-service trace context propagation
Visualization & UI#
- FastAPI-powered interactive Web preview interface
- Real-time rendering (integrated mermaid.js)
- Pan and zoom support (svg-pan-zoom)
- Server-Sent Events (SSE) for auto-refresh
- Directory browsing and multi-file switching
Framework Integration#
FastAPI#
- Zero-config HTTP request tracing middleware
- Header propagation support
LangChain#
MermaidTraceCallbackHandlersupports Chains, LLMs, ChatModels, Tools, Retrievers- Participant stack correctly tracks nested calls and return arrows
- RAG and Agent workflow visualization support
Log Management#
RotatingMermaidFileHandler: Size-based rotationTimedRotatingMermaidFileHandler: Time-based rotation- Auto-split
.mmdfiles overwriteparameter clears diagram files on app restart
Error Handling#
- Captures complete Python traceback on exceptions
- Displays error details as Notes in Mermaid diagrams
Installation & Usage#
# Basic installation
pip install mermaid-trace
# FastAPI integration
pip install mermaid-trace[fastapi]
# LangChain integration
pip install mermaid-trace[langchain]
Quick Start#
from mermaid_trace import trace, configure_flow
configure_flow("my_flow.mmd")
@trace(source="User", target="AuthService")
def login(username):
return verify_db(username)
login("admin")
CLI Preview#
mermaid-trace serve my_flow.mmd # Preview single file
mermaid-trace serve . # Preview directory
Use Cases#
- Legacy Code Understanding: Quickly generate execution path diagrams for unknown codebases
- Automated Documentation: Keep sequence diagrams 100% synchronized with code logic
- Debugging Concurrency & Recursion: Visualize async task interleaving and recursion depth
- Microservice Interaction Visualization: Trace call relationships between microservices
- LLM Application Monitoring: Visualize LangChain Chains, Agent decisions, and RAG retrieval processes
Project Info#
- Python Version: >=3.10 (supports 3.10-3.14)
- Implementation: CPython, PyPy
- Latest Version: v0.7.0
- Test Coverage: >90%
- Type Annotations: 100% Mypy coverage