Path: blob/main/extensions/copilot/src/platform/otel/common/agentOTelEnv.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type { OTelConfig } from './otelConfig';67/**8* Derives environment variables for the Copilot CLI SDK from the extension's9* resolved OTel configuration. Only sets variables that are not already present10* in `process.env`, so explicit user env vars serve as per-agent overrides.11*12* Used for both the in-process `LocalSessionManager` (spread into `process.env`)13* and the terminal CLI session (spread into `TerminalOptions.env`).14*/15export function deriveCopilotCliOTelEnv(config: OTelConfig, env: Record<string, string | undefined> = process.env): Record<string, string> {16if (!config.enabled) {17return {};18}1920const result: Record<string, string> = {};2122if (!env['COPILOT_OTEL_ENABLED']) {23result['COPILOT_OTEL_ENABLED'] = 'true';24}25if (!env['OTEL_EXPORTER_OTLP_ENDPOINT'] && config.otlpEndpoint) {26result['OTEL_EXPORTER_OTLP_ENDPOINT'] = config.otlpEndpoint;27}28if (!env['OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT'] && config.captureContent) {29result['OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT'] = 'true';30}31if (!env['COPILOT_OTEL_FILE_EXPORTER_PATH'] && config.fileExporterPath) {32result['COPILOT_OTEL_FILE_EXPORTER_PATH'] = config.fileExporterPath;33}34if (!env['COPILOT_OTEL_EXPORTER_TYPE'] && config.exporterType === 'file') {35result['COPILOT_OTEL_EXPORTER_TYPE'] = 'file';36}37// Note: Copilot CLI runtime only supports otlp-http (not gRPC).38// The OTEL_EXPORTER_OTLP_ENDPOINT is used with the HTTP protocol regardless.39// Standard vars (OTEL_EXPORTER_OTLP_HEADERS, OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME)40// flow via process.env inheritance — no explicit forwarding needed.4142return result;43}4445/**46* Derives environment variables for the Claude Code subprocess from the47* extension's resolved OTel configuration. Claude uses different env var names48* than the Copilot CLI SDK.49*50* Only sets variables not already present in `process.env`.51*/52export function deriveClaudeOTelEnv(config: OTelConfig, env: Record<string, string | undefined> = process.env): Record<string, string> {53if (!config.enabled) {54return {};55}5657const result: Record<string, string> = {};5859if (!env['CLAUDE_CODE_ENABLE_TELEMETRY']) {60result['CLAUDE_CODE_ENABLE_TELEMETRY'] = '1';61}62if (!env['OTEL_METRICS_EXPORTER']) {63result['OTEL_METRICS_EXPORTER'] = 'otlp';64}65if (!env['OTEL_LOGS_EXPORTER']) {66result['OTEL_LOGS_EXPORTER'] = 'otlp';67}68if (!env['OTEL_EXPORTER_OTLP_ENDPOINT'] && config.otlpEndpoint) {69result['OTEL_EXPORTER_OTLP_ENDPOINT'] = config.otlpEndpoint;70}71if (!env['OTEL_EXPORTER_OTLP_PROTOCOL']) {72result['OTEL_EXPORTER_OTLP_PROTOCOL'] = config.otlpProtocol === 'grpc' ? 'grpc' : 'http/json';73}74if (config.captureContent) {75if (!env['OTEL_LOG_USER_PROMPTS']) {76result['OTEL_LOG_USER_PROMPTS'] = '1';77}78if (!env['OTEL_LOG_TOOL_DETAILS']) {79result['OTEL_LOG_TOOL_DETAILS'] = '1';80}81}82// Claude SDK has no file exporter — skip fileExporterPath.83// Standard vars (OTEL_EXPORTER_OTLP_HEADERS, OTEL_RESOURCE_ATTRIBUTES) flow via inheritance.8485return result;86}878889