MermaidTrace库:让Python运行时“自己画出”时序图 - font-size:12px

📅 2026-11-30 👤 拼多多助力营销技巧 📂 pdd帮助力 👁️ 582 次阅读

玄同 765

大语言模型 (LLM) 开发工程师 | 中国传媒大学 · 数字媒体技术(智能交互与游戏设计)

夸智 · 个人主页 | GitHub · Follow

关于作者

深耕领域:大语言模型开发 / RAG 知识库 / AI Agent 落地 / 模型微调技术栈:Python | RAG (LangChain / Dify + Milvus) | FastAPI + Docker工程能力:专注模型工程化部署、知识库构建与优化,擅长全流程解决方案

「让 AI 交互更智能,让技术落地更高效」 欢迎技术探讨与项目合作,解锁大模型与智能交互的无限可能!

作为后端开发者,你是否曾被复杂的业务逻辑和微服务调用链绕得晕头转向?

面对海量文本日志,一眼难以梳理出核心交互流程;接手异步高并发项目,协程切换导致的上下文断裂让你无从下手;排查线上故障时,需要人肉在脑海中构建调用链路,费时费力;想给团队做技术分享,却苦于没有直观的架构图,只能手动绘制。

今天,我作为 MermaidTrace 的作者 玄同765 , 向你介绍这款能让 Python 代码“自己画画”的工具 —— 它能将真实的运行时调用,自动转化为 Mermaid 时序图,

让那些被代码淹没的逻辑,瞬间清晰可见!

PyPI:mermaid-trace v0.5.3GitHub:xt765/mermaid-traceGitee:https://gitee.com/xt765/mermaid-trace

一、MermaidTrace 是什么?

MermaidTrace 是一款面向 Python 的执行流可视化库。它不同于静态代码分析工具,而是一个 “真实运行记录器” 。 通过装饰器和上下文传播技术,它能捕获每一次函数调用、返回和异常,并输出为标准的 .mmd 文件。

核心特性

运行时真实追踪:装饰器即开即用,所见即所得,精准还原代码执行路径。智能折叠与优化:自动合并循环中的高频重复调用,简化复杂对象的内存地址显示,让图表告别“爆炸”。自动化插桩支持:通过 @trace_class 和 patch_object,零侵入追踪类方法及第三方库函数。异步原生支持:专为 asyncio 设计,基于 contextvars 保证协程切换时上下文不丢失。高性能非阻塞:采用独立后台线程处理 I/O,支持生产级日志轮转(Rotating/TimedRotating)。生态无缝集成:输出标准 Mermaid 语法,提供 FastAPI 中间件实现零配置接入。

