mermaid-trace 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. mermaid_trace-0.3.1/.github/workflows/ci.yml +55 -0
  2. mermaid_trace-0.3.1/.github/workflows/release.yml +51 -0
  3. mermaid_trace-0.3.1/.gitignore +162 -0
  4. mermaid_trace-0.3.1/LICENSE +21 -0
  5. mermaid_trace-0.3.1/PKG-INFO +163 -0
  6. mermaid_trace-0.3.1/README.md +102 -0
  7. mermaid_trace-0.3.1/README_CN.md +102 -0
  8. mermaid_trace-0.3.1/SECURITY.md +21 -0
  9. mermaid_trace-0.3.1/docs/en/API.md +63 -0
  10. mermaid_trace-0.3.1/docs/en/CHANGELOG.md +64 -0
  11. mermaid_trace-0.3.1/docs/en/CONTRIBUTING.md +58 -0
  12. mermaid_trace-0.3.1/docs/en/LICENSE +21 -0
  13. mermaid_trace-0.3.1/docs/en/USER_GUIDE.md +52 -0
  14. mermaid_trace-0.3.1/docs/zh/API.md +63 -0
  15. mermaid_trace-0.3.1/docs/zh/CHANGELOG.md +64 -0
  16. mermaid_trace-0.3.1/docs/zh/CONTRIBUTING.md +58 -0
  17. mermaid_trace-0.3.1/docs/zh/LICENSE +11 -0
  18. mermaid_trace-0.3.1/docs/zh/USER_GUIDE.md +52 -0
  19. mermaid_trace-0.3.1/examples/concurrency_demo.py +46 -0
  20. mermaid_trace-0.3.1/examples/demo.py +33 -0
  21. mermaid_trace-0.3.1/examples/error_handling_demo.py +47 -0
  22. mermaid_trace-0.3.1/examples/nested_demo.py +41 -0
  23. mermaid_trace-0.3.1/flow.mmd +15 -0
  24. mermaid_trace-0.3.1/pyproject.toml +95 -0
  25. mermaid_trace-0.3.1/src/mermaid_trace/__init__.py +70 -0
  26. mermaid_trace-0.3.1/src/mermaid_trace/cli.py +193 -0
  27. mermaid_trace-0.3.1/src/mermaid_trace/core/__init__.py +0 -0
  28. mermaid_trace-0.3.1/src/mermaid_trace/core/context.py +187 -0
  29. mermaid_trace-0.3.1/src/mermaid_trace/core/decorators.py +252 -0
  30. mermaid_trace-0.3.1/src/mermaid_trace/core/events.py +75 -0
  31. mermaid_trace-0.3.1/src/mermaid_trace/core/formatter.py +72 -0
  32. mermaid_trace-0.3.1/src/mermaid_trace/handlers/mermaid_handler.py +86 -0
  33. mermaid_trace-0.3.1/src/mermaid_trace/integrations/fastapi.py +122 -0
  34. mermaid_trace-0.3.1/src/mermaid_trace/py.typed +0 -0
  35. mermaid_trace-0.3.1/tests/__init__.py +0 -0
  36. mermaid_trace-0.3.1/tests/conftest.py +11 -0
  37. mermaid_trace-0.3.1/tests/core/__init__.py +0 -0
  38. mermaid_trace-0.3.1/tests/core/test_cli.py +92 -0
  39. mermaid_trace-0.3.1/tests/core/test_context.py +56 -0
  40. mermaid_trace-0.3.1/tests/core/test_events.py +21 -0
  41. mermaid_trace-0.3.1/tests/core/test_formatter.py +62 -0
  42. mermaid_trace-0.3.1/tests/handlers/test_mermaid_handler.py +60 -0
  43. mermaid_trace-0.3.1/tests/integrations/test_decorators.py +109 -0
  44. mermaid_trace-0.3.1/tests/integrations/test_fastapi_full.py +63 -0
  45. mermaid_trace-0.3.1/tests/repro_concurrency.py +43 -0
  46. mermaid_trace-0.3.1/tests/test_fastapi.py +54 -0
  47. mermaid_trace-0.3.1/tests/test_fastapi_integration.py +76 -0
  48. mermaid_trace-0.3.1/tests/test_flow.py +44 -0
