telemetry.go

Overview

The telemetry.go file in the telemetry package implements the core telemetry and observability functionality for the AI Agent Development Kit (ADK). Its primary purpose is to set up OpenTelemetry tracing infrastructure and emit detailed trace spans for key agent operations such as LLM calls and tool executions. This enables comprehensive visibility into the runtime behavior of AI agents by recording span events enriched with contextual metadata from sessions, agent invocations, and model/tool interactions.

The file manages a local tracer provider configured with span processors, offers APIs to start and end trace spans, and populates span attributes with serialized request/response data. It integrates tightly with agent, session, tool, and model components to correlate telemetry data with operational events.


Types and Variables

tracerProviderHolder

type tracerProviderHolder struct {
	tp trace.TracerProvider
}

tracerProviderConfig

type tracerProviderConfig struct {
	spanProcessors []sdktrace.SpanProcessor
	mu             *sync.RWMutex
}

Package Variables


Functions

AddSpanProcessor

func AddSpanProcessor(processor sdktrace.SpanProcessor)

RegisterTelemetry

func RegisterTelemetry()

getTracers

func getTracers() []trace.Tracer

StartTrace

func StartTrace(ctx context.Context, traceName string) []trace.Span

TraceMergedToolCalls

func TraceMergedToolCalls(spans []trace.Span, fnResponseEvent *session.Event)

TraceToolCall

func TraceToolCall(spans []trace.Span, tool tool.Tool, fnArgs map[string]any, fnResponseEvent *session.Event)

TraceLLMCall

func TraceLLMCall(spans []trace.Span, agentCtx agent.InvocationContext, llmRequest *model.LLMRequest, event *session.Event)

safeSerialize

func safeSerialize(obj any) string

llmRequestToTrace

func llmRequestToTrace(llmRequest *model.LLMRequest) map[string]any

Implementation Details and Algorithms


Interactions With Other System Components

The telemetry file acts as a centralized observability layer, embedding tracing instrumentation seamlessly into agent runtime operations without requiring manual instrumentation in client code. This enables rich, correlated telemetry data collection for system monitoring and debugging.


Usage Examples

Registering Custom Span Processor

processor := sdktrace.NewSimpleSpanProcessor(myExporter)
telemetry.AddSpanProcessor(processor)
telemetry.RegisterTelemetry()

Starting a Trace for an LLM Request

spans := telemetry.StartTrace(ctx, "gcp.vertex.agent.llm_request")
// Perform LLM call...
telemetry.TraceLLMCall(spans, invocationContext, llmRequest, event)

Tracing a Tool Call

spans := telemetry.StartTrace(ctx, "execute_tool")
// Execute tool...
telemetry.TraceToolCall(spans, toolInstance, toolArgs, responseEvent)

Mermaid Diagram of File Structure and Function Relationships

flowchart TB
AddSpanProcessor --> RegisterTelemetry
RegisterTelemetry --> localTracer
getTracers --> RegisterTelemetry
StartTrace --> getTracers
StartTrace --> trace.Spans
TraceLLMCall --> StartTrace
TraceToolCall --> StartTrace
TraceMergedToolCalls --> StartTrace
safeSerialize --> TraceToolCall
safeSerialize --> TraceMergedToolCalls
safeSerialize --> TraceLLMCall
llmRequestToTrace --> TraceLLMCall

Related Topics

These references offer deeper understanding of the components integrated with telemetry.go.