File tree Expand file tree Collapse file tree 3 files changed +83
-3
lines changed
httpfunctions/coze-python Expand file tree Collapse file tree 3 files changed +83
-3
lines changed Original file line number Diff line number Diff line change @@ -17,5 +17,5 @@ COZE_BOT_ID=your_bot_id_here
1717# Optional: Debug mode (enable detailed logging)
1818# DEBUG=true
1919
20- # Trace log to stdout
21- AUTO_TRACES_STDOUT = true
20+ # Trace log to stdout (已通过代码配置启用,无需环境变量)
21+ # AUTO_TRACES_STDOUT=true
Original file line number Diff line number Diff line change @@ -401,3 +401,68 @@ A: 通过客户端请求的 `forwarded_props.parameters` 动态传递,它会
401401** Q: 支持哪些 Coze API 功能?**
402402A: 支持 Coze Chat V3 API 的所有功能,包括流式响应和推理内容。
403403
404+ ---
405+
406+ ## 可观测性配置
407+
408+ 本项目支持 OpenTelemetry 协议的可观测性(Observability)功能,可以追踪 Coze Agent 的执行链路(traces)并导出到控制台或 OTLP 后端(如 Langfuse、Jaeger 等)。
409+
410+ ### 启用方式
411+
412+ 本项目提供两种启用可观测性的方式:
413+
414+ #### 方式一:环境变量(推荐用于部署环境)
415+
416+ 在 ` .env ` 文件中设置:
417+
418+ ``` bash
419+ # 启用可观测性(设为 true、1、yes 均可启用,设为 false 或 0 则关闭)
420+ AUTO_TRACES_STDOUT=true
421+ ```
422+
423+ 或在 CloudBase 云函数控制台的环境变量设置中,添加:
424+
425+ | 变量名 | 值 |
426+ | --------| -----|
427+ | ` AUTO_TRACES_STDOUT ` | ` true ` |
428+
429+ #### 方式二:代码配置(推荐用于开发调试)
430+
431+ 在 ` app.py ` 中修改 ` AgentServiceApp ` 的初始化:
432+
433+ ``` python
434+ from cloudbase_agent.observability.server import ConsoleTraceConfig
435+
436+ # 显式传入可观测性配置
437+ AgentServiceApp(observability = ConsoleTraceConfig()).run(lambda : {" agent" : agent})
438+ ```
439+
440+ ### 关闭可观测性
441+
442+ 如需关闭可观测性功能,可采用以下任一方式:
443+
444+ ** 方式一:本地开发(.env 文件)**
445+
446+ ``` bash
447+ # 关闭可观测性
448+ AUTO_TRACES_STDOUT=false
449+ ```
450+
451+ ** 方式二:云函数控制台(部署环境)**
452+
453+ 在 CloudBase 云函数控制台的环境变量设置中,添加:
454+
455+ | 变量名 | 值 |
456+ | --------| -----|
457+ | ` AUTO_TRACES_STDOUT ` | ` false ` |
458+
459+ ** 方式三:代码配置**
460+
461+ ``` python
462+ AgentServiceApp(observability = None ).run(lambda : {" agent" : agent})
463+ ```
464+
465+ ### 输出格式
466+
467+ 启用后, traces 将以 JSON 格式输出到 stdout,每行一个 span,便于使用 ` grep ` 、` jq ` 等工具分析。
468+
Original file line number Diff line number Diff line change 11# -*- coding: utf-8 -*-
22"""Coze Agent Application Entry Point."""
33
4+ import os
45import sys
56from dotenv import load_dotenv
67
78load_dotenv ()
89
910from cloudbase_agent .server import AgentServiceApp
11+ from cloudbase_agent .observability .server import ConsoleTraceConfig
1012from agent import build_coze_agent , create_jwt_request_preprocessor
1113
1214
15+ def is_observability_enabled ():
16+ """判断是否启用可观测性
17+
18+ 读取 AUTO_TRACES_STDOUT 环境变量,为 "false" 时不启用
19+ """
20+ value = os .environ .get ("AUTO_TRACES_STDOUT" , "" ).lower ()
21+ return value not in ("false" , "0" )
22+
23+
1324def main ():
1425 """Application entry point."""
1526 try :
1627 agent = build_coze_agent ()
17- app = AgentServiceApp ()
28+
29+ # 根据环境变量决定是否启用可观测性
30+ observability = ConsoleTraceConfig () if is_observability_enabled () else None
31+
32+ app = AgentServiceApp (observability = observability )
1833 app .use (create_jwt_request_preprocessor ())
1934 app .run (lambda : {"agent" : agent })
2035 except ValueError as e :
You can’t perform that action at this time.
0 commit comments