telemetry.go

Overview

The telemetry.go file in the telemetry package provides a simple but crucial integration point for registering custom telemetry span processors into the local OpenTelemetry trace provider within the AI Agent Development Kit (ADK). This setup enables the emission of tracing data for ADK events such as LLM calls and tool executions, allowing external systems or monitoring backends to collect and analyze telemetry data.

Its primary responsibility is to expose an API for adding span processors before any telemetry events occur, ensuring that custom processors are attached to the tracer provider that handles span lifecycle management and export.


Package Functionality

Package Declaration

package telemetry

The package comment clarifies that the purpose of telemetry is to allow setting up custom telemetry processors that receive ADK events as OpenTelemetry spans.

Imports


Functions

RegisterSpanProcessor

func RegisterSpanProcessor(processor sdktrace.SpanProcessor)

Purpose

Registers a custom span processor to the local trace provider instance which handles the creation and export of telemetry spans. Span processors are components that process spans as they are ended, e.g., exporting them to telemetry backends such as Jaeger or Zipkin.

Parameters

Behavior

Usage Example

import (
    "go.opentelemetry.io/otel/sdk/trace"
    "google.golang.org/adk/telemetry"
)

func setupTelemetry() {
    // Create a new span processor (e.g., a batch processor exporting to a backend).
    processor := trace.NewBatchSpanProcessor(exporter)

    // Register the span processor before any tracing starts.
    telemetry.RegisterSpanProcessor(processor)
}

This ensures that all spans emitted by ADK events will be processed by the registered processor.


Important Implementation Details


Interaction with Other Parts of the System

This file acts as the entry point for telemetry customization, linking the ADK's internal event tracing to external observability platforms.


Diagram: Function Relationship and Workflow

flowchart TD
A[External Code] -->|RegisterSpanProcessor(processor)| B[telemetry.RegisterSpanProcessor]
B --> C[internaltelemetry.AddSpanProcessor]
C --> D[Local Trace Provider Config]
D --> E[Span Processor List]
E --> F[Span Processing & Exporting]
subgraph Telemetry Emission Pipeline
G[ADK Events Emitted] --> H[Spans Created]
H --> F
end

References to Related Topics


This file is a fundamental building block enabling external telemetry systems to hook into the ADK's internal event tracing by registering span processors early in the application lifecycle.