#mermaid-svg-vRmuzISbyZh8n6Fl{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vRmuzISbyZh8n6Fl .error-icon{fill:#552222;}#mermaid-svg-vRmuzISbyZh8n6Fl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vRmuzISbyZh8n6Fl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vRmuzISbyZh8n6Fl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vRmuzISbyZh8n6Fl .marker.cross{stroke:#333333;}#mermaid-svg-vRmuzISbyZh8n6Fl svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vRmuzISbyZh8n6Fl p{margin:0;}#mermaid-svg-vRmuzISbyZh8n6Fl .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-vRmuzISbyZh8n6Fl .cluster-label text{fill:#333;}#mermaid-svg-vRmuzISbyZh8n6Fl .cluster-label span{color:#333;}#mermaid-svg-vRmuzISbyZh8n6Fl .cluster-label span p{background-color:transparent;}#mermaid-svg-vRmuzISbyZh8n6Fl .label text,#mermaid-svg-vRmuzISbyZh8n6Fl span{fill:#333;color:#333;}#mermaid-svg-vRmuzISbyZh8n6Fl .node rect,#mermaid-svg-vRmuzISbyZh8n6Fl .node circle,#mermaid-svg-vRmuzISbyZh8n6Fl .node ellipse,#mermaid-svg-vRmuzISbyZh8n6Fl .node polygon,#mermaid-svg-vRmuzISbyZh8n6Fl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vRmuzISbyZh8n6Fl .rough-node .label text,#mermaid-svg-vRmuzISbyZh8n6Fl .node .label text,#mermaid-svg-vRmuzISbyZh8n6Fl .image-shape .label,#mermaid-svg-vRmuzISbyZh8n6Fl .icon-shape .label{text-anchor:middle;}#mermaid-svg-vRmuzISbyZh8n6Fl .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-vRmuzISbyZh8n6Fl .rough-node .label,#mermaid-svg-vRmuzISbyZh8n6Fl .node .label,#mermaid-svg-vRmuzISbyZh8n6Fl .image-shape .label,#mermaid-svg-vRmuzISbyZh8n6Fl .icon-shape .label{text-align:center;}#mermaid-svg-vRmuzISbyZh8n6Fl .node.clickable{cursor:pointer;}#mermaid-svg-vRmuzISbyZh8n6Fl .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-vRmuzISbyZh8n6Fl .arrowheadPath{fill:#333333;}#mermaid-svg-vRmuzISbyZh8n6Fl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vRmuzISbyZh8n6Fl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vRmuzISbyZh8n6Fl .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vRmuzISbyZh8n6Fl .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-vRmuzISbyZh8n6Fl .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vRmuzISbyZh8n6Fl .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-vRmuzISbyZh8n6Fl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vRmuzISbyZh8n6Fl .cluster text{fill:#333;}#mermaid-svg-vRmuzISbyZh8n6Fl .cluster span{color:#333;}#mermaid-svg-vRmuzISbyZh8n6Fl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vRmuzISbyZh8n6Fl .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-vRmuzISbyZh8n6Fl rect.text{fill:none;stroke-width:0;}#mermaid-svg-vRmuzISbyZh8n6Fl .icon-shape,#mermaid-svg-vRmuzISbyZh8n6Fl .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vRmuzISbyZh8n6Fl .icon-shape p,#mermaid-svg-vRmuzISbyZh8n6Fl .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-vRmuzISbyZh8n6Fl .icon-shape rect,#mermaid-svg-vRmuzISbyZh8n6Fl .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vRmuzISbyZh8n6Fl .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-vRmuzISbyZh8n6Fl .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-vRmuzISbyZh8n6Fl :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

trace

Collapsing

业务代码

装饰器层

上下文传播

Flow Logger

MermaidFormatter

Pattern Buffer

Handler 层

AsyncHandler

RotatingHandler

.mmd 文件

二、5 分钟快速上手:从安装到生成图表

1. 安装 MermaidTrace

pip install mermaid-trace

2. 最小可用示例:函数调用追踪

只需给函数加上 @trace 装饰器,即可生成清晰的调用时序:

from mermaid_trace import trace, configure_flow

# 初始化配置,指定输出文件,开启覆盖模式

configure_flow("flow.mmd", overwrite=True)

@trace(source="Client", target="PaymentService", action="Process Payment")

def process_payment(amount):

if check_balance(amount):

return "Success"

return "Failed"

@trace(source="PaymentService", target="Database", action="Check Balance")

def check_balance(amount):

return True

# 执行业务逻辑

process_payment(100)

运行上述代码后,生成的 flow.mmd 文件内容如下(可在任何支持 Mermaid 的地方预览):

Database

PaymentService

Client

Database

PaymentService

Client

