使用 OpenTelemetry 監控 Agent 使用狀況

本文說明如何在 VS Code 中啟用並設定 Copilot Chat Agent 互動的 OpenTelemetry 監控。

Copilot Chat 可透過 OpenTelemetry (OTel) 匯出追蹤 (Traces)、指標 (Metrics) 與事件 (Events),讓您能掌握 Agent 互動、LLM 呼叫、工具執行與 Token 使用量。所有訊號名稱與屬性均遵循 OTel GenAI 語意慣例,因此資料可與任何相容 OTel 的後端搭配使用。

資料收集內容

Copilot Chat 會發出三種類型的 OTel 訊號:追蹤、指標與事件。

屬性命名空間 (Attribute namespaces)

Copilot Chat 在三個命名空間下發出 OTel 屬性

命名空間 來源 使用時機
gen_ai.* OTel GenAI 語意慣例 當存在標準鍵 (key) 時使用
github.copilot.* 規範的 Copilot 專用命名空間,與 GitHub Copilot CLI OpenTelemetry 慣例共用 建議用於新的儀表板、警示與查詢
copilot_chat.* 原始 VS Code 擴充功能命名空間 舊版。現在有幾個鍵會與 github.copilot.* 對應項目同時發出

舊版 copilot_chat.* 鍵將無限期持續發出,以便現有的收集器、儀表板與下游消費者能持續運作而不需變更。目前沒有停用計畫。本節表格將同時發出的資料列標記為「舊版 (Legacy)」,並指向建議使用的鍵。

追蹤 (Traces)

每個 Agent 互動都會產生一個階層式 Span 樹狀結構,用以捕捉完整的執行流程

invoke_agent copilot                           [~15s]
  ├── chat gpt-4o                              [~3s]  (LLM requests tool calls)
  ├── execute_tool readFile                    [~50ms]
  ├── execute_tool runCommand                  [~2s]
  ├── chat gpt-4o                              [~4s]  (LLM generates final response)
  └── (span ends)

當 Agent 呼叫子 Agent (例如透過 runSubagent 工具) 時,追蹤內容會自動傳遞。子 Agent 的 invoke_agent Span 會顯示為父 Agent execute_tool Span 的子項目,從而在非同步邊界產生連貫的追蹤樹。

invoke_agent Span

包裝整個 Agent 編排,包含所有 LLM 呼叫與工具執行。

屬性 說明
gen_ai.operation.name 固定為 invoke_agent
gen_ai.provider.name 供應商 (例如 github)
gen_ai.agent.name Agent 名稱 (例如 copilot, copilotcli, claude)
gen_ai.conversation.id 對話工作階段 ID
gen_ai.request.model 請求的模型
gen_ai.response.model 解析後的模型
gen_ai.usage.input_tokens 工作階段中的總輸入 Token 數
gen_ai.usage.output_tokens 工作階段中的總輸出 Token 數
gen_ai.usage.cache_read.input_tokens 快取讀取的輸入 Token 數 (若可用)
gen_ai.usage.cache_creation.input_tokens 快取建立的輸入 Token 數 (若可用)
github.copilot.agent.type builtin, customplugin
github.copilot.git.repository Git 儲存庫的遠端 URL (若在 Git 儲存庫中)
github.copilot.git.branch 作用中分支 (若在 Git 儲存庫中)
github.copilot.git.commit_sha 當前 Commit SHA (若在 Git 儲存庫中)
github.copilot.github.org GitHub 組織擁有者 (僅限 GitHub 遠端)
copilot_chat.repo.remote_url 舊版。建議使用 github.copilot.git.repository
copilot_chat.repo.head_branch_name 舊版。建議使用 github.copilot.git.branch
copilot_chat.repo.head_commit_hash 舊版。建議使用 github.copilot.git.commit_sha
copilot_chat.turn_count 此工作階段中的 LLM 來回對話次數
error.type 錯誤類別 (發生失敗時)
gen_ai.input.messages 完整的提示訊息 (僅內容擷取)
gen_ai.output.messages 完整的回應訊息 (僅內容擷取)
gen_ai.tool.definitions 工具定義結構 (僅內容擷取)

