()
| 328 | } |
| 329 | |
| 330 | async function doInitializeTelemetry(): Promise<void> { |
| 331 | if (telemetryInitialized) { |
| 332 | // Already initialized, nothing to do |
| 333 | return |
| 334 | } |
| 335 | |
| 336 | // Skip entire OTel initialization when telemetry is not enabled. |
| 337 | // Prevents PerformanceMeasure accumulation in long-running sessions. |
| 338 | if (!isEnvTruthy(process.env.CLAUDE_CODE_ENABLE_TELEMETRY)) { |
| 339 | telemetryInitialized = true |
| 340 | logForDebugging( |
| 341 | '[3P telemetry] Skipped — CLAUDE_CODE_ENABLE_TELEMETRY not set', |
| 342 | ) |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | // Set flag before init to prevent double initialization |
| 347 | telemetryInitialized = true |
| 348 | try { |
| 349 | await setMeterState() |
| 350 | } catch (error) { |
| 351 | // Reset flag on failure so subsequent calls can retry |
| 352 | telemetryInitialized = false |
| 353 | throw error |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | async function setMeterState(): Promise<void> { |
| 358 | // Lazy-load instrumentation to defer ~400KB of OpenTelemetry + protobuf |
no test coverage detected