Help us shape the future of Go! Take the 2026 Go Developer Survey

otelprovider

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package otelprovider instruments agents and embedding generators with OpenTelemetry tracing and metrics following the GenAI semantic conventions. Use NewMiddleware for agent, function-tool, and supported model-adapter calls, and WrapEmbeddingGenerator for embedding generation. Model calls are instrumented by default; MiddlewareConfig.DisableModelTracing omits their spans and metrics. Both use the global OpenTelemetry providers and leave their lifetime to the caller.

Model calls produce client spans with operation "chat", model identifiers, token usage, and errors. They are children of the agent span; tool execution remains outside those model spans. SDK retries and any remote server-side orchestration stay within the corresponding adapter-call span. Prompt content, tool arguments, and tool results are not recorded by model tracing. Adapters supply neutral model metadata; instrumentation stays in this package. Nested agents use their own middleware settings rather than inheriting model tracing from a parent. Native SDK instrumentation may produce additional spans under its own scope and is not disabled by this middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(cfg MiddlewareConfig) agent.Middleware

NewMiddleware creates middleware that adds OpenTelemetry tracing and metrics to agent runs, function tools, and supported model-adapter calls. Set MiddlewareConfig.DisableModelTracing to omit model-call instrumentation.

func WrapEmbeddingGenerator added in v0.2.0

func WrapEmbeddingGenerator[TInput, TEmbedding any](inner embedding.Generator[TInput, TEmbedding], cfg EmbeddingGeneratorConfig) embedding.Generator[TInput, TEmbedding]

WrapEmbeddingGenerator wraps inner with OpenTelemetry GenAI v1.41 client spans and duration and input-token histograms. The conventions are experimental. It panics if inner is nil. Requests, results, and errors are forwarded unchanged.

Request options override the configured model and dimension defaults. Response-model telemetry uses only the first result, when it is an embedding.Embedding value or pointer using embedding.Vector or a built-in JSON vector type. Other output types still receive usage and duration telemetry but do not supply response-model tags. A non-nil result Usage reports InputTokenCount, including an explicit zero.

The returned generator implements io.Closer if and only if inner does. Close calls are forwarded unchanged, including errors and repeated calls. Generation does not close inner, and the wrapper never shuts down the global telemetry providers. Callers must not close a generator while it is in use.

Types

type EmbeddingGeneratorConfig added in v0.2.0

type EmbeddingGeneratorConfig struct {
	// SourceName is the tracing and metrics instrumentation scope. Empty uses
	// "github.com/microsoft/agent-framework-go".
	SourceName string
	// ProviderName identifies the embedding service. Empty omits the attribute.
	ProviderName string
	// ServerAddress and ServerPort identify the service endpoint. When the
	// address is empty, both attributes are omitted. Neither affects routing.
	ServerAddress string
	ServerPort    int
	// DefaultModelID is used when a request does not specify a model.
	// Empty means unknown. It does not configure the underlying generator.
	DefaultModelID string
	// DefaultModelDimensions is used when a request does not specify dimensions.
	// Nil means unknown; explicit zero is preserved.
	DefaultModelDimensions *int
	// EnableSensitiveData permits additional request and response properties
	// on spans. Supported attribute types retain their values; other types are
	// formatted as strings, and nil entries are omitted. Raw inputs and vectors
	// are never captured. Nil uses
	// OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, enabled only by
	// "true" (case-insensitive), read when the wrapper is constructed.
	EnableSensitiveData *bool
	// Logger receives operation failures as warning logs. Nil disables logs.
	Logger *slog.Logger
}

EmbeddingGeneratorConfig configures embedding generation telemetry.

type MiddlewareConfig

type MiddlewareConfig struct {
	SourceName string

	// DisableModelTracing omits client spans and usage/duration metrics for
	// model-adapter calls, retaining agent-run and function-tool instrumentation.
	// By default, OpenAI, Foundry Responses, Anthropic, and Gemini adapter calls
	// are instrumented. Raw messages and tool arguments are not captured.
	DisableModelTracing bool
}

MiddlewareConfig holds configuration for the middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL