mingx 0.1.0__py3-none-any.whl → 0.1.1__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.
mingx/_trace.py
CHANGED
|
@@ -11,7 +11,7 @@ from typing import Any, Dict, Literal, Optional
|
|
|
11
11
|
|
|
12
12
|
from opentelemetry import trace
|
|
13
13
|
from opentelemetry.sdk.trace import TracerProvider
|
|
14
|
-
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter
|
|
14
|
+
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor, SpanExporter
|
|
15
15
|
from opentelemetry.trace import Tracer
|
|
16
16
|
|
|
17
17
|
from mingx._default_attributes import DefaultAttributesSpanProcessor
|
|
@@ -50,6 +50,7 @@ def configure_tracer_provider(
|
|
|
50
50
|
user_id: Optional[str] = None,
|
|
51
51
|
extra_attributes: Optional[Dict[str, Any]] = None,
|
|
52
52
|
use_baggage: bool = True,
|
|
53
|
+
use_batch_processor: bool = True,
|
|
53
54
|
) -> TracerProvider:
|
|
54
55
|
"""
|
|
55
56
|
Create and set the global TracerProvider with BatchSpanProcessor and OTLP export.
|
|
@@ -75,6 +76,8 @@ def configure_tracer_provider(
|
|
|
75
76
|
use_baggage: If True (default), read mingx.workspace_id, mingx.span_type,
|
|
76
77
|
mingx.user.id from Baggage and overlay on span (Baggage overrides defaults).
|
|
77
78
|
span_type 不在初始化时设置,由 @traced(span_type=...) 或适配器按调用类型设置。
|
|
79
|
+
use_batch_processor: If True (default), use BatchSpanProcessor,span 在 trace 结束或缓冲区满时批量上送;
|
|
80
|
+
If False, use SimpleSpanProcessor,每个 span 结束时立即上送,便于按节点完成顺序在服务端看到顺序。
|
|
78
81
|
|
|
79
82
|
Returns:
|
|
80
83
|
The TracerProvider that is now set as global.
|
|
@@ -101,7 +104,10 @@ def configure_tracer_provider(
|
|
|
101
104
|
auth_scheme=auth_scheme,
|
|
102
105
|
)
|
|
103
106
|
|
|
104
|
-
|
|
107
|
+
if use_batch_processor:
|
|
108
|
+
provider.add_span_processor(BatchSpanProcessor(exporter))
|
|
109
|
+
else:
|
|
110
|
+
provider.add_span_processor(SimpleSpanProcessor(exporter))
|
|
105
111
|
trace.set_tracer_provider(provider)
|
|
106
112
|
return provider
|
|
107
113
|
|
mingx/adapters/langchain.py
CHANGED
|
@@ -181,6 +181,7 @@ class OpenTelemetryCallbackHandler(BaseCallbackHandler):
|
|
|
181
181
|
trace_content: bool = True # 默认记录所有节点的入参(输入)与返回值(输出),无返回值记空
|
|
182
182
|
record_input_output_as: RecordInputOutputAs = "events" # 推荐 events,与 Traceloop 等一致
|
|
183
183
|
max_content_length: Optional[int] = None # 可选,单条 content 截断长度(字符)
|
|
184
|
+
root_span_name: Optional[str] = None # 可选,指定根 Span 名称(仅对无 parent_run_id 的 chain 生效)
|
|
184
185
|
|
|
185
186
|
def __init__(
|
|
186
187
|
self,
|
|
@@ -188,11 +189,13 @@ class OpenTelemetryCallbackHandler(BaseCallbackHandler):
|
|
|
188
189
|
trace_content: bool = True,
|
|
189
190
|
record_input_output_as: RecordInputOutputAs = "events",
|
|
190
191
|
max_content_length: Optional[int] = None,
|
|
192
|
+
root_span_name: Optional[str] = None,
|
|
191
193
|
) -> None:
|
|
192
194
|
super().__init__()
|
|
193
195
|
self.trace_content = trace_content
|
|
194
196
|
self.record_input_output_as = record_input_output_as
|
|
195
197
|
self.max_content_length = max_content_length
|
|
198
|
+
self.root_span_name = root_span_name
|
|
196
199
|
|
|
197
200
|
def _start_span(
|
|
198
201
|
self,
|
|
@@ -428,6 +431,10 @@ class OpenTelemetryCallbackHandler(BaseCallbackHandler):
|
|
|
428
431
|
kw_name = kwargs.get("name")
|
|
429
432
|
if kw_name is not None and str(kw_name).strip():
|
|
430
433
|
name = kw_name
|
|
434
|
+
# Root span 名称:无 parent_run_id 视为根 run 时,使用创建 handler 时指定的 root_span_name
|
|
435
|
+
parent_run_id = kwargs.get("parent_run_id")
|
|
436
|
+
if parent_run_id is None and self.root_span_name and str(self.root_span_name).strip():
|
|
437
|
+
name = str(self.root_span_name).strip()
|
|
431
438
|
if not isinstance(name, str):
|
|
432
439
|
name = str(name)
|
|
433
440
|
span_attrs = ChainSpanAttributes(run_id=str(run_id))
|
|
@@ -637,10 +644,12 @@ def get_langchain_callback(
|
|
|
637
644
|
trace_content: bool = True,
|
|
638
645
|
record_input_output_as: RecordInputOutputAs = "events",
|
|
639
646
|
max_content_length: Optional[int] = None,
|
|
647
|
+
root_span_name: Optional[str] = None,
|
|
640
648
|
) -> OpenTelemetryCallbackHandler:
|
|
641
649
|
"""Return a default OpenTelemetryCallbackHandler for LangChain/LangGraph. 默认记录所有节点入参与返回值。"""
|
|
642
650
|
return OpenTelemetryCallbackHandler(
|
|
643
651
|
trace_content=trace_content,
|
|
644
652
|
record_input_output_as=record_input_output_as,
|
|
645
653
|
max_content_length=max_content_length,
|
|
654
|
+
root_span_name=root_span_name,
|
|
646
655
|
)
|
mingx/decorator.py
CHANGED
|
@@ -40,6 +40,12 @@ def traced(
|
|
|
40
40
|
@traced(name="custom_span", span_type="model", attributes={"key": "value"})
|
|
41
41
|
def other(): ...
|
|
42
42
|
|
|
43
|
+
# 指定 root span 名称:入口函数用 name= 即可,该 span 即为 trace 的 root
|
|
44
|
+
@traced(name="my_agent")
|
|
45
|
+
def entry():
|
|
46
|
+
...
|
|
47
|
+
|
|
48
|
+
name: Span 名称;不传时使用「模块.函数名」或「函数名」。作为入口调用时,该 name 即为 root span name。
|
|
43
49
|
span_type: 设置 mingx.span_type,如 "model", "retriever", "tool", "chain", "agent" 等(可扩展)。
|
|
44
50
|
record_io: 是否记录函数入参(输入)与返回值(输出),默认 True,与适配器行为一致。
|
|
45
51
|
record_io_as: "events"(默认)| "attributes" | "none"。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mingx
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: OpenTelemetry trace Python SDK with GenAI semantic conventions, LangChain/LangGraph callbacks, and method-level @traced decorator
|
|
5
5
|
Requires-Python: >=3.10
|
|
6
6
|
Requires-Dist: opentelemetry-api>=1.20.0
|
|
@@ -70,12 +70,14 @@ configure_tracer_provider(
|
|
|
70
70
|
workspace_id="ws-001",
|
|
71
71
|
user_id="user-123",
|
|
72
72
|
)
|
|
73
|
+
# LangChain/LangGraph 等有节点先后顺序时,若希望按完成顺序上送 span,可设 use_batch_processor=False
|
|
74
|
+
# configure_tracer_provider(..., use_batch_processor=False)
|
|
73
75
|
|
|
74
76
|
@traced # 默认 record_io=True,会记录函数入参与返回值
|
|
75
77
|
def my_function():
|
|
76
78
|
...
|
|
77
79
|
|
|
78
|
-
#
|
|
80
|
+
# 指定 span 名称;作为入口调用时,该 name 即为 root span name
|
|
79
81
|
@traced(name="custom_span", span_type="model", attributes={"key": "value"})
|
|
80
82
|
def another():
|
|
81
83
|
...
|
|
@@ -92,6 +94,13 @@ chain.invoke({"input": "..."}, config={"callbacks": [handler]})
|
|
|
92
94
|
graph.invoke({"messages": [...]}, config={"callbacks": [handler]})
|
|
93
95
|
```
|
|
94
96
|
|
|
97
|
+
**指定 Root Span 名称**:创建 handler 时传入 `root_span_name`,仅对根 run 生效。
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
handler = get_langchain_callback(root_span_name="my_agent")
|
|
101
|
+
graph.invoke(..., config={"callbacks": [handler]})
|
|
102
|
+
```
|
|
103
|
+
|
|
95
104
|
### 手动上报(span_input / span_output)
|
|
96
105
|
|
|
97
106
|
```python
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
mingx/__init__.py,sha256=cTuHXXyEShdAzaApsgUYg_CA5DowqFWwMPUT45pbaB8,1482
|
|
2
2
|
mingx/_default_attributes.py,sha256=UGs3IuPSlInTlseVeU7cZvITcSBQSpg4HYv7Fz5RzPM,2682
|
|
3
|
-
mingx/_trace.py,sha256=
|
|
4
|
-
mingx/decorator.py,sha256=
|
|
3
|
+
mingx/_trace.py,sha256=NJv--Kh4FjIYn--B8EiyM7_WYV2IC9IOIdsfW-t-E4E,5969
|
|
4
|
+
mingx/decorator.py,sha256=nzTGp5SynFWCiy76cUbJOnUuP39SAw7dfMkyDmD4O2o,6947
|
|
5
5
|
mingx/adapters/__init__.py,sha256=shSWbWvkQTn0-yh3iBQWBn5OICEV5uoDDwLhDf8Rovw,496
|
|
6
6
|
mingx/adapters/base.py,sha256=hv0CPXgeBC0bxUtfnN-y0Q_jh7GxML9XxffK3CXB_34,2524
|
|
7
|
-
mingx/adapters/langchain.py,sha256=
|
|
7
|
+
mingx/adapters/langchain.py,sha256=dGvQktdGEsQJ8SaxT_keGazYBNRBXScpVoG2MT0kE7I,26917
|
|
8
8
|
mingx/genai/__init__.py,sha256=G9ygXhaM_uTU5q0hZoVhDkhRSaSERoYK02isiPRnkvY,2612
|
|
9
9
|
mingx/genai/attributes.py,sha256=ssRFX_YoyNNjYZrpct-Ixawtsz-zK6dlgR-7QgbVl8k,6376
|
|
10
10
|
mingx/genai/io.py,sha256=m4tYrzR4Kk2KxSjHApCf90gdssZWlB_WCzuBk1pQ_9w,17393
|
|
11
11
|
mingx/genai/span_attributes.py,sha256=UveEVnrXWNB10kk24dw0ZfVAxvxRNdmAyKJgk-FMfhs,5758
|
|
12
12
|
mingx/genai/spans.py,sha256=DWSOis5o2en9r_UM8UNqpjIXMqOyC2ru8tnGa8POsww,5259
|
|
13
|
-
mingx-0.1.
|
|
14
|
-
mingx-0.1.
|
|
15
|
-
mingx-0.1.
|
|
13
|
+
mingx-0.1.1.dist-info/METADATA,sha256=eOrELehyJu3VAxCYDwbuJF3nmdlAuOVDZ9Xi2zwwZx0,15324
|
|
14
|
+
mingx-0.1.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
15
|
+
mingx-0.1.1.dist-info/RECORD,,
|
|
File without changes
|