How does the telemetry pipeline collect a tool-call event, batch it, and export through OpenTelemetry, Perfetto tracing, and BigQuery, including span correlation across the agent loop

en
100.0% sentence pass·24/24 cited·43/44 citations valid·115 fn·0 dec·1084 sem
Analytics & TelemetryTool FrameworkAgent & Task OrchestrationApplication Bootstrap & Entrypoints

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

  1. At startup, initializeTelemetry runs a profile checkpoint and calls bootstrapTelemetry to copy ANT_OTEL_* env vars into the standard OTEL_* slots for ant users . The bootstrap also defaults metrics temporality to delta and strips console exporters when stdout is the structured SDK channel .
  2. Telemetry is gated behind CLAUDE_CODE_ENABLE_TELEMETRY, checked via a truthy env parse .
  3. doInitializeTelemetry guards against double-init via a module-level flag and then wires meter state .
  4. 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 by parseExporterTypes .
  5. For eligible users, a BigQuery metrics reader is added on a 5-minute PeriodicExportingMetricReader cadence . Eligibility combines 1P API customers and enterprise/team Claude.ai subscribers .
  6. Trace exporters are selected by protocol (grpc / http/json / http/protobuf) via getOtlpTraceExporters, each wrapped with an OTLP exporter config that threads proxy, mTLS, and dynamic headers . Log exporters follow the same pattern through getOtlpLogExporters .
  7. Beta Perfetto-style tracing is set up when BETA_TRACING_ENDPOINT is defined: an OTLPTraceExporter is wrapped in a BatchSpanProcessor and attached to a BasicTracerProvider, which is registered via setTracerProvider . A log exporter is registered via setLoggerProvider, and meter/event-logger providers via setMeterProvider and setEventLogger .
  8. At tool-use time, runToolUse resolves the tool, logs cancellations/errors via logEvent, and streams into checkPermissionsAndCallTool . That path records tengu_tool_use_error and validation events through the analytics sink, which either dispatches or enqueues for a deferred sink .
  9. For sub-agents, runAgent registers 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 emit tengu_agent_memory_loaded into the same analytics stream .
  10. 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 via registerCleanup .

State touched

Decisions

No [decision:...] tokens appear in the whitelist, so no design-decision citations are available for this flow .