ts/packages/core/src/telemetry/README.md
This document describes how telemetry is implemented in the SDK, including the main components, their responsibilities, and the data flow.
Purpose: The telemetry system collects, batches, and sends usage and error data from the SDK to a remote telemetry server, with minimal impact on application performance.
Key Components:
TelemetryTransport: Main class for instrumentation and telemetry logic.BatchProcessor: Buffers and batches telemetry events before sending.TelemetryService: Handles HTTP requests to the telemetry API.Role: Efficiently batches telemetry events to reduce the number of API calls and avoid performance bottlenecks.
How it works:
batch) to store telemetry events.pushItem:
processBatch:
Initialization:
BatchProcessor with a callback to send metrics via TelemetryService.sendMetric.Instrumentation:
instrument method wraps all async methods of a given instance (e.g., SDK classes).Sending Telemetry:
TelemetryService.sendMetric (POST to /metrics/invocations).TelemetryService.sendErrorLog (POST to /errors).Environment Awareness:
test, ci, or if TELEMETRY_DISABLED is set)./metrics/invocations endpoint./errors endpoint.fetch with JSON payloads.Types: Defined in TelemetryService.types.ts using Zod schemas and TypeScript types.
Payload Fields:
functionName: Name of the function/method invoked.durationMs: Duration of the invocation (optional).timestamp: Epoch time of the event.props: Additional properties (e.g., method arguments).source: Metadata about the SDK/runtime.metadata: Project/provider info.error: Error details (if any).Error Instrumentation:
errorId if not already present.const telemetry = new TelemetryTransport();
telemetry.setup(metadata); // metadata includes SDK version, provider, etc.
telemetry.instrument(someSdkInstance); // Instruments all async methods for telemetry
/apps/telemetry-proxy directory of that repository.