了解如何為 Claude Code 啟用和配置 OpenTelemetry。
/Library/Application Support/ClaudeCode/managed-settings.json
/etc/claude-code/managed-settings.json
C:\ProgramData\ClaudeCode\managed-settings.json
環境變數 | 描述 | 範例值 |
---|---|---|
CLAUDE_CODE_ENABLE_TELEMETRY | 啟用遙測收集(必需) | 1 |
OTEL_METRICS_EXPORTER | 指標匯出器類型(逗號分隔) | console 、otlp 、prometheus |
OTEL_LOGS_EXPORTER | 日誌/事件匯出器類型(逗號分隔) | console 、otlp |
OTEL_EXPORTER_OTLP_PROTOCOL | OTLP 匯出器協定(所有訊號) | grpc 、http/json 、http/protobuf |
OTEL_EXPORTER_OTLP_ENDPOINT | OTLP 收集器端點(所有訊號) | http://localhost:4317 |
OTEL_EXPORTER_OTLP_METRICS_PROTOCOL | 指標協定(覆蓋一般設定) | grpc 、http/json 、http/protobuf |
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT | OTLP 指標端點(覆蓋一般設定) | http://localhost:4318/v1/metrics |
OTEL_EXPORTER_OTLP_LOGS_PROTOCOL | 日誌協定(覆蓋一般設定) | grpc 、http/json 、http/protobuf |
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT | OTLP 日誌端點(覆蓋一般設定) | http://localhost:4318/v1/logs |
OTEL_EXPORTER_OTLP_HEADERS | OTLP 驗證標頭 | Authorization=Bearer token |
OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY | mTLS 驗證的客戶端金鑰 | 客戶端金鑰檔案路徑 |
OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE | mTLS 驗證的客戶端憑證 | 客戶端憑證檔案路徑 |
OTEL_METRIC_EXPORT_INTERVAL | 匯出間隔(毫秒)(預設:60000) | 5000 、60000 |
OTEL_LOGS_EXPORT_INTERVAL | 日誌匯出間隔(毫秒)(預設:5000) | 1000 、10000 |
OTEL_LOG_USER_PROMPTS | 啟用使用者提示內容記錄(預設:停用) | 1 啟用 |
環境變數 | 描述 | 預設值 | 停用範例 |
---|---|---|---|
OTEL_METRICS_INCLUDE_SESSION_ID | 在指標中包含 session.id 屬性 | true | false |
OTEL_METRICS_INCLUDE_VERSION | 在指標中包含 app.version 屬性 | false | true |
OTEL_METRICS_INCLUDE_ACCOUNT_UUID | 在指標中包含 user.account_uuid 屬性 | true | false |
.claude/settings.json
:
OTEL_RESOURCE_ATTRIBUTES
環境變數新增自訂屬性來區分不同群組:
OTEL_RESOURCE_ATTRIBUTES
環境變數遵循 W3C Baggage 規範,該規範有嚴格的格式要求:user.organizationName=My Company
是無效的key1=value1,key2=value2
"key=value with spaces"
)不受 OpenTelemetry 規範支援,會導致屬性前綴引號。屬性 | 描述 | 控制變數 |
---|---|---|
session.id | 唯一會話識別碼 | OTEL_METRICS_INCLUDE_SESSION_ID (預設:true) |
app.version | 目前 Claude Code 版本 | OTEL_METRICS_INCLUDE_VERSION (預設:false) |
organization.id | 組織 UUID(已驗證時) | 可用時始終包含 |
user.account_uuid | 帳戶 UUID(已驗證時) | OTEL_METRICS_INCLUDE_ACCOUNT_UUID (預設:true) |
terminal.type | 終端類型(例如 iTerm.app 、vscode 、cursor 、tmux ) | 偵測到時始終包含 |
指標名稱 | 描述 | 單位 |
---|---|---|
claude_code.session.count | 啟動的 CLI 會話計數 | count |
claude_code.lines_of_code.count | 修改的程式碼行數計數 | count |
claude_code.pull_request.count | 建立的拉取請求數量 | count |
claude_code.commit.count | 建立的 git 提交數量 | count |
claude_code.cost.usage | Claude Code 會話的成本 | USD |
claude_code.token.usage | 使用的權杖數量 | tokens |
claude_code.code_edit_tool.decision | 程式碼編輯工具權限決策計數 | count |
claude_code.active_time.total | 總活動時間(秒) | s |
type
:("added"
、"removed"
)model
:模型識別碼(例如 “claude-3-5-sonnet-20241022”)type
:("input"
、"output"
、"cacheRead"
、"cacheCreation"
)model
:模型識別碼(例如 “claude-3-5-sonnet-20241022”)tool
:工具名稱("Edit"
、"MultiEdit"
、"Write"
、"NotebookEdit"
)decision
:使用者決策("accept"
、"reject"
)language
:編輯檔案的程式語言(例如 "TypeScript"
、"Python"
、"JavaScript"
、"Markdown"
)。對於無法識別的副檔名返回 "unknown"
。OTEL_LOGS_EXPORTER
時):
claude_code.user_prompt
屬性:
event.name
:"user_prompt"
event.timestamp
:ISO 8601 時間戳prompt_length
:提示長度prompt
:提示內容(預設編輯,使用 OTEL_LOG_USER_PROMPTS=1
啟用)claude_code.tool_result
屬性:
event.name
:"tool_result"
event.timestamp
:ISO 8601 時間戳tool_name
:工具名稱success
:"true"
或 "false"
duration_ms
:執行時間(毫秒)error
:錯誤訊息(如果失敗)decision
:"accept"
或 "reject"
source
:決策來源 - "config"
、"user_permanent"
、"user_temporary"
、"user_abort"
或 "user_reject"
tool_parameters
:包含工具特定參數的 JSON 字串(可用時)
bash_command
、full_command
、timeout
、description
、sandbox
claude_code.api_request
屬性:
event.name
:"api_request"
event.timestamp
:ISO 8601 時間戳model
:使用的模型(例如 “claude-3-5-sonnet-20241022”)cost_usd
:估計成本(美元)duration_ms
:請求持續時間(毫秒)input_tokens
:輸入權杖數量output_tokens
:輸出權杖數量cache_read_tokens
:從快取讀取的權杖數量cache_creation_tokens
:用於快取建立的權杖數量claude_code.api_error
屬性:
event.name
:"api_error"
event.timestamp
:ISO 8601 時間戳model
:使用的模型(例如 “claude-3-5-sonnet-20241022”)error
:錯誤訊息status_code
:HTTP 狀態碼(如果適用)duration_ms
:請求持續時間(毫秒)attempt
:嘗試次數(對於重試請求)claude_code.tool_decision
屬性:
event.name
:"tool_decision"
event.timestamp
:ISO 8601 時間戳tool_name
:工具名稱(例如 “Read”、“Edit”、“MultiEdit”、“Write”、“NotebookEdit” 等)decision
:"accept"
或 "reject"
source
:決策來源 - "config"
、"user_permanent"
、"user_temporary"
、"user_abort"
或 "user_reject"
指標 | 分析機會 |
---|---|
claude_code.token.usage | 按 type (輸入/輸出)、使用者、團隊或模型分解 |
claude_code.session.count | 追蹤隨時間的採用和參與度 |
claude_code.lines_of_code.count | 透過追蹤程式碼新增/移除來衡量生產力 |
claude_code.commit.count 和 claude_code.pull_request.count | 了解對開發工作流程的影響 |
claude_code.cost.usage
指標有助於:
user.account_uuid
、organization.id
、session.id
、model
和 app.version
分段。
service.name
:claude-code
service.version
:目前 Claude Code 版本os.type
:作業系統類型(例如 linux
、darwin
、windows
)os.version
:作業系統版本字串host.arch
:主機架構(例如 amd64
、arm64
)wsl.version
:WSL 版本號(僅在 Windows Subsystem for Linux 上執行時存在)com.anthropic.claude_code
OTEL_LOG_USER_PROMPTS=1