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

Function initializeTelemetry

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

Source from the content-addressed store, hash-verified

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

Tested by

no test coverage detected