File tree Expand file tree Collapse file tree 5 files changed +93
-9
lines changed
httpfunctions/coze-python Expand file tree Collapse file tree 5 files changed +93
-9
lines changed Original file line number Diff line number Diff line change @@ -15,4 +15,7 @@ COZE_BOT_ID=your_bot_id_here
1515# COZE_API_BASE=https://api.coze.cn
1616
1717# Optional: Debug mode (enable detailed logging)
18- # DEBUG=true
18+ # DEBUG=true
19+
20+ # Trace log to stdout (已通过代码配置启用,无需环境变量)
21+ # AUTO_TRACES_STDOUT=true
Original file line number Diff line number Diff line change @@ -49,6 +49,9 @@ python -m pip install -r ./requirements.txt \
4949``` bash
5050COZE_API_TOKEN=your_api_token_here
5151COZE_BOT_ID=your_bot_id_here
52+
53+ # Trace log to stdout(可选)
54+ AUTO_TRACES_STDOUT=true
5255```
5356
5457** 环境变量说明** :
@@ -398,3 +401,68 @@ A: 通过客户端请求的 `forwarded_props.parameters` 动态传递,它会
398401** Q: 支持哪些 Coze API 功能?**
399402A: 支持 Coze Chat V3 API 的所有功能,包括流式响应和推理内容。
400403
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 @@ -51,16 +51,19 @@ def create_jwt_request_preprocessor():
5151
5252 Extracts user_id from JWT 'sub' field in Authorization header
5353 and writes to request.forwarded_props.user_id.
54+
55+ Note: This is a middleware generator following the onion model.
5456 """
5557 from ag_ui .core import RunAgentInput
5658 from fastapi import Request
5759
58- def jwt_preprocessor (request : RunAgentInput , http_context : Request ) -> None :
60+ def jwt_preprocessor (request : RunAgentInput , http_context : Request ):
5961 user_id = extract_user_id_from_request (http_context )
6062 if user_id :
6163 if not request .forwarded_props :
6264 request .forwarded_props = {}
6365 request .forwarded_props ["user_id" ] = user_id
66+ yield # Required for middleware generator pattern
6467
6568 return jwt_preprocessor
6669
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+ value = os .environ .get ("AUTO_TRACES_STDOUT" , "" ).lower ()
17+ return value not in ("false" , "0" )
18+
19+
1320def main ():
1421 """Application entry point."""
1522 try :
1623 agent = build_coze_agent ()
17- AgentServiceApp ().run (
18- lambda : {"agent" : agent },
19- request_preprocessor = create_jwt_request_preprocessor (),
20- )
24+
25+ observability = ConsoleTraceConfig () if is_observability_enabled () else None
26+
27+ app = AgentServiceApp (observability = observability )
28+ app .use (create_jwt_request_preprocessor ())
29+ app .run (lambda : {"agent" : agent })
2130 except ValueError as e :
2231 print (f"Configuration Error: { e } " , file = sys .stderr )
2332 sys .exit (1 )
Original file line number Diff line number Diff line change 1- cloudbase-agent-core == 0.1.11
2- cloudbase-agent-server == 0.1.16
3- cloudbase-agent-coze == 0.1.17
1+ cloudbase-agent-core == 0.1.12
2+ cloudbase-agent-server == 0.1.21
3+ cloudbase-agent-coze == 0.1.18
4+ cloudbase-agent-observability == 0.1.2
45cozepy >= 0.20.0
56python-dotenv >= 1.2.1
You can’t perform that action at this time.
0 commit comments