#mermaid-svg-McMiFAzTotIX11Au{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-McMiFAzTotIX11Au .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-McMiFAzTotIX11Au .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-McMiFAzTotIX11Au .error-icon{fill:#552222;}#mermaid-svg-McMiFAzTotIX11Au .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-McMiFAzTotIX11Au .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-McMiFAzTotIX11Au .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-McMiFAzTotIX11Au .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-McMiFAzTotIX11Au .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-McMiFAzTotIX11Au .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-McMiFAzTotIX11Au .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-McMiFAzTotIX11Au .marker{fill:#333333;stroke:#333333;}#mermaid-svg-McMiFAzTotIX11Au .marker.cross{stroke:#333333;}#mermaid-svg-McMiFAzTotIX11Au svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-McMiFAzTotIX11Au p{margin:0;}#mermaid-svg-McMiFAzTotIX11Au .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-McMiFAzTotIX11Au text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-McMiFAzTotIX11Au .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-McMiFAzTotIX11Au .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-McMiFAzTotIX11Au .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-McMiFAzTotIX11Au .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-McMiFAzTotIX11Au #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-McMiFAzTotIX11Au .sequenceNumber{fill:white;}#mermaid-svg-McMiFAzTotIX11Au #sequencenumber{fill:#333;}#mermaid-svg-McMiFAzTotIX11Au #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-McMiFAzTotIX11Au .messageText{fill:#333;stroke:none;}#mermaid-svg-McMiFAzTotIX11Au .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-McMiFAzTotIX11Au .labelText,#mermaid-svg-McMiFAzTotIX11Au .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-McMiFAzTotIX11Au .loopText,#mermaid-svg-McMiFAzTotIX11Au .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-McMiFAzTotIX11Au .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-McMiFAzTotIX11Au .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-McMiFAzTotIX11Au .noteText,#mermaid-svg-McMiFAzTotIX11Au .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-McMiFAzTotIX11Au .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-McMiFAzTotIX11Au .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-McMiFAzTotIX11Au .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-McMiFAzTotIX11Au .actorPopupMenu{position:absolute;}#mermaid-svg-McMiFAzTotIX11Au .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-McMiFAzTotIX11Au .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-McMiFAzTotIX11Au .actor-man circle,#mermaid-svg-McMiFAzTotIX11Au line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-McMiFAzTotIX11Au :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

Process Payment(100)

Check Balance(100)

Return: True

Return: 'Success'

3. 预览图表

你可以使用命令行工具快速启动预览服务:

mermaid-trace serve flow.mmd

三、深入解析:强大的异步操作支持

在现代 Python 异步编程(asyncio)中,传统的追踪工具往往因为协程切换而丢失上下文。MermaidTrace 从设计之初就坚持 “异步优先” 。

1. 异步上下文传播

我们基于 contextvars 构建了上下文管理机制,确保 Trace Context 能跨 await 边界自动传递。每个异步任务(Task)拥有独立的追踪栈,互不干扰。

2. 高性能异步 I/O

为了不拖累高并发业务,我们实现了 AsyncMermaidHandler。业务线程只需将事件推入内存队列,繁重的磁盘写入工作全部由后台线程完成。

3. 异步支持架构图

下图展示了从业务协程触发,到装饰器捕获,再到后台异步写入的全链路工作流:

.mmd File

Background Writer

Memory Queue

Trace Decorator

Async Task

Event Loop

.mmd File

Background Writer

Memory Queue

Trace Decorator

Async Task

Event Loop

#mermaid-svg-KHxxCT3AjPWAzDZ4{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .error-icon{fill:#552222;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .marker.cross{stroke:#333333;}#mermaid-svg-KHxxCT3AjPWAzDZ4 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KHxxCT3AjPWAzDZ4 p{margin:0;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-KHxxCT3AjPWAzDZ4 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-KHxxCT3AjPWAzDZ4 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-KHxxCT3AjPWAzDZ4 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .sequenceNumber{fill:white;}#mermaid-svg-KHxxCT3AjPWAzDZ4 #sequencenumber{fill:#333;}#mermaid-svg-KHxxCT3AjPWAzDZ4 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .messageText{fill:#333;stroke:none;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .labelText,#mermaid-svg-KHxxCT3AjPWAzDZ4 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .loopText,#mermaid-svg-KHxxCT3AjPWAzDZ4 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-KHxxCT3AjPWAzDZ4 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .noteText,#mermaid-svg-KHxxCT3AjPWAzDZ4 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .actorPopupMenu{position:absolute;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-KHxxCT3AjPWAzDZ4 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-KHxxCT3AjPWAzDZ4 .actor-man circle,#mermaid-svg-KHxxCT3AjPWAzDZ4 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-KHxxCT3AjPWAzDZ4 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

par

[Async Execution]

[Background Write]

Schedule Task

Call decorated func

Capture Start Event 拼多多帮助力入口

put_nowait(StartEvent)

await original_func()

await (Suspend)

Resume

get()

Event

write(line)

Return Result

Capture End Event

put_nowait(EndEvent)

Return Result

四、高级特性与实战场景

1. 生产环境性能模式

在生产环境中,建议开启异步模式以获得最佳性能:

from mermaid_trace import configure_flow

configure_flow("flow.mmd", async_mode=True)

性能对比测试显示,异步模式能显著减少主线程阻塞:

