使用 OpenTelemetry 監控代理程式使用情況

本文說明如何在 VS Code 中,為 Copilot Chat 代理程式互動啟用及設定 OpenTelemetry 監控。

Copilot Chat 可以透過 OpenTelemetry (OTel) 匯出追蹤、指標和事件,讓您能夠深入了解代理程式互動、LLM 呼叫、工具執行和權杖使用情況。所有訊號名稱和屬性都遵循 OTel GenAI 語意慣例,因此資料可與任何 OTel 相容的後端搭配使用。

收集什麼內容

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

追蹤

每個代理程式互動都會產生一個階層式 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)

三種 Span 類型構成追蹤

Span 說明 主要屬性
invoke_agent 包裹整個代理程式協調,包括所有 LLM 呼叫和工具執行 代理程式名稱、對話 ID、回合計數、總權杖使用量
chat 單次 LLM API 呼叫 模型、權杖計數、回應時間、結束原因
execute_tool 單次工具叫用 工具名稱、工具類型、持續時間、成功狀態

當代理程式叫用子代理程式時 (例如,透過 runSubagent 工具),追蹤內容會自動傳播。子代理程式的 invoke_agent span 會顯示為父代理程式的 execute_tool span 的子項,產生跨非同步邊界的連線追蹤樹。

指標

指標 類型 說明
gen_ai.client.operation.duration 長條圖 LLM API 呼叫持續時間 (秒)
gen_ai.client.token.usage 長條圖 權杖計數 (輸入和輸出)
copilot_chat.tool.call.count 計數器 依名稱和成功狀態區分的工具叫用
copilot_chat.tool.call.duration 長條圖 工具執行延遲 (毫秒)
copilot_chat.agent.invocation.duration 長條圖 代理程式端對端持續時間 (秒)
copilot_chat.agent.turn.count 長條圖 每次代理程式叫用的 LLM 往返行程
copilot_chat.session.count 計數器 已啟動的聊天工作階段
copilot_chat.time_to_first_token 長條圖 第一個 SSE 權杖的時間 (秒)

指標包含用於篩選的屬性,例如 gen_ai.request.modelgen_ai.provider.namegen_ai.tool.nameerror.type

活動

事件 說明
gen_ai.client.inference.operation.details 完整的 LLM 呼叫中繼資料,包含模型、權杖和結束原因
copilot_chat.session.start 當新的聊天工作階段開始時發出
copilot_chat.tool.call 每次工具叫用,包含時間和錯誤詳細資料
copilot_chat.agent.turn 每次回合的 LLM 往返行程,包含權杖計數

資源屬性

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

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

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

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

內容擷取

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

若要擷取完整內容,請啟用 github.copilot.chat.otel.captureContent Open in VS Code Open in VS Code Insiders 設定,或設定 COPILOT_OTEL_CAPTURE_CONTENT=true。這會將完整的提示訊息、回應訊息、系統提示、工具結構描述、工具引數和工具結果填入 span 屬性。

注意

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

啟用 OTel 監控

符合下列任何條件時,OTel 會啟用

  • github.copilot.chat.otel.enabled Open in VS Code Open in 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-httpotlp-grpcconsolefile
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.outfile 在 VS Code 中開啟 在 VS Code Insiders 中開啟 string "" JSON-lines 輸出的檔案路徑

環境變數

環境變數始終優先於 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 會改變行為。
OTEL_SERVICE_NAME copilot-chat 資源屬性中的服務名稱
OTEL_RESOURCE_ATTRIBUTES 額外的資源屬性 (key1=val1,key2=val2)
COPILOT_OTEL_CAPTURE_CONTENT false 擷取完整的提示和回應內容
OTEL_EXPORTER_OTLP_HEADERS 驗證標頭 (例如,Authorization=Bearer token)

搭配可觀測性後端使用

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

Aspire Dashboard

Aspire Dashboard 是本機開發最簡單的選項。它是一個單一的 Docker 容器,內建 OTLP 端點和追蹤檢視器,而且不需要雲端帳戶。

docker run --rm -d \
  -p 18888:18888 \
  -p 4317:18889 \
  --name aspire-dashboard \
  mcr.microsoft.com/dotnet/aspire-dashboard:latest
{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.exporterType": "otlp-grpc",
  "github.copilot.chat.otel.otlpEndpoint": "https://:4317"
}

開啟 https://:18888 並前往追蹤以檢視您的代理程式互動 Span。

Jaeger

Jaeger 是一個開放原始碼分散式追蹤平台,可直接接受 OTLP。

docker run -d --name jaeger -p 16686:16686 -p 4318:4318 jaegertracing/jaeger:latest
{
  "github.copilot.chat.otel.enabled": true,
  "github.copilot.chat.otel.otlpEndpoint": "https://:4318"
}

開啟 https://:16686,選取服務 copilot-chat,然後選取尋找追蹤

Azure Application Insights

使用 OTel 收集器搭配 Azure Monitor 匯出器,將 Copilot Chat 遙測資料轉發到 Application Insights。將 VS Code github.copilot.chat.otel.otlpEndpoint Open in VS Code Open in VS Code Insiders 設定指向收集器的 OTLP 端點,並將收集器設定為匯出到您的 Application Insights 連線字串。

Langfuse

Langfuse 是一個開放原始碼 LLM 可觀測性平台,具有原生 OTLP 擷取功能,並支援 OTel GenAI 語意慣例。

{
  "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 擷取設定。

安全性與隱私

OTel 監控預設為關閉,除非您明確啟用,否則不會發出任何資料。您可以控制收集哪些內容以及資料傳送至何處。

方面 詳細資訊
預設為關閉 除非您明確啟用 OTel 資料,否則不會發出任何 OTel 資料。停用時,OTel SDK 不會載入,因此不會產生執行階段開銷。
預設不含內容 提示、回應和工具引數需要透過 captureContent 選擇啟用。
預設屬性中不含 PII 工作階段 ID、模型名稱和權杖計數無法識別個人身分。
使用者設定的端點 資料只會傳送至您指定的位置。沒有回報行為。
© . This site is unofficial and not affiliated with Microsoft.