@@ -0,0 +1,55 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: true
14
+ matrix:
15
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+
20
+ - name: Set up Python ${{ matrix.python-version }}
21
+ uses: actions/setup-python@v5
22
+ with:
23
+ python-version: ${{ matrix.python-version }}
24
+
25
+ - name: Cache pip dependencies
26
+ uses: actions/cache@v4
27
+ with:
28
+ path: ~/.cache/pip
29
+ key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
30
+ restore-keys: |
31
+ ${{ runner.os }}-pip-${{ matrix.python-version }}-
32
+ ${{ runner.os }}-pip-
33
+
34
+ - name: Install dependencies
35
+ run: |
36
+ python -m pip install --upgrade pip
37
+ pip install -e .[dev]
38
+
39
+ - name: Lint with Ruff
40
+ run: |
41
+ ruff check src tests
42
+
43
+ - name: Type check with Mypy
44
+ run: |
45
+ mypy --no-incremental src
46
+
47
+ - name: Test with pytest
48
+ run: |
49
+ pytest --cov-report=xml
50
+
51
+ - name: Upload coverage to Codecov
52
+ uses: codecov/codecov-action@v4
53
+ with:
54
+ token: ${{ secrets.CODECOV_TOKEN }}
55
+ fail_ci_if_error: true
@@ -0,0 +1,51 @@
1
+ name: Release
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ test-and-build:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+ - name: Set up Python
13
+ uses: actions/setup-python@v5
14
+ with:
15
+ python-version: "3.10"
16
+ - name: Install dependencies
17
+ run: |
18
+ python -m pip install --upgrade pip
19
+ pip install build twine
20
+ pip install -e .[dev]
21
+ - name: Run tests
22
+ run: |
23
+ pytest
24
+ - name: Build package
25
+ run: |
26
+ python -m build
27
+ - name: Check distribution
28
+ run: |
29
+ python -m twine check --strict dist/*
30
+ - name: Upload Artifacts
31
+ uses: actions/upload-artifact@v4
32
+ with:
33
+ name: dist
34
+ path: dist/
35
+
36
+ pypi-publish:
37
+ name: Upload release to PyPI
38
+ needs: test-and-build
39
+ runs-on: ubuntu-latest
40
+ environment:
41
+ name: pypi
42
+ url: https://pypi.org/p/mermaid-trace
43
+ permissions:
44
+ id-token: write
45
+ steps:
46
+ - uses: actions/download-artifact@v4
47
+ with:
48
+ name: dist
49
+ path: dist/
50
+ - name: Publish package distributions to PyPI
51
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,162 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+ docs/build/
74
+ docs/source/generated/
75
+
76
+ # PyBuilder
77
+ .pybuilder/
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary dependencies to ensure reproducible builds.
100
+ #poetry.lock
101
+
102
+ # pdm
103
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
104
+ #pdm.lock
105
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
106
+ # in version control.
107
+ # https://pdm.fming.dev/#use-with-ide
108
+ .pdm.toml
109
+
110
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and others
111
+ __pypackages__/
112
+
113
+ # Celery stuff
114
+ celerybeat-schedule
115
+ celerybeat.pid
116
+
117
+ # SageMath parsed files
118
+ *.sage.py
119
+
120
+ # Environments
121
+ .env
122
+ .venv
123
+ env/
124
+ venv/
125
+ ENV/
126
+ env.bak/
127
+ venv.bak/
128
+
129
+ # Spyder project settings
130
+ .spyderproject
131
+ .spyproject
132
+
133
+ # Rope project settings
134
+ .ropeproject
135
+
136
+ # mkdocs documentation
137
+ /site
138
+
139
+ # mypy
140
+ .mypy_cache/
141
+ .dmypy.json
142
+ dmypy.json
143
+
144
+ # Pyre type checker
145
+ .pyre/
146
+
147
+ # pytype static type analyzer
148
+ .pytype/
149
+
150
+ # Cython debug symbols
151
+ cython_debug/
152
+
153
+ # IDEs
154
+ .vscode/
155
+ .idea/
156
+ .trae/
157
+ *.swp
158
+ *.swo
159
+ .DS_Store
160
+
161
+ # Ruff
162
+ .ruff_cache/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 xt765
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,163 @@
1
+ Metadata-Version: 2.4
2
+ Name: mermaid-trace
3
+ Version: 0.3.1
4
+ Summary: Visualize your Python code execution flow as Mermaid Sequence Diagrams.
5
+ Project-URL: Documentation, https://github.com/xt765/mermaid-trace#readme
6
+ Project-URL: Changelog, https://github.com/xt765/mermaid-trace/blob/main/docs/en/CHANGELOG.md
7
+ Project-URL: Issues, https://github.com/xt765/mermaid-trace/issues
8
+ Project-URL: Source, https://github.com/xt765/mermaid-trace
9
+ Author-email: xt765 <xt765@foxmail.com>
10
+ License: MIT License
11
+
12
+ Copyright (c) 2026 xt765
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all
22
+ copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ SOFTWARE.
31
+ License-File: LICENSE
32
+ Keywords: asyncio,logging,mermaid,mermaid-trace,sequence-diagram,trace,visualization
33
+ Classifier: Development Status :: 4 - Beta
34
+ Classifier: Intended Audience :: Developers
35
+ Classifier: License :: OSI Approved :: MIT License
36
+ Classifier: Operating System :: OS Independent
37
+ Classifier: Programming Language :: Python
38
+ Classifier: Programming Language :: Python :: 3.10
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Classifier: Programming Language :: Python :: Implementation :: CPython
42
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
43
+ Classifier: Topic :: Software Development :: Debuggers
44
+ Classifier: Topic :: System :: Logging
45
+ Requires-Python: >=3.10
46
+ Requires-Dist: typing-extensions>=4.0.0
47
+ Requires-Dist: watchdog>=2.0.0
48
+ Provides-Extra: all
49
+ Requires-Dist: fastapi>=0.100.0; extra == 'all'
50
+ Provides-Extra: dev
51
+ Requires-Dist: fastapi>=0.100.0; extra == 'dev'
52
+ Requires-Dist: httpx; extra == 'dev'
53
+ Requires-Dist: mypy; extra == 'dev'
54
+ Requires-Dist: pytest; extra == 'dev'
55
+ Requires-Dist: pytest-asyncio; extra == 'dev'
56
+ Requires-Dist: pytest-cov; extra == 'dev'
57
+ Requires-Dist: ruff; extra == 'dev'
58
+ Provides-Extra: fastapi
59
+ Requires-Dist: fastapi>=0.100.0; extra == 'fastapi'
60
+ Description-Content-Type: text/markdown
61
+
62
+ # MermaidTrace: The Python Logger That Draws Diagrams
63
+
64
+ [![PyPI version](https://img.shields.io/pypi/v/mermaid-trace.svg?style=flat-square)](https://pypi.org/project/mermaid-trace/)
65
+ [![Python Versions](https://img.shields.io/pypi/pyversions/mermaid-trace.svg?style=flat-square)](https://pypi.org/project/mermaid-trace/)
66
+ [![License](https://img.shields.io/github/license/xt765/mermaid-trace?style=flat-square)](LICENSE)
67
+ [![CI Status](https://img.shields.io/github/actions/workflow/status/xt765/mermaid-trace/ci.yml?style=flat-square&label=CI)](https://github.com/xt765/mermaid-trace/actions/workflows/ci.yml)
68
+ [![Codecov](https://img.shields.io/codecov/c/github/xt765/mermaid-trace?style=flat-square&logo=codecov)](https://codecov.io/gh/xt765/mermaid-trace)
69
+
70
+ **Stop reading logs. Start watching them.**
71
+
72
+ MermaidTrace is a specialized logging tool that automatically generates [Mermaid JS](https://mermaid.js.org/) sequence diagrams from your code execution. It's perfect for visualizing complex business logic, microservice interactions, or asynchronous flows.
73
+
74
+ ## ✨ Features
75
+
76
+ - **Decorator-Driven**: Just add `@trace` or `@trace_interaction` to your functions.
77
+ - **Auto-Diagramming**: Generates `.mmd` files that can be viewed in VS Code, GitHub, or Mermaid Live Editor.
78
+ - **Async Support**: Works seamlessly with `asyncio` coroutines.
79
+ - **Context Inference**: Automatically tracks nested calls and infers `source` participants using `contextvars`.
80
+ - **FastAPI Integration**: Includes middleware for zero-config HTTP request tracing.
81
+ - **CLI Tool**: Built-in viewer to preview diagrams in your browser.
82
+
83
+ ## 🚀 Quick Start
84
+
85
+ ### Installation
86
+
87
+ ```bash
88
+ pip install mermaid-trace
89
+ ```
90
+
91
+ ### Basic Usage
92
+
93
+ ```python
94
+ from mermaid_trace import trace, configure_flow
95
+ import time
96
+
97
+ # 1. Configure output
98
+ configure_flow("my_flow.mmd")
99
+
100
+ # 2. Add decorators
101
+ @trace(source="Client", target="PaymentService", action="Process Payment")
102
+ def process_payment(amount):
103
+ if check_balance(amount):
104
+ return "Success"
105
+ return "Failed"
106
+
107
+ @trace(source="PaymentService", target="Database", action="Check Balance")
108
+ def check_balance(amount):
109
+ return True
110
+
111
+ # 3. Run your code
112
+ process_payment(100)
113
+ ```
114
+
115
+ ### Nested Calls (Context Inference)
116
+
117
+ You don't need to specify `source` every time. MermaidTrace infers it from the current context.
118
+
119
+ ```python
120
+ @trace(source="Client", target="API")
121
+ def main():
122
+ # Inside here, current participant is "API"
123
+ service_call()
124
+
125
+ @trace(target="Service") # source inferred as "API"
126
+ def service_call():
127
+ pass
128
+ ```
129
+
130
+ ### FastAPI Integration
131
+
132
+ ```python
133
+ from fastapi import FastAPI
134
+ from mermaid_trace.integrations.fastapi import MermaidTraceMiddleware
135
+
136
+ app = FastAPI()
137
+ app.add_middleware(MermaidTraceMiddleware, app_name="MyAPI")
138
+
139
+ @app.get("/")
140
+ async def root():
141
+ return {"message": "Hello World"}
142
+ ```
143
+
144
+ ### CLI Viewer
145
+
146
+ Visualize your generated `.mmd` files instantly:
147
+
148
+ ```bash
149
+ mermaid-trace serve my_flow.mmd
150
+ ```
151
+
152
+ ## 📂 Documentation
153
+
154
+ - [English Documentation](docs/en/README.md)
155
+ - [中文文档](README_CN.md)
156
+
157
+ ## 🤝 Contributing
158
+
159
+ We welcome contributions! Please see [CONTRIBUTING.md](docs/en/CONTRIBUTING.md) for details.
160
+
161
+ ## 📄 License
162
+
163
+ MIT
@@ -0,0 +1,102 @@
1
+ # MermaidTrace: The Python Logger That Draws Diagrams
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/mermaid-trace.svg?style=flat-square)](https://pypi.org/project/mermaid-trace/)
4
+ [![Python Versions](https://img.shields.io/pypi/pyversions/mermaid-trace.svg?style=flat-square)](https://pypi.org/project/mermaid-trace/)
5
+ [![License](https://img.shields.io/github/license/xt765/mermaid-trace?style=flat-square)](LICENSE)
6
+ [![CI Status](https://img.shields.io/github/actions/workflow/status/xt765/mermaid-trace/ci.yml?style=flat-square&label=CI)](https://github.com/xt765/mermaid-trace/actions/workflows/ci.yml)
7
+ [![Codecov](https://img.shields.io/codecov/c/github/xt765/mermaid-trace?style=flat-square&logo=codecov)](https://codecov.io/gh/xt765/mermaid-trace)
8
+
9
+ **Stop reading logs. Start watching them.**
10
+
11
+ MermaidTrace is a specialized logging tool that automatically generates [Mermaid JS](https://mermaid.js.org/) sequence diagrams from your code execution. It's perfect for visualizing complex business logic, microservice interactions, or asynchronous flows.
12
+
13
+ ## ✨ Features
14
+
15
+ - **Decorator-Driven**: Just add `@trace` or `@trace_interaction` to your functions.
16
+ - **Auto-Diagramming**: Generates `.mmd` files that can be viewed in VS Code, GitHub, or Mermaid Live Editor.
17
+ - **Async Support**: Works seamlessly with `asyncio` coroutines.
18
+ - **Context Inference**: Automatically tracks nested calls and infers `source` participants using `contextvars`.
19
+ - **FastAPI Integration**: Includes middleware for zero-config HTTP request tracing.
20
+ - **CLI Tool**: Built-in viewer to preview diagrams in your browser.
21
+
22
+ ## 🚀 Quick Start
23
+
24
+ ### Installation
25
+
26
+ ```bash
27
+ pip install mermaid-trace
28
+ ```
29
+
30
+ ### Basic Usage
31
+
32
+ ```python
33
+ from mermaid_trace import trace, configure_flow
34
+ import time
35
+
36
+ # 1. Configure output
37
+ configure_flow("my_flow.mmd")
38
+
39
+ # 2. Add decorators
40
+ @trace(source="Client", target="PaymentService", action="Process Payment")
41
+ def process_payment(amount):
42
+ if check_balance(amount):
43
+ return "Success"
44
+ return "Failed"
45
+
46
+ @trace(source="PaymentService", target="Database", action="Check Balance")
47
+ def check_balance(amount):
48
+ return True
49
+
50
+ # 3. Run your code
51
+ process_payment(100)
52
+ ```
53
+
54
+ ### Nested Calls (Context Inference)
55
+
56
+ You don't need to specify `source` every time. MermaidTrace infers it from the current context.
57
+
58
+ ```python
59
+ @trace(source="Client", target="API")
60
+ def main():
61
+ # Inside here, current participant is "API"
62
+ service_call()
63
+
64
+ @trace(target="Service") # source inferred as "API"
65
+ def service_call():
66
+ pass
67
+ ```
68
+
69
+ ### FastAPI Integration
70
+
71
+ ```python
72
+ from fastapi import FastAPI
73
+ from mermaid_trace.integrations.fastapi import MermaidTraceMiddleware
74
+
75
+ app = FastAPI()
76
+ app.add_middleware(MermaidTraceMiddleware, app_name="MyAPI")
77
+
78
+ @app.get("/")
79
+ async def root():
80
+ return {"message": "Hello World"}
81
+ ```
82
+
83
+ ### CLI Viewer
84
+
85
+ Visualize your generated `.mmd` files instantly:
86
+
87
+ ```bash
88
+ mermaid-trace serve my_flow.mmd
89
+ ```
90
+
91
+ ## 📂 Documentation
92
+
93
+ - [English Documentation](docs/en/README.md)
94
+ - [中文文档](README_CN.md)
95
+
96
+ ## 🤝 Contributing
97
+
98
+ We welcome contributions! Please see [CONTRIBUTING.md](docs/en/CONTRIBUTING.md) for details.
99
+
100
+ ## 📄 License
101
+
102
+ MIT
@@ -0,0 +1,102 @@
1
+ # MermaidTrace: 能画图的 Python 日志工具
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/mermaid-trace.svg?style=flat-square)](https://pypi.org/project/mermaid-trace/)
4
+ [![Python Versions](https://img.shields.io/pypi/pyversions/mermaid-trace.svg?style=flat-square)](https://pypi.org/project/mermaid-trace/)
5
+ [![License](https://img.shields.io/github/license/xt765/mermaid-trace?style=flat-square)](LICENSE)
6
+ [![CI Status](https://img.shields.io/github/actions/workflow/status/xt765/mermaid-trace/ci.yml?style=flat-square&label=CI)](https://github.com/xt765/mermaid-trace/actions/workflows/ci.yml)
7
+ [![Codecov](https://img.shields.io/codecov/c/github/xt765/mermaid-trace?style=flat-square&logo=codecov)](https://codecov.io/gh/xt765/mermaid-trace)
8
+
9
+ **别再干读日志了。开始“看”懂它们吧。**
10
+
11
+ MermaidTrace 是一个专业的日志工具,能从你的代码执行中自动生成 [Mermaid JS](https://mermaid.js.org/) 时序图。它非常适合可视化复杂的业务逻辑、微服务交互或异步流程。
12
+
13
+ ## ✨ 特性
14
+
15
+ - **装饰器驱动**:只需在函数上添加 `@trace` 或 `@trace_interaction` 即可。
16
+ - **自动绘图**:生成 `.mmd` 文件,可在 VS Code、GitHub 或 Mermaid Live Editor 中查看。
17
+ - **异步支持**:无缝支持 `asyncio` 协程。
18
+ - **上下文推断**:利用 `contextvars` 自动追踪嵌套调用并推断 `source`(调用方)参与者。
19
+ - **FastAPI 集成**:内置中间件,实现零配置的 HTTP 请求追踪。
20
+ - **CLI 工具**:内置查看器,可在浏览器中即时预览图表。
21
+
22
+ ## 🚀 快速开始
23
+
24
+ ### 安装
25
+
26
+ ```bash
27
+ pip install mermaid-trace
28
+ ```
29
+
30
+ ### 基础用法
31
+
32
+ ```python
33
+ from mermaid_trace import trace, configure_flow
34
+ import time
35
+
36
+ # 1. 配置输出文件
37
+ configure_flow("my_flow.mmd")
38
+
39
+ # 2. 添加装饰器
40
+ @trace(source="Client", target="PaymentService", action="Process Payment")
41
+ def process_payment(amount):
42
+ if check_balance(amount):
43
+ return "Success"
44
+ return "Failed"
45
+
46
+ @trace(source="PaymentService", target="Database", action="Check Balance")
47
+ def check_balance(amount):
48
+ return True
49
+
50
+ # 3. 运行代码
51
+ process_payment(100)
52
+ ```
53
+
54
+ ### 嵌套调用(上下文推断)
55
+
56
+ 你不需要每次都指定 `source`(调用方)。MermaidTrace 会根据当前上下文自动推断。
57
+
58
+ ```python
59
+ @trace(source="Client", target="API")
60
+ def main():
61
+ # 在这里,当前的参与者是 "API"
62
+ service_call()
63
+
64
+ @trace(target="Service") # source 被自动推断为 "API"
65
+ def service_call():
66
+ pass
67
+ ```
68
+
69
+ ### FastAPI 集成
70
+
71
+ ```python
72
+ from fastapi import FastAPI
73
+ from mermaid_trace.integrations.fastapi import MermaidTraceMiddleware
74
+
75
+ app = FastAPI()
76
+ app.add_middleware(MermaidTraceMiddleware, app_name="MyAPI")
77
+
78
+ @app.get("/")
79
+ async def root():
80
+ return {"message": "Hello World"}
81
+ ```
82
+
83
+ ### CLI 查看器
84
+
85
+ 即时可视化生成的 `.mmd` 文件:
86
+
87
+ ```bash
88
+ mermaid-trace serve my_flow.mmd
89
+ ```
90
+
91
+ ## 📂 文档
92
+
93
+ - [英文文档](docs/en/README.md)
94
+ - [中文文档](README_CN.md)
95
+
96
+ ## 🤝 贡献
97
+
98
+ 欢迎贡献!详情请参阅 [CONTRIBUTING.md](docs/zh/CONTRIBUTING.md)。
99
+
100
+ ## 📄 许可证
101
+
102
+ MIT
@@ -0,0 +1,21 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ Use this section to tell people about which versions of your project are
6
+ currently being supported with security updates.
7
+
8
+ | Version | Supported |
9
+ | ------- | --------- |
10
+ | 0.1.x | ✅ |
11
+ | < 0.1 | ❌ |
12
+
13
+ ## Reporting a Vulnerability
14
+
15
+ Use this section to tell people how to report a vulnerability.
16
+
17
+ Tell them where to go, how often they can expect to get an update on a
18
+ reported vulnerability, what to expect if the vulnerability is accepted or
19
+ rejected, etc.
20
+
21
+ Please report vulnerabilities to xt765@foxmail.com. We will respond within 48 hours.