mermaid-trace 0.3.1__tar.gz → 0.4.0__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.
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/PKG-INFO +4 -2
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/README.md +1 -1
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/README_CN.md +2 -2
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/en/API.md +14 -2
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/en/CHANGELOG.md +14 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/en/CONTRIBUTING.md +11 -1
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/en/USER_GUIDE.md +41 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/zh/API.md +14 -2
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/zh/CHANGELOG.md +15 -1
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/zh/CONTRIBUTING.md +11 -1
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/zh/USER_GUIDE.md +41 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/__init__.md +122 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/cli.md +262 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/core/context.md +200 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/core/decorators.md +325 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/core/events.md +96 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/core/formatter.md +105 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/handlers/async_handler.md +69 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/handlers/mermaid_handler.md +110 -0
- mermaid_trace-0.4.0/docs/zh/code_comments/src/mermaid_trace/integrations/fastapi.md +150 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/examples/concurrency_demo.py +8 -8
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/examples/demo.py +6 -1
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/examples/error_handling_demo.py +6 -3
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/examples/nested_demo.py +5 -1
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/pyproject.toml +14 -2
- mermaid_trace-0.4.0/src/mermaid_trace/__init__.py +131 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/cli.py +88 -20
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/core/context.py +21 -20
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/core/decorators.py +212 -84
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/core/events.py +27 -25
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/core/formatter.py +20 -11
- mermaid_trace-0.4.0/src/mermaid_trace/handlers/async_handler.py +52 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/handlers/mermaid_handler.py +33 -20
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/integrations/fastapi.py +45 -22
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/tests/conftest.py +1 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/tests/core/test_cli.py +42 -23
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/tests/core/test_context.py +13 -7
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/tests/core/test_events.py +9 -2
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/tests/core/test_formatter.py +29 -10
- mermaid_trace-0.4.0/tests/handlers/test_handlers.py +122 -0
- mermaid_trace-0.3.1/tests/integrations/test_decorators.py → mermaid_trace-0.4.0/tests/integrations/test_integration_decorators.py +97 -21
- mermaid_trace-0.4.0/tests/integrations/test_integration_fastapi.py +135 -0
- mermaid_trace-0.4.0/tests/scenarios/test_basic_flow.py +44 -0
- mermaid_trace-0.4.0/tests/scenarios/test_concurrency.py +89 -0
- mermaid_trace-0.4.0/tests/scenarios/test_edge_cases.py +81 -0
- mermaid_trace-0.4.0/tests/scenarios/test_performance.py +94 -0
- mermaid_trace-0.4.0/tox.ini +24 -0
- mermaid_trace-0.3.1/src/mermaid_trace/__init__.py +0 -70
- mermaid_trace-0.3.1/tests/handlers/test_mermaid_handler.py +0 -60
- mermaid_trace-0.3.1/tests/integrations/test_fastapi_full.py +0 -63
- mermaid_trace-0.3.1/tests/repro_concurrency.py +0 -43
- mermaid_trace-0.3.1/tests/test_fastapi.py +0 -54
- mermaid_trace-0.3.1/tests/test_fastapi_integration.py +0 -76
- mermaid_trace-0.3.1/tests/test_flow.py +0 -44
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/.github/workflows/ci.yml +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/.github/workflows/release.yml +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/.gitignore +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/LICENSE +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/SECURITY.md +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/en/LICENSE +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/docs/zh/LICENSE +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/flow.mmd +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/core/__init__.py +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/src/mermaid_trace/py.typed +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/tests/__init__.py +0 -0
- {mermaid_trace-0.3.1 → mermaid_trace-0.4.0}/tests/core/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mermaid-trace
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Visualize your Python code execution flow as Mermaid Sequence Diagrams.
|
|
5
5
|
Project-URL: Documentation, https://github.com/xt765/mermaid-trace#readme
|
|
6
6
|
Project-URL: Changelog, https://github.com/xt765/mermaid-trace/blob/main/docs/en/CHANGELOG.md
|
|
@@ -38,6 +38,8 @@ Classifier: Programming Language :: Python
|
|
|
38
38
|
Classifier: Programming Language :: Python :: 3.10
|
|
39
39
|
Classifier: Programming Language :: Python :: 3.11
|
|
40
40
|
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
41
43
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
42
44
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
43
45
|
Classifier: Topic :: Software Development :: Debuggers
|
|
@@ -151,7 +153,7 @@ mermaid-trace serve my_flow.mmd
|
|
|
151
153
|
|
|
152
154
|
## 📂 Documentation
|
|
153
155
|
|
|
154
|
-
- [English Documentation](docs/en/
|
|
156
|
+
- [English Documentation](docs/en/USER_GUIDE.md)
|
|
155
157
|
- [中文文档](README_CN.md)
|
|
156
158
|
|
|
157
159
|
## 🤝 Contributing
|
|
@@ -12,25 +12,37 @@ Decorator to trace function execution. Can be used with or without arguments.
|
|
|
12
12
|
def my_func(): ...
|
|
13
13
|
|
|
14
14
|
# Detailed usage
|
|
15
|
-
@trace(source="Client", target="Server", action="Login")
|
|
15
|
+
@trace(source="Client", target="Server", action="Login", capture_args=False)
|
|
16
16
|
def login(username): ...
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
**Arguments:**
|
|
20
20
|
- `source` (Optional[str]): The caller participant name. If `None`, inferred from `contextvars`.
|
|
21
21
|
- `target` (Optional[str]): The callee participant name. If `None`, inferred from class name (if method) or module name.
|
|
22
|
+
- `name` (Optional[str]): Alias for `target`. Explicitly sets the participant name.
|
|
22
23
|
- `action` (Optional[str]): Description of the interaction. If `None`, defaults to formatted function name (e.g., `process_payment` -> "Process Payment").
|
|
24
|
+
- `capture_args` (bool): Whether to log arguments and return values. Defaults to `True`. Set to `False` for sensitive data.
|
|
25
|
+
- `max_arg_length` (int): Maximum length of string representation for arguments. Defaults to 50.
|
|
26
|
+
- `max_arg_depth` (int): Maximum depth for nested structures in argument representation. Defaults to 1.
|
|
23
27
|
|
|
24
28
|
### `configure_flow`
|
|
25
29
|
|
|
26
30
|
Configures the global logger to output to a Mermaid file. This should be called once at application startup.
|
|
27
31
|
|
|
28
32
|
```python
|
|
29
|
-
def configure_flow(
|
|
33
|
+
def configure_flow(
|
|
34
|
+
output_file: str = "flow.mmd",
|
|
35
|
+
handlers: Optional[List[logging.Handler]] = None,
|
|
36
|
+
append: bool = False,
|
|
37
|
+
async_mode: bool = False
|
|
38
|
+
) -> logging.Logger
|
|
30
39
|
```
|
|
31
40
|
|
|
32
41
|
**Arguments:**
|
|
33
42
|
- `output_file` (str): Path to the `.mmd` output file. Defaults to "flow.mmd".
|
|
43
|
+
- `handlers` (List[logging.Handler]): Optional list of custom logging handlers. If provided, `output_file` is ignored unless you include `MermaidFileHandler` manually.
|
|
44
|
+
- `append` (bool): If `True`, adds new handlers without removing existing ones. Defaults to `False`.
|
|
45
|
+
- `async_mode` (bool): If `True`, uses a non-blocking background thread for logging (QueueHandler). Recommended for production. Defaults to `False`.
|
|
34
46
|
|
|
35
47
|
### `LogContext`
|
|
36
48
|
|
|
@@ -5,12 +5,26 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.4.0] - 2026-01-26
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **Async Mode**: Introduced `async_mode=True` in `configure_flow` to offload log writing to a background thread, eliminating I/O blocking in the main application loop.
|
|
12
|
+
- **Data Privacy**: Added `capture_args=False` to `@trace` to prevent sensitive arguments from being logged.
|
|
13
|
+
- **Argument Truncation**: Added `max_arg_length` and `max_arg_depth` to `@trace` to control the size of logged data structures.
|
|
14
|
+
- **Explicit Naming**: Added `name` (or `target` alias) parameter to `@trace` for explicitly setting the participant name, overriding automatic inference.
|
|
15
|
+
- **Flexible Configuration**: Updated `configure_flow` to accept a list of custom handlers and an `append` flag, allowing better integration with existing logging setups.
|
|
16
|
+
|
|
17
|
+
### Improved
|
|
18
|
+
- **Test Coverage**: Achieved >90% test coverage with a comprehensive new test suite covering unit, integration, and concurrency scenarios.
|
|
19
|
+
- **PyPI Compliance**: Switched to dynamic versioning via `hatch-vcs` and improved package metadata and artifact inclusion.
|
|
20
|
+
|
|
8
21
|
## [0.3.1] - 2026-01-26
|
|
9
22
|
|
|
10
23
|
### Fixed
|
|
11
24
|
- **CI/CD**: Resolved test coverage reporting issues in GitHub Actions by standardizing on editable installs (`pip install -e .[dev]`) across all workflows.
|
|
12
25
|
- **CI/CD**: Aligned coverage configuration in `pyproject.toml` to correctly target the source directory (`src/mermaid_trace`).
|
|
13
26
|
- **Docs**: Fixed badge links in README to use consistent style and valid sources.
|
|
27
|
+
- **Compatibility**: Officially added support for Python 3.13 and 3.14 in project classifiers and CI workflows.
|
|
14
28
|
|
|
15
29
|
## [0.3.0] - 2026-01-26
|
|
16
30
|
|
|
@@ -29,7 +29,12 @@ The following is a set of guidelines for contributing to MermaidTrace and its pa
|
|
|
29
29
|
|
|
30
30
|
4. **Run tests**:
|
|
31
31
|
```bash
|
|
32
|
+
# Run all tests
|
|
32
33
|
pytest
|
|
34
|
+
|
|
35
|
+
# Run tests across multiple Python versions (requires tox)
|
|
36
|
+
pip install tox
|
|
37
|
+
tox
|
|
33
38
|
```
|
|
34
39
|
|
|
35
40
|
## Styleguides
|
|
@@ -38,12 +43,17 @@ The following is a set of guidelines for contributing to MermaidTrace and its pa
|
|
|
38
43
|
|
|
39
44
|
- All Python code is linted with `ruff`.
|
|
40
45
|
- All Python code is type-checked with `mypy`.
|
|
46
|
+
- We use `hatch` for build management and `hatch-vcs` for dynamic versioning.
|
|
41
47
|
|
|
42
48
|
To run checks locally:
|
|
43
49
|
|
|
44
50
|
```bash
|
|
51
|
+
# Using tox (recommended)
|
|
52
|
+
tox -e lint
|
|
53
|
+
|
|
54
|
+
# Or manually
|
|
45
55
|
ruff check src tests
|
|
46
|
-
mypy src
|
|
56
|
+
mypy src/mermaid_trace
|
|
47
57
|
```
|
|
48
58
|
|
|
49
59
|
## Pull Requests
|
|
@@ -41,6 +41,47 @@ MermaidTrace uses Python's `contextvars` to track the "Current Participant".
|
|
|
41
41
|
|
|
42
42
|
This means you usually only need to set the `source` on the *entry point* (the first function).
|
|
43
43
|
|
|
44
|
+
## Advanced Configuration
|
|
45
|
+
|
|
46
|
+
### Async Mode (Performance)
|
|
47
|
+
For high-throughput production environments, enable `async_mode` to offload file writing to a background thread. This ensures your application's main thread is never blocked by disk I/O.
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
configure_flow("flow.mmd", async_mode=True)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Data Capture Control
|
|
54
|
+
You can control how function arguments and return values are recorded to keep diagrams clean and secure.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
# Hide sensitive data
|
|
58
|
+
@trace(capture_args=False)
|
|
59
|
+
def login(password):
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
# Truncate long strings (default: 50 chars)
|
|
63
|
+
@trace(max_arg_length=10)
|
|
64
|
+
def process_large_data(data):
|
|
65
|
+
pass
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Explicit Naming
|
|
69
|
+
If the automatic class/function name inference isn't what you want, you can explicitly name the participant.
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
@trace(name="AuthService") # "AuthService" will appear in the diagram
|
|
73
|
+
def login():
|
|
74
|
+
pass
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Flexible Handler Configuration
|
|
78
|
+
You can add MermaidTrace to an existing logging setup or append multiple handlers.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
# Append to existing handlers instead of clearing them
|
|
82
|
+
configure_flow("flow.mmd", append=True)
|
|
83
|
+
```
|
|
84
|
+
|
|
44
85
|
## CLI Viewer
|
|
45
86
|
|
|
46
87
|
To view your diagrams, use the CLI:
|
|
@@ -12,25 +12,37 @@
|
|
|
12
12
|
def my_func(): ...
|
|
13
13
|
|
|
14
14
|
# 详细用法
|
|
15
|
-
@trace(source="Client", target="Server", action="Login")
|
|
15
|
+
@trace(source="Client", target="Server", action="Login", capture_args=False)
|
|
16
16
|
def login(username): ...
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
**参数:**
|
|
20
20
|
- `source` (Optional[str]): 调用方参与者名称。如果为 `None`,则从 `contextvars` 推断。
|
|
21
21
|
- `target` (Optional[str]): 被调用方参与者名称。如果为 `None`,则从类名(如果是方法)或模块名推断。
|
|
22
|
+
- `name` (Optional[str]): `target` 的别名。显式设置参与者名称。
|
|
22
23
|
- `action` (Optional[str]): 交互描述。如果为 `None`,默认为格式化后的函数名(例如 `process_payment` -> "Process Payment")。
|
|
24
|
+
- `capture_args` (bool): 是否记录参数和返回值。默认为 `True`。对于敏感数据可设置为 `False`。
|
|
25
|
+
- `max_arg_length` (int): 参数字符串表示的最大长度。默认为 50。
|
|
26
|
+
- `max_arg_depth` (int): 参数嵌套结构表示的最大深度。默认为 1。
|
|
23
27
|
|
|
24
28
|
### `configure_flow`
|
|
25
29
|
|
|
26
30
|
配置全局日志记录器以输出到 Mermaid 文件。应在应用程序启动时调用一次。
|
|
27
31
|
|
|
28
32
|
```python
|
|
29
|
-
def configure_flow(
|
|
33
|
+
def configure_flow(
|
|
34
|
+
output_file: str = "flow.mmd",
|
|
35
|
+
handlers: Optional[List[logging.Handler]] = None,
|
|
36
|
+
append: bool = False,
|
|
37
|
+
async_mode: bool = False
|
|
38
|
+
) -> logging.Logger
|
|
30
39
|
```
|
|
31
40
|
|
|
32
41
|
**参数:**
|
|
33
42
|
- `output_file` (str): `.mmd` 输出文件的路径。默认为 "flow.mmd"。
|
|
43
|
+
- `handlers` (List[logging.Handler]): 可选的自定义日志处理器列表。如果提供,`output_file` 将被忽略,除非您手动包含了 `MermaidFileHandler`。
|
|
44
|
+
- `append` (bool): 如果为 `True`,则添加新的处理器而不移除现有的。默认为 `False`。
|
|
45
|
+
- `async_mode` (bool): 如果为 `True`,使用非阻塞后台线程进行日志记录 (QueueHandler)。推荐用于生产环境。默认为 `False`。
|
|
34
46
|
|
|
35
47
|
### `LogContext`
|
|
36
48
|
|
|
@@ -5,12 +5,26 @@
|
|
|
5
5
|
格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.0.0/),
|
|
6
6
|
并且本项目遵守 [Semantic Versioning](https://semver.org/lang/zh-CN/)(语义化版本控制)。
|
|
7
7
|
|
|
8
|
+
## [0.4.0] - 2026-01-26
|
|
9
|
+
|
|
10
|
+
### 新增
|
|
11
|
+
- **异步模式**: 在 `configure_flow` 中引入了 `async_mode=True`,将日志写入操作卸载到后台线程,消除了主应用程序循环中的 I/O 阻塞。
|
|
12
|
+
- **数据隐私**: 为 `@trace` 添加了 `capture_args=False`,以防止敏感参数被记录。
|
|
13
|
+
- **参数截断**: 为 `@trace` 添加了 `max_arg_length` 和 `max_arg_depth`,以控制记录的数据结构的大小。
|
|
14
|
+
- **显式命名**: 为 `@trace` 添加了 `name`(或 `target` 别名)参数,用于显式设置参与者名称,覆盖自动推断。
|
|
15
|
+
- **灵活配置**: 更新了 `configure_flow` 以接受自定义 Handler 列表和 `append` 标志,允许更好地与现有日志设置集成。
|
|
16
|
+
|
|
17
|
+
### 改进
|
|
18
|
+
- **测试覆盖率**: 实现了 >90% 的测试覆盖率,拥有覆盖单元、集成和并发场景的全面新测试套件。
|
|
19
|
+
- **PyPI 合规性**: 切换到通过 `hatch-vcs` 进行动态版本控制,并改进了包元数据和工件包含。
|
|
20
|
+
|
|
8
21
|
## [0.3.1] - 2026-01-26
|
|
9
22
|
|
|
10
23
|
### 修复
|
|
11
24
|
- **CI/CD**: 通过在所有工作流中统一使用可编辑安装 (`pip install -e .[dev]`),解决了 GitHub Actions 中的测试覆盖率报告问题。
|
|
12
25
|
- **CI/CD**: 调整了 `pyproject.toml` 中的覆盖率配置,以正确指向源代码目录 (`src/mermaid_trace`)。
|
|
13
26
|
- **文档**: 修复了 README 中的徽章链接,使用了统一的样式和有效的来源。
|
|
27
|
+
- **兼容性**: 在项目分类器和 CI 工作流中正式添加了对 Python 3.13 和 3.14 的支持。
|
|
14
28
|
|
|
15
29
|
## [0.3.0] - 2026-01-26
|
|
16
30
|
|
|
@@ -47,7 +61,7 @@ MermaidTrace 已从通用的日志包装器转型为专门的 **执行流可视
|
|
|
47
61
|
- **上下文推断**:使用 `contextvars` 自动检测 `source`(源)参与者,无需手动连线即可实现嵌套调用可视化。
|
|
48
62
|
- **Mermaid 处理器**:专门的日志处理器,实时写入 `.mmd` 文件。
|
|
49
63
|
- **CLI 工具**:添加了 `mermaid-trace serve <file.mmd>` 命令,可在浏览器中预览生成的图表。
|
|
50
|
-
- **FastAPI 中间件**:`MermaidTraceMiddleware
|
|
64
|
+
- **FastAPI 中间件**:`MermaidTraceMiddleware` for zero-config HTTP request tracing.
|
|
51
65
|
- **数据捕获**:支持在图表中捕获并显示函数参数和返回值。
|
|
52
66
|
|
|
53
67
|
### 移除
|
|
@@ -29,7 +29,12 @@
|
|
|
29
29
|
|
|
30
30
|
4. **运行测试**:
|
|
31
31
|
```bash
|
|
32
|
+
# 运行所有测试
|
|
32
33
|
pytest
|
|
34
|
+
|
|
35
|
+
# 跨多个 Python 版本运行测试 (需要 tox)
|
|
36
|
+
pip install tox
|
|
37
|
+
tox
|
|
33
38
|
```
|
|
34
39
|
|
|
35
40
|
## 代码风格指南
|
|
@@ -38,12 +43,17 @@
|
|
|
38
43
|
|
|
39
44
|
- 所有 Python 代码均使用 `ruff` 进行 lint 检查。
|
|
40
45
|
- 所有 Python 代码均使用 `mypy` 进行类型检查。
|
|
46
|
+
- 我们使用 `hatch` 进行构建管理,使用 `hatch-vcs` 进行动态版本控制。
|
|
41
47
|
|
|
42
48
|
要在本地运行检查:
|
|
43
49
|
|
|
44
50
|
```bash
|
|
51
|
+
# 使用 tox (推荐)
|
|
52
|
+
tox -e lint
|
|
53
|
+
|
|
54
|
+
# 或者手动运行
|
|
45
55
|
ruff check src tests
|
|
46
|
-
mypy src
|
|
56
|
+
mypy src/mermaid_trace
|
|
47
57
|
```
|
|
48
58
|
|
|
49
59
|
## Pull Request 流程
|
|
@@ -41,6 +41,47 @@ MermaidTrace 使用 Python 的 `contextvars` 来追踪“当前参与者”。
|
|
|
41
41
|
|
|
42
42
|
这意味着通常您只需要在 *入口点*(第一个函数)设置 `source`。
|
|
43
43
|
|
|
44
|
+
## 高级配置
|
|
45
|
+
|
|
46
|
+
### 异步模式 (性能优化)
|
|
47
|
+
对于高吞吐量的生产环境,请启用 `async_mode` 将文件写入操作卸载到后台线程。这确保了您的应用程序主线程永远不会被磁盘 I/O 阻塞。
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
configure_flow("flow.mmd", async_mode=True)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 数据捕获控制
|
|
54
|
+
您可以控制如何记录函数参数和返回值,以保持图表整洁并保护敏感数据。
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
# 隐藏敏感数据
|
|
58
|
+
@trace(capture_args=False)
|
|
59
|
+
def login(password):
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
# 截断长字符串 (默认: 50 字符)
|
|
63
|
+
@trace(max_arg_length=10)
|
|
64
|
+
def process_large_data(data):
|
|
65
|
+
pass
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 显式命名
|
|
69
|
+
如果自动推断的类名/函数名不是您想要的,您可以显式命名参与者。
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
@trace(name="AuthService") # 图表中将显示 "AuthService"
|
|
73
|
+
def login():
|
|
74
|
+
pass
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 灵活的 Handler 配置
|
|
78
|
+
您可以将 MermaidTrace 添加到现有的日志设置中,或者追加多个 Handler。
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
# 追加到现有的 Handler,而不是清除它们
|
|
82
|
+
configure_flow("flow.mmd", append=True)
|
|
83
|
+
```
|
|
84
|
+
|
|
44
85
|
## CLI 查看器
|
|
45
86
|
|
|
46
87
|
要查看您的图表,请使用 CLI:
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# 文件: src/mermaid_trace/__init__.py
|
|
2
|
+
|
|
3
|
+
## 概览
|
|
4
|
+
本文件是 `mermaid-trace` 包的入口点。它导出了主要的 API,如 `trace` 装饰器、`LogContext` 上下文管理器和 `configure_flow` 配置函数。
|
|
5
|
+
|
|
6
|
+
## 核心功能分析
|
|
7
|
+
|
|
8
|
+
### configure_flow
|
|
9
|
+
这是配置日志系统的核心函数。它负责:
|
|
10
|
+
1. 初始化专用的 Logger (`mermaid_trace.flow`)。
|
|
11
|
+
2. 管理 Handler(处理器)的生命周期:
|
|
12
|
+
- 默认情况下,它会清除现有的 Handler,保证配置的幂等性。
|
|
13
|
+
- 通过 `append=True` 参数,允许用户追加新的 Handler 而不覆盖旧的。
|
|
14
|
+
3. **异步支持**:通过 `async_mode` 参数,它能够将实际的 Handler 包装在 `AsyncMermaidHandler` (QueueHandler) 中,从而将文件写入操作移至后台线程,避免阻塞主循环。
|
|
15
|
+
|
|
16
|
+
## 源代码与中文注释
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
"""
|
|
20
|
+
MermaidTrace: 将 Python 代码执行流可视化为 Mermaid 时序图。
|
|
21
|
+
|
|
22
|
+
该包提供了自动追踪函数调用并生成兼容 Mermaid 的时序图(.mmd 文件)的工具。
|
|
23
|
+
旨在帮助开发者理解应用程序的流程、调试复杂的交互并记录系统行为。
|
|
24
|
+
|
|
25
|
+
关键组件:
|
|
26
|
+
- `trace`: 用于检测函数进行追踪的装饰器。它捕获参数、返回值和错误,并将它们记录为交互。
|
|
27
|
+
- `LogContext`: 管理执行上下文(类似于线程本地存储),用于在异步任务和线程之间追踪调用者/被调用者关系。
|
|
28
|
+
- `configure_flow`: 设置日志处理器以将图表写入文件。它处理处理器配置、文件模式和异步日志设置。
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from .core.decorators import trace_interaction, trace
|
|
32
|
+
from .handlers.mermaid_handler import MermaidFileHandler
|
|
33
|
+
from .handlers.async_handler import AsyncMermaidHandler
|
|
34
|
+
from .core.events import FlowEvent
|
|
35
|
+
from .core.context import LogContext
|
|
36
|
+
from .core.formatter import MermaidFormatter
|
|
37
|
+
|
|
38
|
+
# 默认不导入集成模块,以避免硬依赖
|
|
39
|
+
# 如果需要,用户必须显式导入集成(如 FastAPI)。
|
|
40
|
+
|
|
41
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
42
|
+
from typing import List, Optional
|
|
43
|
+
|
|
44
|
+
import logging
|
|
45
|
+
|
|
46
|
+
def configure_flow(
|
|
47
|
+
output_file: str = "flow.mmd",
|
|
48
|
+
handlers: Optional[List[logging.Handler]] = None,
|
|
49
|
+
append: bool = False,
|
|
50
|
+
async_mode: bool = False
|
|
51
|
+
) -> logging.Logger:
|
|
52
|
+
"""
|
|
53
|
+
配置流程日志记录器以输出到 Mermaid 文件。
|
|
54
|
+
|
|
55
|
+
此函数设置捕获追踪事件并将其写入指定输出文件所需的日志基础设施。
|
|
56
|
+
应在应用程序启动时调用一次以初始化追踪系统。
|
|
57
|
+
|
|
58
|
+
参数:
|
|
59
|
+
output_file (str): 输出 .mmd 文件的绝对或相对路径。
|
|
60
|
+
默认为当前目录下的 "flow.mmd"。
|
|
61
|
+
如果文件不存在,将使用正确的头部创建它。
|
|
62
|
+
handlers (List[logging.Handler], optional): 自定义日志处理器列表。
|
|
63
|
+
如果提供,'output_file' 将被忽略,除非
|
|
64
|
+
你显式包含 MermaidFileHandler。
|
|
65
|
+
如果你想将日志流式传输到其他目的地,这很有用。
|
|
66
|
+
append (bool): 如果为 True,则添加新处理器而不移除现有处理器。
|
|
67
|
+
默认为 False(清除现有处理器以防止重复记录)。
|
|
68
|
+
async_mode (bool): 如果为 True,使用非阻塞后台线程进行日志记录 (QueueHandler)。
|
|
69
|
+
推荐用于高性能生产环境,以避免在文件 I/O 期间阻塞主执行线程。
|
|
70
|
+
默认为 False。
|
|
71
|
+
|
|
72
|
+
返回:
|
|
73
|
+
logging.Logger: 用于流程追踪的已配置日志记录器实例。
|
|
74
|
+
"""
|
|
75
|
+
# 获取追踪装饰器使用的特定日志记录器
|
|
76
|
+
# 此日志记录器与根日志记录器隔离,以防止污染
|
|
77
|
+
logger = logging.getLogger("mermaid_trace.flow")
|
|
78
|
+
logger.setLevel(logging.INFO)
|
|
79
|
+
|
|
80
|
+
# 移除现有处理器以避免重复日志(如果配置了多次)
|
|
81
|
+
# 除非请求了 'append'。这确保了多次调用 configure_flow 时的幂等性。
|
|
82
|
+
if not append and logger.hasHandlers():
|
|
83
|
+
logger.handlers.clear()
|
|
84
|
+
|
|
85
|
+
# 确定目标处理器
|
|
86
|
+
target_handlers = []
|
|
87
|
+
|
|
88
|
+
if handlers:
|
|
89
|
+
# 如果可用,使用用户提供的处理器
|
|
90
|
+
target_handlers = handlers
|
|
91
|
+
else:
|
|
92
|
+
# 创建默认 Mermaid 处理器
|
|
93
|
+
# 此处理器知道如何写入 Mermaid 头部并格式化事件
|
|
94
|
+
handler = MermaidFileHandler(output_file)
|
|
95
|
+
handler.setFormatter(MermaidFormatter())
|
|
96
|
+
target_handlers = [handler]
|
|
97
|
+
|
|
98
|
+
if async_mode:
|
|
99
|
+
# 将目标处理器包装在 AsyncMermaidHandler (QueueHandler) 中
|
|
100
|
+
# QueueListener 将从队列中提取日志并分发到目标处理器
|
|
101
|
+
# 这将应用程序执行与日志 I/O 解耦
|
|
102
|
+
async_handler = AsyncMermaidHandler(target_handlers)
|
|
103
|
+
logger.addHandler(async_handler)
|
|
104
|
+
else:
|
|
105
|
+
# 直接将处理器附加到日志记录器以进行同步日志记录
|
|
106
|
+
# 简单可靠,适用于调试或低吞吐量应用程序
|
|
107
|
+
for h in target_handlers:
|
|
108
|
+
logger.addHandler(h)
|
|
109
|
+
|
|
110
|
+
return logger
|
|
111
|
+
|
|
112
|
+
try:
|
|
113
|
+
# 尝试检索已安装的包版本
|
|
114
|
+
__version__ = version("mermaid-trace")
|
|
115
|
+
except PackageNotFoundError:
|
|
116
|
+
# 如果包未安装(例如,本地开发),则使用回退版本
|
|
117
|
+
__version__ = "0.0.0"
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# 导出公共 API 以方便访问
|
|
121
|
+
__all__ = ["trace_interaction", "trace", "configure_flow", "MermaidFileHandler", "AsyncMermaidHandler", "LogContext", "FlowEvent", "MermaidFormatter"]
|
|
122
|
+
```
|