MCPcopy
hub / github.com/claude-code-best/claude-code / initializeTelemetry

Function initializeTelemetry

src/utils/telemetry/instrumentation.ts:422–706  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

420}
421
422export async function initializeTelemetry() {
423 profileCheckpoint('telemetry_init_start')
424 bootstrapTelemetry()
425
426 // Console exporters call console.dir on a timer (5s logs/traces, 60s
427 // metrics), writing pretty-printed objects to stdout. In stream-json
428 // mode stdout is the SDK message channel; the first line (`{`) breaks
429 // the SDK's line reader. Stripped here (not main.tsx) because init.ts
430 // re-runs applyConfigEnvironmentVariables() inside initializeTelemetry-
431 // AfterTrust for remote-managed-settings users, and bootstrapTelemetry
432 // above copies ANT_OTEL_* for ant users — both would undo an earlier strip.
433 if (getHasFormattedOutput()) {
434 for (const key of [
435 'OTEL_METRICS_EXPORTER',
436 'OTEL_LOGS_EXPORTER',
437 'OTEL_TRACES_EXPORTER',
438 ] as const) {
439 const v = process.env[key]
440 if (v?.includes('console')) {
441 process.env[key] = v
442 .split(',')
443 .map(s => s.trim())
444 .filter(s => s !== 'console')
445 .join(',')
446 }
447 }
448 }
449
450 diag.setLogger(new ClaudeCodeDiagLogger(), DiagLogLevel.ERROR)
451
452 // Initialize Perfetto tracing (independent of OTEL)
453 // Enable via CLAUDE_CODE_PERFETTO_TRACE=1 or CLAUDE_CODE_PERFETTO_TRACE=<path>
454 initializePerfettoTracing()
455
456 const readers = []
457
458 // Add customer exporters (if enabled)
459 const telemetryEnabled = isTelemetryEnabled()
460 logForDebugging(
461 `[3P telemetry] isTelemetryEnabled=${telemetryEnabled} (CLAUDE_CODE_ENABLE_TELEMETRY=${process.env.CLAUDE_CODE_ENABLE_TELEMETRY})`,
462 )
463 if (telemetryEnabled) {
464 readers.push(...(await getOtlpReaders()))
465 }
466
467 // Add BigQuery exporter (for API customers, C4E users, and internal users)
468 if (isBigQueryMetricsEnabled()) {
469 readers.push(getBigQueryExportingReader())
470 }
471
472 // Create base resource with service attributes
473 const platform = getPlatform()
474 const baseAttributes: Record<string, string> = {
475 [ATTR_SERVICE_NAME]: 'claude-code',
476 [ATTR_SERVICE_VERSION]: MACRO.VERSION,
477 }
478
479 // Add WSL-specific attributes if running on WSL

Callers 1

setMeterStateFunction · 0.85

Calls 15

profileCheckpointFunction · 0.85
bootstrapTelemetryFunction · 0.85
getHasFormattedOutputFunction · 0.85
isTelemetryEnabledFunction · 0.85
getOtlpReadersFunction · 0.85
isBigQueryMetricsEnabledFunction · 0.85
getPlatformFunction · 0.85
isBetaTracingEnabledFunction · 0.85
initializeBetaTracingFunction · 0.85
setMeterProviderFunction · 0.85

Tested by

no test coverage detected