chat Span

每個 LLM API 呼叫一個 Span。

屬性 說明
gen_ai.operation.name 固定為 chat
gen_ai.provider.name 供應商名稱
gen_ai.request.model 請求的模型
gen_ai.response.model 解析後的模型
gen_ai.response.finish_reasons 停止原因 (例如 ["stop"])
gen_ai.request.max_tokens 最大輸出 Token 數
gen_ai.request.temperature Temperature 設定值
gen_ai.request.top_p Top-p 設定值
gen_ai.usage.input_tokens 此呼叫的輸入 Token 數
gen_ai.usage.output_tokens 此呼叫的輸出 Token 數
gen_ai.usage.cache_read.input_tokens 快取讀取的輸入 Token 數 (若可用)
gen_ai.usage.cache_creation.input_tokens 快取建立的輸入 Token 數 (若可用)
gen_ai.usage.reasoning.output_tokens 推理 Token 數 (若可用)
gen_ai.usage.reasoning_tokens 舊版。建議使用 gen_ai.usage.reasoning.output_tokens
copilot_chat.time_to_first_token 首個 SSE Token 的回應時間 (毫秒)
server.address API 主機名稱
error.type 錯誤類別 (發生失敗時)

execute_tool Span

每個工具呼叫一個 Span。

屬性 說明
gen_ai.operation.name 固定為 execute_tool
gen_ai.tool.name 工具名稱 (例如 readFile)
gen_ai.tool.type functionextension (MCP 工具)
gen_ai.tool.call.id 工具呼叫識別碼
github.copilot.tool.parameters.edit_type 針對編輯工具:create, update, str_replaceinsert
github.copilot.tool.parameters.skill_name 針對技能呼叫
github.copilot.tool.parameters.mcp_server_name_hash 針對 MCP 工具:伺服器名稱的 SHA-256 值
github.copilot.tool.parameters.mcp_tool_name 針對 MCP 工具:已呼叫的工具名稱
github.copilot.tool.parameters.command 針對 Shell 工具 (僅內容擷取,已截斷)
github.copilot.tool.parameters.file_path 針對檔案工具 (僅內容擷取)
github.copilot.tool.parameters.mcp_server_name 針對 MCP 工具 (僅內容擷取)
error.type 錯誤類別 (發生失敗時)
gen_ai.tool.call.arguments 工具輸入參數 (僅內容擷取)
gen_ai.tool.call.result 工具輸出 (僅內容擷取)

execute_hook Span

每個 Hook 執行一個 Span (例如 PreToolUse, Stop)。

屬性 說明
gen_ai.operation.name 固定為 execute_hook
github.copilot.hook.decision pass, blocknon_blocking_error
github.copilot.hook.duration Hook 執行持續時間 (秒)
github.copilot.hook.tool_names Hook 作用範圍內的工具 (JSON 陣列)
copilot_chat.hook_type Hook 事件 (例如 PreToolUse)
copilot_chat.hook_result_kind success, errornon_blocking_error
copilot_chat.hook_input Hook 輸入負載 (已截斷)
copilot_chat.hook_output Hook 標準輸出,成功時 (已截斷)
error.type 錯誤類別 (發生失敗時)

指標 (Metrics)

GenAI 語意慣例指標

指標 類型 說明
gen_ai.client.operation.duration 長條圖 (Histogram) LLM API 呼叫持續時間 (秒)
gen_ai.client.token.usage 長條圖 (Histogram) Token 數量 (輸入與輸出)

擴充功能專用指標

指標 類型 說明
copilot_chat.tool.call.count 計數器 (Counter) 依名稱與成功與否統計的工具呼叫數
copilot_chat.tool.call.duration 長條圖 (Histogram) 工具執行延遲 (毫秒)
copilot_chat.agent.invocation.duration 長條圖 (Histogram) Agent 端對端持續時間 (秒)
copilot_chat.agent.turn.count 長條圖 (Histogram) 每次 Agent 呼叫的 LLM 來回對話次數
copilot_chat.session.count 計數器 (Counter) 已啟動的 Chat 工作階段數
copilot_chat.time_to_first_token 長條圖 (Histogram) 首個 SSE Token 的回應時間 (秒)

