Skip to content

Commit cd39e66

Browse files
committed
add docs for tracing
Signed-off-by: Mustafa974 <920998500@qq.com>
1 parent 8118405 commit cd39e66

File tree

3 files changed

+198
-2
lines changed

3 files changed

+198
-2
lines changed

lazyllm/docs/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
config.add('init_doc', bool, False, 'INIT_DOC', description='whether to init docs')
55
if config['init_doc'] and (add_doc.__doc__ is None or 'Add document' not in add_doc.__doc__):
6-
from . import common, components, configs, flow, hook, launcher, module, patch, prompt_template, tools, utils # noqa F401
7-
del common, components, configs, flow, hook, launcher, module, patch, prompt_template, tools, utils
6+
from . import common, components, configs, flow, hook, launcher, module, patch, prompt_template, tools, tracing, utils # noqa F401
7+
del common, components, configs, flow, hook, launcher, module, patch, prompt_template, tools, tracing, utils
88

99
__all__ = [
1010
'add_doc'

lazyllm/docs/hook.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@
116116
This is an abstract method and must be implemented in subclasses.
117117
''')
118118

119+
add_chinese_doc('HookPhaseError', '''\
120+
Hook 阶段错误,当一个 hook 阶段中有一个或多个 strict 模式的 hook 执行失败时抛出。
121+
122+
Args:
123+
phase (str): 发生错误的 hook 阶段名称(如 ``'post_hook'``、``'on_error'``、``'report'``)。
124+
errors: 包含 ``(hook_obj, exception)`` 元组的序列,记录所有失败的 hook 及其异常。
125+
''')
126+
127+
add_english_doc('HookPhaseError', '''\
128+
Raised when one or more strict-mode hooks fail during a hook phase.
129+
130+
Args:
131+
phase (str): The name of the hook phase where the error(s) occurred (e.g. ``'post_hook'``, ``'on_error'``, ``'report'``).
132+
errors: A sequence of ``(hook_obj, exception)`` tuples recording each failed hook and its exception.
133+
''')
134+
119135
add_chinese_doc('LazyTracingHook', '''\
120136
为 flow 或 module 创建 tracing hook。
121137

lazyllm/docs/tracing.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# flake8: noqa E501
2+
from . import utils
3+
import functools
4+
import lazyllm
5+
6+
# ============= TracingBackend
7+
8+
add_chinese_doc = functools.partial(utils.add_chinese_doc, module=lazyllm.tracing.backends.base)
9+
add_english_doc = functools.partial(utils.add_english_doc, module=lazyllm.tracing.backends.base)
10+
11+
add_chinese_doc('TracingBackend', '''\
12+
追踪后端的抽象基类,定义了将 LazyLLM 追踪数据导出至外部可观测平台所需的接口。
13+
14+
子类需要实现所有抽象方法,以适配具体的可观测后端(如 Langfuse、Jaeger 等)。
15+
16+
**注意**: 此类是抽象基类,不能直接实例化。
17+
''')
18+
19+
add_english_doc('TracingBackend', '''\
20+
Abstract base class for tracing backends, defining the interface required to export
21+
LazyLLM tracing data to external observability platforms.
22+
23+
Subclasses must implement all abstract methods to adapt to a specific observability
24+
backend (e.g. Langfuse, Jaeger, etc.).
25+
26+
**Note**: This class is an abstract base class and cannot be instantiated directly.
27+
''')
28+
29+
add_chinese_doc('TracingBackend.build_exporter', '''\
30+
构建并返回一个 OpenTelemetry SpanExporter 实例,用于将 Span 数据发送至目标后端。
31+
32+
Returns:
33+
opentelemetry.sdk.trace.export.SpanExporter: 配置好的 Span 导出器。
34+
35+
Raises:
36+
RuntimeError: 当必要的后端配置缺失时抛出。
37+
''')
38+
39+
add_english_doc('TracingBackend.build_exporter', '''\
40+
Build and return an OpenTelemetry SpanExporter instance for sending span data to
41+
the target backend.
42+
43+
Returns:
44+
opentelemetry.sdk.trace.export.SpanExporter: A configured span exporter.
45+
46+
Raises:
47+
RuntimeError: If required backend configuration is missing.
48+
''')
49+
50+
add_chinese_doc('TracingBackend.context_attributes', '''\
51+
将追踪上下文转换为后端特定的 Span 属性。
52+
53+
Args:
54+
trace_ctx (Dict[str, Any]): 当前请求的追踪上下文,包含 ``session_id``、``user_id``、``request_tags`` 等字段。
55+
is_root_span (bool): 当前 Span 是否为调用链的根 Span。
56+
57+
Returns:
58+
Dict[str, Any]: 后端特定的 Span 属性字典。
59+
''')
60+
61+
add_english_doc('TracingBackend.context_attributes', '''\
62+
Convert the trace context into backend-specific span attributes.
63+
64+
Args:
65+
trace_ctx (Dict[str, Any]): The current request trace context containing fields such as ``session_id``, ``user_id``, and ``request_tags``.
66+
is_root_span (bool): Whether the current span is the root span of the trace.
67+
68+
Returns:
69+
Dict[str, Any]: A dictionary of backend-specific span attributes.
70+
''')
71+
72+
add_chinese_doc('TracingBackend.input_attributes', '''\
73+
将调用输入转换为后端特定的 Span 属性。
74+
75+
Args:
76+
args (tuple): 传递给目标对象的位置参数。
77+
kwargs (Dict[str, Any]): 传递给目标对象的关键字参数。
78+
capture_payload (bool): 是否记录输入 payload 内容。
79+
is_root_span (bool): 当前 Span 是否为调用链的根 Span。
80+
81+
Returns:
82+
Dict[str, Any]: 后端特定的输入属性字典。若 ``capture_payload`` 为 False,返回空字典。
83+
''')
84+
85+
add_english_doc('TracingBackend.input_attributes', '''\
86+
Convert call inputs into backend-specific span attributes.
87+
88+
Args:
89+
args (tuple): Positional arguments passed to the target object.
90+
kwargs (Dict[str, Any]): Keyword arguments passed to the target object.
91+
capture_payload (bool): Whether to record the input payload content.
92+
is_root_span (bool): Whether the current span is the root span of the trace.
93+
94+
Returns:
95+
Dict[str, Any]: A dictionary of backend-specific input attributes. Returns an empty dict when ``capture_payload`` is False.
96+
''')
97+
98+
add_chinese_doc('TracingBackend.set_root_span_name', '''\
99+
为根 Span 设置后端特定的显示名称。
100+
101+
Args:
102+
span: OpenTelemetry Span 对象。
103+
span_name (str): 要设置的 Span 名称。
104+
''')
105+
106+
add_english_doc('TracingBackend.set_root_span_name', '''\
107+
Set a backend-specific display name on the root span.
108+
109+
Args:
110+
span: The OpenTelemetry span object.
111+
span_name (str): The name to assign to the span.
112+
''')
113+
114+
add_chinese_doc('TracingBackend.output_attributes', '''\
115+
将调用输出转换为后端特定的 Span 属性。
116+
117+
Args:
118+
text (str): 序列化后的输出文本。
119+
is_root_span (bool): 当前 Span 是否为调用链的根 Span。
120+
121+
Returns:
122+
Dict[str, Any]: 后端特定的输出属性字典。
123+
''')
124+
125+
add_english_doc('TracingBackend.output_attributes', '''\
126+
Convert call output into backend-specific span attributes.
127+
128+
Args:
129+
text (str): The serialized output text.
130+
is_root_span (bool): Whether the current span is the root span of the trace.
131+
132+
Returns:
133+
Dict[str, Any]: A dictionary of backend-specific output attributes.
134+
''')
135+
136+
add_chinese_doc('TracingBackend.error_attributes', '''\
137+
将异常信息转换为后端特定的 Span 属性。
138+
139+
Args:
140+
exc (Exception): 执行过程中抛出的异常对象。
141+
142+
Returns:
143+
Dict[str, Any]: 后端特定的错误属性字典。
144+
''')
145+
146+
add_english_doc('TracingBackend.error_attributes', '''\
147+
Convert exception information into backend-specific span attributes.
148+
149+
Args:
150+
exc (Exception): The exception raised during execution.
151+
152+
Returns:
153+
Dict[str, Any]: A dictionary of backend-specific error attributes.
154+
''')
155+
156+
# ============= LangfuseBackend
157+
158+
add_chinese_doc_lf = functools.partial(utils.add_chinese_doc, module=lazyllm.tracing.backends.langfuse)
159+
add_english_doc_lf = functools.partial(utils.add_english_doc, module=lazyllm.tracing.backends.langfuse)
160+
161+
add_chinese_doc_lf('LangfuseBackend', '''\
162+
Langfuse 追踪后端实现,通过 OTLP/HTTP 协议将追踪数据导出至 Langfuse 平台。
163+
164+
使用 HTTP Basic Auth 认证,需要配置以下环境变量:
165+
166+
- ``LANGFUSE_HOST`` 或 ``LANGFUSE_BASE_URL``: Langfuse 服务地址
167+
- ``LANGFUSE_PUBLIC_KEY``: Langfuse 公钥
168+
- ``LANGFUSE_SECRET_KEY``: Langfuse 密钥
169+
''')
170+
171+
add_english_doc_lf('LangfuseBackend', '''\
172+
Langfuse tracing backend implementation that exports trace data to the Langfuse platform
173+
via OTLP/HTTP protocol.
174+
175+
Uses HTTP Basic Auth authentication. The following environment variables must be configured:
176+
177+
- ``LANGFUSE_HOST`` or ``LANGFUSE_BASE_URL``: Langfuse service URL
178+
- ``LANGFUSE_PUBLIC_KEY``: Langfuse public key
179+
- ``LANGFUSE_SECRET_KEY``: Langfuse secret key
180+
''')

0 commit comments

Comments
 (0)