#mermaid-svg-dvptQMz2lL5JAkNk{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-dvptQMz2lL5JAkNk .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dvptQMz2lL5JAkNk .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dvptQMz2lL5JAkNk .error-icon{fill:#552222;}#mermaid-svg-dvptQMz2lL5JAkNk .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dvptQMz2lL5JAkNk .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dvptQMz2lL5JAkNk .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dvptQMz2lL5JAkNk .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dvptQMz2lL5JAkNk .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dvptQMz2lL5JAkNk .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dvptQMz2lL5JAkNk .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dvptQMz2lL5JAkNk .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dvptQMz2lL5JAkNk .marker.cross{stroke:#333333;}#mermaid-svg-dvptQMz2lL5JAkNk svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dvptQMz2lL5JAkNk p{margin:0;}#mermaid-svg-dvptQMz2lL5JAkNk .pieCircle{stroke:#000000;stroke-width:2px;opacity:0.7;}#mermaid-svg-dvptQMz2lL5JAkNk .pieOuterCircle{stroke:#000000;stroke-width:1px;fill:none;}#mermaid-svg-dvptQMz2lL5JAkNk .pieTitleText{text-anchor:middle;font-size:25px;fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-dvptQMz2lL5JAkNk .slice{font-family:"trebuchet ms",verdana,arial,sans-serif;fill:#000000;font-size:17px;}#mermaid-svg-dvptQMz2lL5JAkNk .legend text{fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:17px;}#mermaid-svg-dvptQMz2lL5JAkNk :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

80%

20%

相对耗时对比 (Sync vs Async)

Sync (同步)

Async (异步)

2. 智能折叠与对象优化 (New!)

面对复杂的循环调用,MermaidTrace 能够自动识别模式并进行“智能折叠”,同时简化 Python 对象的显示,让图表保持整洁。

from mermaid_trace import trace, configure_flow

@trace(target="Worker")

def process_item(i):

return i * 10

@trace(source="Manager", target="Processor")

def run_loop():

for i in range(100):

process_item(i)

# 自动将 100 次调用合并为一条带计数的记录

run_loop()

生成的图表效果:

Worker

Processor

Manager

Worker

Processor

Manager

#mermaid-svg-pepgXbwSHjNDnanU{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-pepgXbwSHjNDnanU .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-pepgXbwSHjNDnanU .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-pepgXbwSHjNDnanU .error-icon{fill:#552222;}#mermaid-svg-pepgXbwSHjNDnanU .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-pepgXbwSHjNDnanU .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-pepgXbwSHjNDnanU .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-pepgXbwSHjNDnanU .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-pepgXbwSHjNDnanU .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-pepgXbwSHjNDnanU .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-pepgXbwSHjNDnanU .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-pepgXbwSHjNDnanU .marker{fill:#333333;stroke:#333333;}#mermaid-svg-pepgXbwSHjNDnanU .marker.cross{stroke:#333333;}#mermaid-svg-pepgXbwSHjNDnanU svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-pepgXbwSHjNDnanU p{margin:0;}#mermaid-svg-pepgXbwSHjNDnanU .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-pepgXbwSHjNDnanU text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-pepgXbwSHjNDnanU .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-pepgXbwSHjNDnanU .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-pepgXbwSHjNDnanU .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-pepgXbwSHjNDnanU .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-pepgXbwSHjNDnanU #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-pepgXbwSHjNDnanU .sequenceNumber{fill:white;}#mermaid-svg-pepgXbwSHjNDnanU #sequencenumber{fill:#333;}#mermaid-svg-pepgXbwSHjNDnanU #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-pepgXbwSHjNDnanU .messageText{fill:#333;stroke:none;}#mermaid-svg-pepgXbwSHjNDnanU .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-pepgXbwSHjNDnanU .labelText,#mermaid-svg-pepgXbwSHjNDnanU .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-pepgXbwSHjNDnanU .loopText,#mermaid-svg-pepgXbwSHjNDnanU .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-pepgXbwSHjNDnanU .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-pepgXbwSHjNDnanU .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-pepgXbwSHjNDnanU .noteText,#mermaid-svg-pepgXbwSHjNDnanU .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-pepgXbwSHjNDnanU .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-pepgXbwSHjNDnanU .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-pepgXbwSHjNDnanU .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-pepgXbwSHjNDnanU .actorPopupMenu{position:absolute;}#mermaid-svg-pepgXbwSHjNDnanU .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-pepgXbwSHjNDnanU .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-pepgXbwSHjNDnanU .actor-man circle,#mermaid-svg-pepgXbwSHjNDnanU line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-pepgXbwSHjNDnanU :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

run_loop()

process_item(i) x 100

3. 自动化插桩与第三方库补丁

无需逐个添加装饰器,通过 @trace_class 可以一键追踪类中所有方法;使用 patch_object 甚至可以追踪你无法修改源码的第三方库。

from mermaid_trace import trace_class, patch_object

import requests

# 一键追踪整个类

@trace_class

class AuthService:

def login(self): pass

def logout(self): pass

# 为第三方库方法打补丁

patch_object(requests, "get", source="MyAPI", target="GitHub", action="Fetch Repo")

4. 生产级日志轮转

针对长运行系统,支持按文件大小或时间进行自动切割,防止 .mmd 文件过大。

from mermaid_trace.handlers.mermaid_handler import RotatingMermaidFileHandler

import logging

handler = RotatingMermaidFileHandler("app.mmd", maxBytes=1024*1024, backupCount=5)

configure_flow(handlers=[handler])

5. 数据脱敏与输出控制

支持精细化控制记录内容,保护敏感数据并防止日志膨胀:

from mermaid_trace import trace

# 不记录参数,适用于包含敏感信息的函数

@trace(capture_args=False)

def login(password):

pass

# 限制参数长度和递归深度

@trace(max_arg_length=10, max_arg_depth=1)

def process_large_data(data):

pass

6. FastAPI 无缝集成

为 FastAPI 应用添加中间件,即可自动记录 HTTP 请求的全链路:

from fastapi import FastAPI

from mermaid_trace.integrations.fastapi import MermaidTraceMiddleware

app = FastAPI()

app.add_middleware(MermaidTraceMiddleware, app_name="MyAPI")

生成的请求链路示意图:

Handler

MyAPI

Client

Handler

MyAPI

Client

#mermaid-svg-gzUfrVE7Ixqa56zb{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-gzUfrVE7Ixqa56zb .error-icon{fill:#552222;}#mermaid-svg-gzUfrVE7Ixqa56zb .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gzUfrVE7Ixqa56zb .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gzUfrVE7Ixqa56zb .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gzUfrVE7Ixqa56zb .marker.cross{stroke:#333333;}#mermaid-svg-gzUfrVE7Ixqa56zb svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gzUfrVE7Ixqa56zb p{margin:0;}#mermaid-svg-gzUfrVE7Ixqa56zb .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-gzUfrVE7Ixqa56zb text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-gzUfrVE7Ixqa56zb .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-gzUfrVE7Ixqa56zb .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-gzUfrVE7Ixqa56zb .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-gzUfrVE7Ixqa56zb .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-gzUfrVE7Ixqa56zb #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-gzUfrVE7Ixqa56zb .sequenceNumber{fill:white;}#mermaid-svg-gzUfrVE7Ixqa56zb #sequencenumber{fill:#333;}#mermaid-svg-gzUfrVE7Ixqa56zb #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-gzUfrVE7Ixqa56zb .messageText{fill:#333;stroke:none;}#mermaid-svg-gzUfrVE7Ixqa56zb .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-gzUfrVE7Ixqa56zb .labelText,#mermaid-svg-gzUfrVE7Ixqa56zb .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-gzUfrVE7Ixqa56zb .loopText,#mermaid-svg-gzUfrVE7Ixqa56zb .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-gzUfrVE7Ixqa56zb .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-gzUfrVE7Ixqa56zb .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-gzUfrVE7Ixqa56zb .noteText,#mermaid-svg-gzUfrVE7Ixqa56zb .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-gzUfrVE7Ixqa56zb .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-gzUfrVE7Ixqa56zb .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-gzUfrVE7Ixqa56zb .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-gzUfrVE7Ixqa56zb .actorPopupMenu{position:absolute;}#mermaid-svg-gzUfrVE7Ixqa56zb .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-gzUfrVE7Ixqa56zb .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-gzUfrVE7Ixqa56zb .actor-man circle,#mermaid-svg-gzUfrVE7Ixqa56zb line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-gzUfrVE7Ixqa56zb :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

MermaidTraceMiddleware 记录请求

MermaidTraceMiddleware 记录响应与耗时

HTTP Request

路由处理

Response

HTTP Response

五、高级图表示例:应对复杂业务场景

MermaidTrace 不仅能画简单的时序图,还能结合 Mermaid 强大语法,清晰表达复杂的业务逻辑。

1. 分布式 Saga 事务状态流转

场景:微服务架构下的订单创建流程,涉及多服务状态流转与补偿。 复杂度:包含 par 并行处理、alt 条件分支及异常回滚。

支付服务

库存服务

订单服务

支付服务

库存服务

订单服务

#mermaid-svg-QGav5vSG9nfH4g5E{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QGav5vSG9nfH4g5E .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QGav5vSG9nfH4g5E .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QGav5vSG9nfH4g5E .error-icon{fill:#552222;}#mermaid-svg-QGav5vSG9nfH4g5E .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QGav5vSG9nfH4g5E .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QGav5vSG9nfH4g5E .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QGav5vSG9nfH4g5E .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QGav5vSG9nfH4g5E .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QGav5vSG9nfH4g5E .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QGav5vSG9nfH4g5E .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QGav5vSG9nfH4g5E .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QGav5vSG9nfH4g5E .marker.cross{stroke:#333333;}#mermaid-svg-QGav5vSG9nfH4g5E svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QGav5vSG9nfH4g5E p{margin:0;}#mermaid-svg-QGav5vSG9nfH4g5E .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-QGav5vSG9nfH4g5E text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-QGav5vSG9nfH4g5E .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-QGav5vSG9nfH4g5E .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-QGav5vSG9nfH4g5E .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-QGav5vSG9nfH4g5E .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-QGav5vSG9nfH4g5E #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-QGav5vSG9nfH4g5E .sequenceNumber{fill:white;}#mermaid-svg-QGav5vSG9nfH4g5E #sequencenumber{fill:#333;}#mermaid-svg-QGav5vSG9nfH4g5E #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-QGav5vSG9nfH4g5E .messageText{fill:#333;stroke:none;}#mermaid-svg-QGav5vSG9nfH4g5E .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-QGav5vSG9nfH4g5E .labelText,#mermaid-svg-QGav5vSG9nfH4g5E .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-QGav5vSG9nfH4g5E .loopText,#mermaid-svg-QGav5vSG9nfH4g5E .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-QGav5vSG9nfH4g5E .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-QGav5vSG9nfH4g5E .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-QGav5vSG9nfH4g5E .noteText,#mermaid-svg-QGav5vSG9nfH4g5E .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-QGav5vSG9nfH4g5E .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-QGav5vSG9nfH4g5E .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-QGav5vSG9nfH4g5E .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-QGav5vSG9nfH4g5E .actorPopupMenu{position:absolute;}#mermaid-svg-QGav5vSG9nfH4g5E .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-QGav5vSG9nfH4g5E .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-QGav5vSG9nfH4g5E .actor-man circle,#mermaid-svg-QGav5vSG9nfH4g5E line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-QGav5vSG9nfH4g5E :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

par

[并行处理]

触发补偿流程

alt

[余额充足]

[余额不足]

1. 预扣库存

库存锁定成功

2. 发起扣款

3. 创建本地订单记录

扣款成功

更新订单状态: 已支付

扣款失败

4. 释放库存 (补偿)

更新订单状态: 支付失败

2. 复杂领域模型关系图 (Class Diagram)

场景:电商核心领域模型展示。 复杂度:展示继承、组合、聚合等多种类关系。

#mermaid-svg-Q3P0dy7yu1YVyG1t{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Q3P0dy7yu1YVyG1t .error-icon{fill:#552222;}#mermaid-svg-Q3P0dy7yu1YVyG1t .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Q3P0dy7yu1YVyG1t .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Q3P0dy7yu1YVyG1t .marker.cross{stroke:#333333;}#mermaid-svg-Q3P0dy7yu1YVyG1t svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Q3P0dy7yu1YVyG1t p{margin:0;}#mermaid-svg-Q3P0dy7yu1YVyG1t g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-Q3P0dy7yu1YVyG1t g.classGroup text .title{font-weight:bolder;}#mermaid-svg-Q3P0dy7yu1YVyG1t .nodeLabel,#mermaid-svg-Q3P0dy7yu1YVyG1t .edgeLabel{color:#131300;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-Q3P0dy7yu1YVyG1t .label text{fill:#131300;}#mermaid-svg-Q3P0dy7yu1YVyG1t .labelBkg{background:#ECECFF;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-Q3P0dy7yu1YVyG1t .classTitle{font-weight:bolder;}#mermaid-svg-Q3P0dy7yu1YVyG1t .node rect,#mermaid-svg-Q3P0dy7yu1YVyG1t .node circle,#mermaid-svg-Q3P0dy7yu1YVyG1t .node ellipse,#mermaid-svg-Q3P0dy7yu1YVyG1t .node polygon,#mermaid-svg-Q3P0dy7yu1YVyG1t .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Q3P0dy7yu1YVyG1t .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t g.clickable{cursor:pointer;}#mermaid-svg-Q3P0dy7yu1YVyG1t g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-Q3P0dy7yu1YVyG1t g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-Q3P0dy7yu1YVyG1t .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-Q3P0dy7yu1YVyG1t .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-Q3P0dy7yu1YVyG1t .dashed-line{stroke-dasharray:3;}#mermaid-svg-Q3P0dy7yu1YVyG1t .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-Q3P0dy7yu1YVyG1t #compositionStart,#mermaid-svg-Q3P0dy7yu1YVyG1t .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #compositionEnd,#mermaid-svg-Q3P0dy7yu1YVyG1t .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #dependencyStart,#mermaid-svg-Q3P0dy7yu1YVyG1t .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #dependencyStart,#mermaid-svg-Q3P0dy7yu1YVyG1t .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #extensionStart,#mermaid-svg-Q3P0dy7yu1YVyG1t .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #extensionEnd,#mermaid-svg-Q3P0dy7yu1YVyG1t .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #aggregationStart,#mermaid-svg-Q3P0dy7yu1YVyG1t .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #aggregationEnd,#mermaid-svg-Q3P0dy7yu1YVyG1t .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #lollipopStart,#mermaid-svg-Q3P0dy7yu1YVyG1t .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t #lollipopEnd,#mermaid-svg-Q3P0dy7yu1YVyG1t .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Q3P0dy7yu1YVyG1t .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-Q3P0dy7yu1YVyG1t .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Q3P0dy7yu1YVyG1t .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Q3P0dy7yu1YVyG1t .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Q3P0dy7yu1YVyG1t :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

继承

继承

下单

组合 (强依赖)

聚合 (引用)

1

1

*

*

*

1

User

+String userId

+String email

+login()

+register()

Customer

+Address address

+getOrderHistory()

Admin

+grantPermissions()

Order

+String orderId

+Date createTime

+calculateTotal()

OrderItem

+int quantity

+double price

Product

+String sku

+String name

+double stock

3. 大数据并行处理流水线 (Flowchart)

场景:模拟大数据 ETL 作业流程。 复杂度:使用嵌套 subgraph 展示数据源、清洗、并行计算到存储的多层级流向。