Agent 活動與結果指標可追蹤跨所有介面 (內聯聊天、本地 Agent、Copilot CLI Agent、Claude Agent 與 Copilot Coding Agent) 的 Agent 程式碼變更。

指標 類型 說明
copilot_chat.edit.acceptance.count 計數器 (Counter) 編輯接受與拒絕決策 (內聯聊天、聊天編輯、Hunk 級別)
copilot_chat.chat_edit.outcome.count 計數器 (Counter) 檔案級別聊天編輯工作階段結果 (已接受、已拒絕、已儲存)
copilot_chat.lines_of_code.count 計數器 (Counter) 已接受的 Agent 編輯所新增或移除的程式碼行數
copilot_chat.edit.survival.four_gram 長條圖 (Histogram) 4-gram 文字相似度存留分數 (0-1)
copilot_chat.edit.survival.no_revert 長條圖 (Histogram) 無還原存留分數 (0-1)
copilot_chat.user.action.count 計數器 (Counter) 使用者參與動作:複製、插入、套用、後續追蹤
copilot_chat.user.feedback.count 計數器 (Counter) 針對聊天回應的按讚與倒讚投票
copilot_chat.agent.edit_response.count 計數器 (Counter) 按成功或錯誤統計的 Agent 編輯回應
copilot_chat.agent.summarization.count 計數器 (Counter) 內容摘要結果 (已套用、失敗)
copilot_chat.pull_request.count 計數器 (Counter) 透過 CLI Agent 建立的 Pull Request 數
copilot_chat.cloud.session.count 計數器 (Counter) 依合作夥伴 Agent 統計的雲端與遠端 Agent 工作階段數
copilot_chat.cloud.pr_ready.count 計數器 (Counter) 遠端 Agent 工作完成後的 PR 就緒通知

指標包含用於篩選的屬性,例如 gen_ai.request.model, gen_ai.provider.name, gen_ai.tool.name, copilot_chat.edit.sourceerror.type

活動

事件 (Event) 說明
gen_ai.client.inference.operation.details 包含模型、Token 與結束原因的完整 LLM 呼叫中繼資料
copilot_chat.session.start 新的聊天工作階段開始時發出
copilot_chat.tool.call 包含計時與錯誤詳細資料的個別工具呼叫
copilot_chat.agent.turn 包含 Token 數的每回合 LLM 來回對話
copilot_chat.edit.feedback 使用者接受或拒絕了檔案級別的 Agent 編輯
copilot_chat.edit.hunk.action 使用者接受或拒絕了個別的 Hunk (程式碼區塊)
copilot_chat.inline.done 內聯聊天編輯已被接受或拒絕
copilot_chat.edit.survival 定期測量 AI 生成的程式碼在接受後存留了多少
copilot_chat.user.feedback 使用者對聊天回應進行了投票 (按讚或倒讚)
copilot_chat.cloud.session.invoke 已啟動雲端或遠端 Agent 工作階段

資源屬性 (Resource attributes)

所有訊號都帶有這些資源屬性

屬性
service.name copilot-chat (可使用 OTEL_SERVICE_NAME 設定)
service.version 擴充功能版本
session.id 每個 VS Code 視窗唯一的 ID

使用 OTEL_RESOURCE_ATTRIBUTES 新增自訂資源屬性,以便依團隊、部門或其他組織邊界進行篩選

export OTEL_RESOURCE_ATTRIBUTES="team.id=platform,department=engineering"

內容擷取

預設情況下,不會擷取提示內容、回應或工具參數。僅包含模型名稱、Token 數與持續時間等中繼資料。

若要擷取完整內容,請啟用 github.copilot.chat.otel.captureContent 在 VS Code 中開啟 在 VS Code Insiders 中開啟 設定,或設定 COPILOT_OTEL_CAPTURE_CONTENT=true。這會將完整的提示訊息、回應訊息、系統提示、工具定義結構、工具參數與工具結果填入 Span 屬性中。

注意

內容擷取可能包含敏感資訊,例如程式碼、檔案內容與使用者提示。請僅在受信任的環境中啟用此功能。

