Telemetry Pipeline: Tool-Call Event → Batched Export
Overview
Claude Code initializes an OpenTelemetry SDK at startup and routes tool-call events through analytics and OTLP exporters, with optional BigQuery metrics and Perfetto trace correlation across agent loops .
Steps
- At startup,
initializeTelemetryruns a profile checkpoint and callsbootstrapTelemetryto copyANT_OTEL_*env vars into the standardOTEL_*slots for ant users . The bootstrap also defaults metrics temporality todeltaand stripsconsoleexporters when stdout is the structured SDK channel . - Telemetry is gated behind
CLAUDE_CODE_ENABLE_TELEMETRY, checked via a truthy env parse . doInitializeTelemetryguards against double-init via a module-level flag and then wires meter state .- Metric readers are assembled by
getOtlpReaders, which parses the exporter list and reads the export interval from env . Exporter type strings are split and filtered byparseExporterTypes. - For eligible users, a BigQuery metrics reader is added on a 5-minute
PeriodicExportingMetricReadercadence . Eligibility combines 1P API customers and enterprise/team Claude.ai subscribers . - Trace exporters are selected by protocol (
grpc/http/json/http/protobuf) viagetOtlpTraceExporters, each wrapped with an OTLP exporter config that threads proxy, mTLS, and dynamic headers . Log exporters follow the same pattern throughgetOtlpLogExporters. - Beta Perfetto-style tracing is set up when
BETA_TRACING_ENDPOINTis defined: anOTLPTraceExporteris wrapped in aBatchSpanProcessorand attached to aBasicTracerProvider, which is registered viasetTracerProvider. A log exporter is registered viasetLoggerProvider, and meter/event-logger providers viasetMeterProviderandsetEventLogger. - At tool-use time,
runToolUseresolves the tool, logs cancellations/errors vialogEvent, and streams intocheckPermissionsAndCallTool. That path recordstengu_tool_use_errorand validation events through the analytics sink, which either dispatches or enqueues for a deferred sink . - For sub-agents,
runAgentregisters the child in the Perfetto trace hierarchy (parent = parent agentId or session) so tool spans emitted during the agent loop correlate back to the spawning agent . Agent definitions and memory loads also emittengu_agent_memory_loadedinto the same analytics stream . - On process shutdown, providers flush within bounds configured by
CLAUDE_CODE_OTEL_FLUSH_TIMEOUT_MS/CLAUDE_CODE_OTEL_SHUTDOWN_TIMEOUT_MS, with cleanup handlers registered viaregisterCleanup.
State touched
Decisions
No [decision:...] tokens appear in the whitelist, so no design-decision citations are available for this flow .