#mermaid-svg-Ix8A0pZ28D6NzC0t{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Ix8A0pZ28D6NzC0t .error-icon{fill:#552222;}#mermaid-svg-Ix8A0pZ28D6NzC0t .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Ix8A0pZ28D6NzC0t .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .marker.cross{stroke:#333333;}#mermaid-svg-Ix8A0pZ28D6NzC0t svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Ix8A0pZ28D6NzC0t p{margin:0;}#mermaid-svg-Ix8A0pZ28D6NzC0t .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .cluster-label text{fill:#333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .cluster-label span{color:#333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .cluster-label span p{background-color:transparent;}#mermaid-svg-Ix8A0pZ28D6NzC0t .label text,#mermaid-svg-Ix8A0pZ28D6NzC0t span{fill:#333;color:#333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .node rect,#mermaid-svg-Ix8A0pZ28D6NzC0t .node circle,#mermaid-svg-Ix8A0pZ28D6NzC0t .node ellipse,#mermaid-svg-Ix8A0pZ28D6NzC0t .node polygon,#mermaid-svg-Ix8A0pZ28D6NzC0t .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Ix8A0pZ28D6NzC0t .rough-node .label text,#mermaid-svg-Ix8A0pZ28D6NzC0t .node .label text,#mermaid-svg-Ix8A0pZ28D6NzC0t .image-shape .label,#mermaid-svg-Ix8A0pZ28D6NzC0t .icon-shape .label{text-anchor:middle;}#mermaid-svg-Ix8A0pZ28D6NzC0t .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Ix8A0pZ28D6NzC0t .rough-node .label,#mermaid-svg-Ix8A0pZ28D6NzC0t .node .label,#mermaid-svg-Ix8A0pZ28D6NzC0t .image-shape .label,#mermaid-svg-Ix8A0pZ28D6NzC0t .icon-shape .label{text-align:center;}#mermaid-svg-Ix8A0pZ28D6NzC0t .node.clickable{cursor:pointer;}#mermaid-svg-Ix8A0pZ28D6NzC0t .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .arrowheadPath{fill:#333333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Ix8A0pZ28D6NzC0t .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Ix8A0pZ28D6NzC0t .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Ix8A0pZ28D6NzC0t .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Ix8A0pZ28D6NzC0t .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Ix8A0pZ28D6NzC0t .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Ix8A0pZ28D6NzC0t .cluster text{fill:#333;}#mermaid-svg-Ix8A0pZ28D6NzC0t .cluster span{color:#333;}#mermaid-svg-Ix8A0pZ28D6NzC0t div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Ix8A0pZ28D6NzC0t .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Ix8A0pZ28D6NzC0t rect.text{fill:none;stroke-width:0;}#mermaid-svg-Ix8A0pZ28D6NzC0t .icon-shape,#mermaid-svg-Ix8A0pZ28D6NzC0t .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Ix8A0pZ28D6NzC0t .icon-shape p,#mermaid-svg-Ix8A0pZ28D6NzC0t .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Ix8A0pZ28D6NzC0t .icon-shape rect,#mermaid-svg-Ix8A0pZ28D6NzC0t .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Ix8A0pZ28D6NzC0t .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Ix8A0pZ28D6NzC0t .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Ix8A0pZ28D6NzC0t :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-Ix8A0pZ28D6NzC0t .storage>*{fill:#e1f5fe!important;stroke:#01579b!important;}#mermaid-svg-Ix8A0pZ28D6NzC0t .storage span{fill:#e1f5fe!important;stroke:#01579b!important;}

数仓存储

ETL 处理层

数据源层

并行计算

业务数据库

日志文件

第三方API

数据抽取

数据清洗

Map: 用户维度聚合

Map: 商品维度聚合

Reduce: 汇总统计

HDFS

ClickHouse

六、未来规划:MermaidTrace 的下一步

作为开源项目,我们的目标是持续提升开发者的可视化体验。未来的计划包括:

可视化 Dashboard:内置轻量级 Web 界面,实时预览和检索历史 Trace。多维度追踪增强:支持按 Trace ID 自动分离独立的 .mmd 文件,便于在大流量下快速定位。更多框架适配:除了 FastAPI,将逐步增加对 Django、Flask 和 Sanic 的原生支持。AI 辅助分析:集成 LLM 自动总结 Trace 链路,并给出性能优化或 Bug 修复建议。

七、贡献与反馈

MermaidTrace 是一个完全开源的项目,欢迎大家通过以下方式参与进来:

PyPI:https://pypi.org/project/mermaid-trace/GitHub:https://github.com/xt765/mermaid-traceGitee:https://gitee.com/xt765/mermaid-trace

欢迎提交 Issue 或 PR,让我们一起把它变得更好!

八、总结:让代码逻辑一目了然

真实、低侵入、可视化、可扩展 —— 这就是 MermaidTrace 的核心价值。它用最少的成本,把晦涩的运行时逻辑变成直观的图表,让系统真正变得“可解释”。

如果你正为复杂业务流、异步链路或系统可视化而烦恼,不妨试试 MermaidTrace,它会成为你开发工具箱中的得力助手!

现在就开始体验吧: pip install mermaid-trace

让代码逻辑一目了然,从 MermaidTrace 开始!

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