啟用 OTel 監控

當以下任一條件為真時,OTel 即會啟動

  • github.copilot.chat.otel.enabled 在 VS Code 中開啟 在 VS Code Insiders 中開啟 true
  • github.copilot.chat.otel.dbSpanExporter.enabled 在 VS Code 中開啟 在 VS Code Insiders 中開啟 true
  • COPILOT_OTEL_ENABLED=true
  • 已設定 OTEL_EXPORTER_OTLP_ENDPOINT

VS Code 設定

開啟設定 (⌘, (Windows, Linux Ctrl+,)) 並搜尋 copilot otel

設定 類型 預設值 說明
github.copilot.chat.otel.enabled 在 VS Code 中開啟 在 VS Code Insiders 中開啟 boolean false 啟用 OTel 發送
github.copilot.chat.otel.exporterType 在 VS Code 中開啟 在 VS Code Insiders 中開啟 string "otlp-http" otlp-http, otlp-grpc, consolefile
github.copilot.chat.otel.otlpEndpoint 在 VS Code 中開啟 在 VS Code Insiders 中開啟 string "https://:4318" OTLP 收集器端點
github.copilot.chat.otel.captureContent 在 VS Code 中開啟 在 VS Code Insiders 中開啟 boolean false 擷取完整提示與回應內容
github.copilot.chat.otel.maxAttributeSizeChars 在 VS Code 中開啟 在 VS Code Insiders 中開啟 整數 0 每個內容屬性的最大字元數 (提示、工具參數、工具結果)。0 表示停用截斷。設定一個正值以符合您後端的每個屬性大小限制。
github.copilot.chat.otel.outfile 在 VS Code 中開啟 在 VS Code Insiders 中開啟 string "" JSON-lines 輸出的檔案路徑
github.copilot.chat.otel.dbSpanExporter.enabled 在 VS Code 中開啟 在 VS Code Insiders 中開啟 boolean false 將 OTel Span 持續寫入本地 SQLite 資料庫,供 Chat: Export Agent Traces DB 命令使用。這會隱含啟用 OTel。

環境變數

環境變數的優先順序永遠高於 VS Code 設定。

變數 預設值 說明
COPILOT_OTEL_ENABLED false 啟用 OTel。當設定 OTEL_EXPORTER_OTLP_ENDPOINT 時也會啟用。
COPILOT_OTEL_ENDPOINT OTLP 端點 URL (優先順序高於 OTEL_EXPORTER_OTLP_ENDPOINT)
OTEL_EXPORTER_OTLP_ENDPOINT 標準 OTel OTLP 端點 URL
OTEL_EXPORTER_OTLP_PROTOCOL http/protobuf OTLP 通訊協定。只有 grpc 會改變行為。
COPILOT_OTEL_PROTOCOL 覆寫 OTLP 通訊協定 (grpchttp)。優先順序高於 OTEL_EXPORTER_OTLP_PROTOCOL
OTEL_SERVICE_NAME copilot-chat 資源屬性中的服務名稱
OTEL_RESOURCE_ATTRIBUTES 額外的資源屬性 (key1=val1,key2=val2)
COPILOT_OTEL_CAPTURE_CONTENT false 擷取完整提示與回應內容
COPILOT_OTEL_MAX_ATTRIBUTE_SIZE_CHARS 0 覆寫內容屬性的最大字元大小。0 表示停用截斷。優先順序高於 maxAttributeSizeChars 設定。
COPILOT_OTEL_LOG_LEVEL info 最低日誌層級:trace, debug, info, warnerror
COPILOT_OTEL_FILE_EXPORTER_PATH 將所有訊號以 JSON lines 格式寫入此檔案。
COPILOT_OTEL_HTTP_INSTRUMENTATION false 啟用 HTTP 層級的 OTel 檢測。
OTEL_EXPORTER_OTLP_HEADERS 驗證標頭 (例如 Authorization=Bearer token)

命令

github.copilot.chat.otel.dbSpanExporter.enabled 在 VS Code 中開啟 在 VS Code Insiders 中開啟 true 時,Copilot Chat 會將 OTel Span 持續寫入本地 SQLite 資料庫。這對於離線檢查或在不安裝 OTLP 後端的情況下分享追蹤資料非常有用。

