mermaid-trace 0.4.0__py3-none-any.whl → 0.5.3.post0__py3-none-any.whl
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/__init__.py +55 -21
- mermaid_trace/cli.py +159 -63
- mermaid_trace/core/config.py +55 -0
- mermaid_trace/core/context.py +83 -23
- mermaid_trace/core/decorators.py +440 -145
- mermaid_trace/core/events.py +46 -63
- mermaid_trace/core/formatter.py +257 -29
- mermaid_trace/core/utils.py +96 -0
- mermaid_trace/handlers/async_handler.py +156 -27
- mermaid_trace/handlers/mermaid_handler.py +162 -76
- mermaid_trace/integrations/__init__.py +4 -0
- mermaid_trace/integrations/fastapi.py +110 -34
- {mermaid_trace-0.4.0.dist-info → mermaid_trace-0.5.3.post0.dist-info}/METADATA +78 -11
- mermaid_trace-0.5.3.post0.dist-info/RECORD +19 -0
- mermaid_trace-0.4.0.dist-info/RECORD +0 -16
- {mermaid_trace-0.4.0.dist-info → mermaid_trace-0.5.3.post0.dist-info}/WHEEL +0 -0
- {mermaid_trace-0.4.0.dist-info → mermaid_trace-0.5.3.post0.dist-info}/entry_points.txt +0 -0
- {mermaid_trace-0.4.0.dist-info → mermaid_trace-0.5.3.post0.dist-info}/licenses/LICENSE +0 -0
mermaid_trace/core/context.py
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Log Context Management Module
|
|
3
|
+
|
|
4
|
+
This module provides a thread-safe, async-friendly context management system
|
|
5
|
+
for tracking execution context across the application. It uses Python's ContextVar
|
|
6
|
+
mechanism to ensure proper context propagation in both synchronous and asynchronous
|
|
7
|
+
environments.
|
|
8
|
+
"""
|
|
9
|
+
|
|
1
10
|
from contextvars import ContextVar, Token
|
|
2
11
|
from contextlib import asynccontextmanager, contextmanager
|
|
3
12
|
from typing import Any, AsyncIterator, Dict, Iterator
|
|
@@ -9,16 +18,14 @@ class LogContext:
|
|
|
9
18
|
Manages global context information for logging (e.g., request_id, user_id, current_participant).
|
|
10
19
|
|
|
11
20
|
This class utilizes `contextvars.ContextVar` to ensure thread-safety and
|
|
12
|
-
correct context propagation in asynchronous (asyncio) environments.
|
|
13
|
-
|
|
21
|
+
correct context propagation in asynchronous (asyncio) environments. Unlike
|
|
22
|
+
`threading.local()`, `ContextVar` works natively with Python's async/await
|
|
14
23
|
event loop, ensuring that context is preserved across `await` points but isolated
|
|
15
24
|
between different concurrent tasks.
|
|
16
25
|
"""
|
|
17
26
|
|
|
18
|
-
# ContextVar
|
|
19
|
-
#
|
|
20
|
-
# "log_context" is the name of the variable, useful for debugging.
|
|
21
|
-
# The default value is implicitly an empty state if not set (handled in _get_store).
|
|
27
|
+
# ContextVar stores a dictionary unique to the current execution context (Task/Thread)
|
|
28
|
+
# The name "log_context" is used for debugging purposes
|
|
22
29
|
_context_store: ContextVar[Dict[str, Any]] = ContextVar("log_context")
|
|
23
30
|
|
|
24
31
|
@classmethod
|
|
@@ -27,13 +34,19 @@ class LogContext:
|
|
|
27
34
|
Retrieves the current context dictionary.
|
|
28
35
|
|
|
29
36
|
If the context variable has not been set in the current context,
|
|
30
|
-
it
|
|
31
|
-
and
|
|
37
|
+
it creates a fresh empty dictionary, sets it to the contextvar,
|
|
38
|
+
and returns it. This prevents LookupError and ensures there's
|
|
39
|
+
always a valid dictionary to work with.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
Dict[str, Any]: Current context dictionary for the execution flow
|
|
32
43
|
"""
|
|
33
44
|
try:
|
|
34
45
|
return cls._context_store.get()
|
|
35
46
|
except LookupError:
|
|
36
|
-
|
|
47
|
+
empty_dict: Dict[str, Any] = {}
|
|
48
|
+
cls._context_store.set(empty_dict)
|
|
49
|
+
return empty_dict
|
|
37
50
|
|
|
38
51
|
@classmethod
|
|
39
52
|
def set(cls, key: str, value: Any) -> None:
|
|
@@ -42,10 +55,14 @@ class LogContext:
|
|
|
42
55
|
|
|
43
56
|
Important: ContextVars are immutable collections. To modify the context,
|
|
44
57
|
we must:
|
|
45
|
-
1. Retrieve the current dictionary
|
|
46
|
-
2. Create a shallow copy
|
|
47
|
-
3. Update the copy
|
|
48
|
-
4. Re-set the ContextVar with the new dictionary
|
|
58
|
+
1. Retrieve the current dictionary using _get_store()
|
|
59
|
+
2. Create a shallow copy to avoid affecting parent contexts
|
|
60
|
+
3. Update the copy with the new key-value pair
|
|
61
|
+
4. Re-set the ContextVar with the new dictionary
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
key (str): Name of the context variable to set
|
|
65
|
+
value (Any): Value to associate with the key
|
|
49
66
|
"""
|
|
50
67
|
ctx = cls._get_store().copy()
|
|
51
68
|
ctx[key] = value
|
|
@@ -57,7 +74,10 @@ class LogContext:
|
|
|
57
74
|
Updates multiple keys in the current context at once.
|
|
58
75
|
|
|
59
76
|
This follows the same Copy-Update-Set pattern as `set()` to maintain
|
|
60
|
-
context isolation.
|
|
77
|
+
context isolation between different execution flows.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
data (Dict[str, Any]): Dictionary of key-value pairs to update in context
|
|
61
81
|
"""
|
|
62
82
|
if not data:
|
|
63
83
|
return
|
|
@@ -69,6 +89,13 @@ class LogContext:
|
|
|
69
89
|
def get(cls, key: str, default: Any = None) -> Any:
|
|
70
90
|
"""
|
|
71
91
|
Retrieves a value from the current context safely.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
key (str): Name of the context variable to retrieve
|
|
95
|
+
default (Any, optional): Default value if key doesn't exist. Defaults to None.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
Any: Value associated with the key, or default if key not found
|
|
72
99
|
"""
|
|
73
100
|
return cls._get_store().get(key, default)
|
|
74
101
|
|
|
@@ -76,6 +103,9 @@ class LogContext:
|
|
|
76
103
|
def get_all(cls) -> Dict[str, Any]:
|
|
77
104
|
"""
|
|
78
105
|
Returns a copy of the entire context dictionary.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
Dict[str, Any]: Complete copy of the current context
|
|
79
109
|
"""
|
|
80
110
|
return cls._get_store().copy()
|
|
81
111
|
|
|
@@ -92,11 +122,17 @@ class LogContext:
|
|
|
92
122
|
# user_id reverts to previous value (or disappears) here
|
|
93
123
|
|
|
94
124
|
Mechanism:
|
|
95
|
-
1. Copies current context and updates it with new data
|
|
96
|
-
2. Sets the ContextVar to this new state, receiving a `Token
|
|
97
|
-
3. Yields control to the block
|
|
125
|
+
1. Copies current context and updates it with new data
|
|
126
|
+
2. Sets the ContextVar to this new state, receiving a `Token`
|
|
127
|
+
3. Yields control to the block
|
|
98
128
|
4. Finally, uses the `Token` to reset the ContextVar to its exact state
|
|
99
|
-
before the block entered
|
|
129
|
+
before the block entered
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
data (Dict[str, Any]): Dictionary of context values to set within the scope
|
|
133
|
+
|
|
134
|
+
Yields:
|
|
135
|
+
None: Control to the block using this context manager
|
|
100
136
|
"""
|
|
101
137
|
current_ctx = cls._get_store().copy()
|
|
102
138
|
current_ctx.update(data)
|
|
@@ -104,7 +140,7 @@ class LogContext:
|
|
|
104
140
|
try:
|
|
105
141
|
yield
|
|
106
142
|
finally:
|
|
107
|
-
#
|
|
143
|
+
# Reset restores context to state before .set() was called
|
|
108
144
|
cls._context_store.reset(token)
|
|
109
145
|
|
|
110
146
|
@classmethod
|
|
@@ -120,6 +156,12 @@ class LogContext:
|
|
|
120
156
|
This is functionally identical to `scope` but designed for `async with` blocks.
|
|
121
157
|
It ensures that even if the code inside `yield` suspends execution (await),
|
|
122
158
|
the context remains valid for that task.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
data (Dict[str, Any]): Dictionary of context values to set within the scope
|
|
162
|
+
|
|
163
|
+
Yields:
|
|
164
|
+
None: Control to the async block using this context manager
|
|
123
165
|
"""
|
|
124
166
|
current_ctx = cls._get_store().copy()
|
|
125
167
|
current_ctx.update(data)
|
|
@@ -137,6 +179,12 @@ class LogContext:
|
|
|
137
179
|
"""
|
|
138
180
|
Replaces the entire context with the provided data.
|
|
139
181
|
Returns a Token that can be used to manually reset the context later.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
data (Dict[str, Any]): New context dictionary to replace the current one
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
Token[Dict[str, Any]]: Token for resetting context to previous state
|
|
140
188
|
"""
|
|
141
189
|
return cls._context_store.set(data.copy())
|
|
142
190
|
|
|
@@ -144,21 +192,30 @@ class LogContext:
|
|
|
144
192
|
def reset(cls, token: Token[Dict[str, Any]]) -> None:
|
|
145
193
|
"""
|
|
146
194
|
Manually resets the context using a Token obtained from `set` or `set_all`.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
token (Token[Dict[str, Any]]): Token returned by set_all() method
|
|
147
198
|
"""
|
|
148
199
|
cls._context_store.reset(token)
|
|
149
200
|
|
|
150
201
|
@classmethod
|
|
151
202
|
def current_participant(cls) -> str:
|
|
152
203
|
"""
|
|
153
|
-
Helper to get the 'participant' field, representing the current active object/module.
|
|
204
|
+
Helper method to get the 'participant' field, representing the current active object/module.
|
|
154
205
|
Defaults to 'Unknown' if not set.
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
str: Name of the current participant in the trace flow
|
|
155
209
|
"""
|
|
156
210
|
return str(cls.get("participant", "Unknown"))
|
|
157
211
|
|
|
158
212
|
@classmethod
|
|
159
213
|
def set_participant(cls, name: str) -> None:
|
|
160
214
|
"""
|
|
161
|
-
Helper to set the 'participant' field.
|
|
215
|
+
Helper method to set the 'participant' field.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
name (str): Name of the participant to set
|
|
162
219
|
"""
|
|
163
220
|
cls.set("participant", name)
|
|
164
221
|
|
|
@@ -170,9 +227,12 @@ class LogContext:
|
|
|
170
227
|
Lazy Initialization Logic:
|
|
171
228
|
If no trace_id exists in the current context, it generates a new UUIDv4
|
|
172
229
|
and sets it immediately. This ensures that:
|
|
173
|
-
1. A trace ID is always available when asked for
|
|
230
|
+
1. A trace ID is always available when asked for
|
|
174
231
|
2. Once generated, the same ID persists for the duration of the context
|
|
175
|
-
(unless manually changed), linking all subsequent logs together
|
|
232
|
+
(unless manually changed), linking all subsequent logs together
|
|
233
|
+
|
|
234
|
+
Returns:
|
|
235
|
+
str: Unique trace ID for the current execution flow
|
|
176
236
|
"""
|
|
177
237
|
tid = cls.get("trace_id")
|
|
178
238
|
if not tid:
|