指令 說明
Chat: Export Agent Traces DB (github.copilot.chat.otel.exportAgentTracesDB) 將本地 SQLite 追蹤資料庫匯出為 .db 檔案。僅在 dbSpanExporter.enabled 設定為 true 時可用。

後台與 Claude Agent 的追蹤結構

當啟用 OTel 後,所有 Agent 類型都會自動進行檢測。啟用前景 Agent 追蹤的相同設定,也會啟用 Copilot CLI 與 Claude Agent 的追蹤。

Copilot CLI (後台 Agent)

Copilot CLI SDK 在與聊天擴充功能相同的 VS Code 程序中執行,並產生包含子 Agent、權限、Hook 與工具呼叫的豐富追蹤階層。擴充功能封裝 Span (invoke_agent copilotcli,服務 copilot-chat) 為 SDK 原生 Span (服務 github-copilot) 的父項。兩者都會出現在您收集器的相同追蹤中。

CLI 工作階段也會在 VS Code 的Agent Debug Log 面板中顯示完整的 SDK 階層,內容與追蹤檢視器中顯示的完全相同。即使 OTel 匯出功能被停用,偵錯面板仍可運作,因為 SDK 的內部追蹤功能對於該面板來說永遠是啟動狀態。

當 OTel 匯出功能停用時,偵錯面板會自動擷取完整的提示與回應內容。當啟用 OTel 匯出時, github.copilot.chat.otel.captureContent 在 VS Code 中開啟 在 VS Code Insiders 中開啟 設定會同時控制偵錯面板與 OTLP 匯出的內容擷取。

Copilot CLI (終端機工作階段)

透過New Copilot CLI Session 啟動的終端機 CLI 工作階段會在個別程序中執行。當啟用 OTel 時,擴充功能會將 COPILOT_OTEL_ENABLEDOTEL_EXPORTER_OTLP_ENDPOINT 轉發給終端機程序。終端機追蹤會以 github-copilot 服務下的獨立根追蹤顯示,且不會與擴充功能追蹤連結。

CLI 執行階段僅支援 otlp-http。當設定 otlp-grpc 時,終端機 CLI 仍會使用 HTTP。在同一連接埠上同時提供兩種通訊協定的後端 (例如 Aspire Dashboard) 可透明地運作。

Claude Agent

當啟用 OTel 時,Claude Agent 工作階段會在 copilot-chat 服務下產生遵循 GenAI 語意慣例的擴充功能層級 Span。擴充功能透過攔截 Claude SDK 訊息,並透過本地 HTTP 伺服器代理 LLM 呼叫來建立 Span。

invoke_agent claude 根 Span 會包裝每個使用者請求,其中包含巢狀的 chatexecute_toolexecute_hook Span。當工具為 Agent (Claude 子 Agent 呼叫) 時,子 chatexecute_tool Span 會巢狀在下方,提供完整的子 Agent 可見度。

依 Agent 類型篩選

在您的追蹤檢視器中,依 service.name 篩選以查看來自特定 Agent 執行階段的追蹤

service.name 來源
copilot-chat 前景 Agent、CLI 封裝與 Claude Agent Span (由擴充功能發出)
github-copilot CLI SDK 原生 Span 與 CLI 終端機工作階段
claude-code Claude Code 子程序 SDK 遙測 (當轉發了 CLAUDE_CODE_ENABLE_TELEMETRY 時)

copilot-chat 服務內,依 gen_ai.agent.name 區分 Agent 類型

gen_ai.agent.name Agent 類型
GitHub Copilot Chat 前景 Agent (Agent 模式)
copilotcli CLI 封裝 Span
claude Claude Agent

搭配可觀測性後端使用

Copilot Chat 的 OTel 輸出可與任何支援 OTLP 通訊協定的後端搭配使用。將 github.copilot.chat.otel.otlpEndpoint 在 VS Code 中開啟 在 VS Code Insiders 中開啟 設定或 OTEL_EXPORTER_OTLP_ENDPOINT 環境變數指向後端的 OTLP 接收 URL,並將匯出器類型設定為符合後端的通訊協定 (otlp-httpotlp-grpc)。

Aspire Dashboard

Aspire Dashboard 是本地開發最簡單的選擇。它是一個單一應用程式,具有內建的 OTLP 端點與追蹤檢視器,且不需要雲端帳號。

您可以使用 Aspire CLI 啟動儀表板

aspire dashboard run

或從其 Docker 容器映像檔執行相同的獨立儀表板

docker run --rm -d -p 18888:18888 -p 4318:18890 --name aspire-dashboard \
  mcr.microsoft.com/dotnet/aspire-dashboard:latest

VS Code 設定

{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.captureContent": true
}

開啟 https://:18888 並前往 Traces 以查看您的 Agent 互動 Span。

Screenshot showing agent interaction traces in the Aspire Dashboard with spans for invoke_agent, chat, and execute_tool.

Jaeger

Jaeger 是一個開源的分散式追蹤平台,可直接接收 OTLP。

docker run -d --name jaeger -p 16686:16686 -p 4318:4318 jaegertracing/jaeger:latest

VS Code 設定

{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.otlpEndpoint": "https://:4318"
}

開啟 https://:16686,選擇 copilot-chat 服務,然後選擇 Find Traces

Azure Application Insights

使用具有 Azure Monitor 匯出器OTel Collector,將 Copilot Chat 遙測資料轉發至 Application Insights。將 VS Code github.copilot.chat.otel.otlpEndpoint 在 VS Code 中開啟 在 VS Code Insiders 中開啟 設定指向收集器的 OTLP 端點,並將收集器設定為匯出至您的 Application Insights 連接字串。

若要取得包含現成儀表板的端對端設定,請參閱使用 Grafana 監控 AI Coding Agent。本指南逐步說明如何執行 OTel Collector、將 VS Code 指向它,以及匯入預先建置的 Azure Managed Grafana 儀表板。儀表板會將 Copilot 操作、輸入與輸出 Token、聊天工作階段、工具呼叫,以及 Application Insights 中的個別模型回應時間與 TTFT 可視化。

Langfuse

Langfuse 是一個開源的 LLM 可觀測性平台,具有原生的 OTLP 接收能力,並支援 OTel GenAI 語意慣例。

VS Code 設定

{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.otlpEndpoint": "https://:3000/api/public/otel",
  "github.copilot.chat.otel.captureContent": true
}

使用 OTEL_EXPORTER_OTLP_HEADERS 環境變數設定驗證標頭。詳細資訊請參閱 Langfuse OTel 文件

其他後端

任何相容 OTLP 的後端均可運作,包括 Grafana TempoHoneycombDatadog。請參閱各後端的 OTLP 接收設定說明文件。

其他匯出器範例

預設匯出器為 otlp-http。您可以切換為 otlp-grpcconsolefile 以符合您的後端或偵錯工作流程。

OTLP/gRPC

{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.exporterType": "otlp-grpc",
  "github.copilot.chat.otel.otlpEndpoint": "https://:4317"
}

主控台輸出 (快速偵錯)

{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.exporterType": "console"
}

檔案式輸出 (離線或 CI)

{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.exporterType": "file",
  "github.copilot.chat.otel.outfile": "/tmp/copilot-otel.jsonl"
}

遠端收集器的驗證標頭只能透過 OTEL_EXPORTER_OTLP_HEADERS 環境變數進行設定 (例如 Authorization=Bearer your-token)。

安全性與隱私

OTel 監控預設為關閉,除非您明確啟用,否則不會發出任何資料。您可完全控制收集內容與傳送目的地。

面向 詳細資料
預設關閉 除非明確啟用,否則不會發出任何 OTel 資料。關閉時不會載入 OTel SDK,因此不會造成執行階段額外負擔。
預設無內容 提示、回應與工具參數需要透過 captureContent 主動選擇加入。
預設屬性不包含 PII 工作階段 ID、模型名稱與 Token 數不屬於個人識別資訊。
使用者設定的端點 資料僅會傳送到您指定的目的地。沒有任何「回報總部 (phone-home)」的行為。
© . This site is unofficial and not affiliated with Microsoft.