otel

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package otel provides OpenTelemetry integration for Phoenix.

This package is the Go equivalent of the Python phoenix-otel package, providing easy setup of OpenTelemetry tracing with Phoenix-aware defaults.

Quick Start

import "github.com/agentplexus/go-phoenix/otel"

func main() {
	// Register with Phoenix (sends traces to localhost:6006)
	tp, err := otel.Register()
	if err != nil {
		log.Fatal(err)
	}
	defer tp.Shutdown(context.Background())

	// Use the tracer
	tracer := tp.Tracer("my-service")
	ctx, span := tracer.Start(context.Background(), "my-operation")
	defer span.End()
}

Production Configuration

tp, err := otel.Register(
	otel.WithEndpoint("https://app.phoenix.arize.com"),
	otel.WithAPIKey("your-api-key"),
	otel.WithProjectName("my-project"),
	otel.WithBatch(true),
)

Environment Variables

The following environment variables are supported:

  • PHOENIX_COLLECTOR_ENDPOINT: Phoenix collector endpoint
  • PHOENIX_PROJECT_NAME: Project name for traces
  • PHOENIX_API_KEY: API key for authentication
  • PHOENIX_CLIENT_HEADERS: Additional headers (W3C Baggage format)
  • OTEL_EXPORTER_OTLP_ENDPOINT: Fallback OTLP endpoint

Index

Constants

View Source
const (
	// SpanKindLLM indicates an LLM call span.
	SpanKindLLM = "LLM"
	// SpanKindChain indicates a chain/workflow span.
	SpanKindChain = "CHAIN"
	// SpanKindTool indicates a tool call span.
	SpanKindTool = "TOOL"
	// SpanKindAgent indicates an agent span.
	SpanKindAgent = "AGENT"
	// SpanKindRetriever indicates a retrieval span.
	SpanKindRetriever = "RETRIEVER"
	// SpanKindEmbedding indicates an embedding span.
	SpanKindEmbedding = "EMBEDDING"
	// SpanKindReranker indicates a reranker span.
	SpanKindReranker = "RERANKER"
	// SpanKindGuardrail indicates a guardrail span.
	SpanKindGuardrail = "GUARDRAIL"
)

Span kind attributes for LLM operations.

View Source
const (
	// OpenInferenceSpanKind is the span kind attribute key.
	OpenInferenceSpanKind = "openinference.span.kind"

	// Input/Output attributes
	InputValue  = "input.value"
	InputMime   = "input.mime_type"
	OutputValue = "output.value"
	OutputMime  = "output.mime_type"

	// LLM attributes
	LLMModelName            = "llm.model_name"
	LLMProvider             = "llm.provider"
	LLMInvocationParams     = "llm.invocation_parameters"
	LLMTokenCountPrompt     = "llm.token_count.prompt"     //nolint:gosec // Not a credential
	LLMTokenCountCompletion = "llm.token_count.completion" //nolint:gosec // Not a credential
	LLMTokenCountTotal      = "llm.token_count.total"      //nolint:gosec // Not a credential

	// Message attributes
	LLMInputMessages  = "llm.input_messages"
	LLMOutputMessages = "llm.output_messages"

	// Tool attributes
	ToolName        = "tool.name"
	ToolDescription = "tool.description"
	ToolParameters  = "tool.parameters"

	// Retrieval attributes
	RetrievalDocuments = "retrieval.documents"

	// Embedding attributes
	EmbeddingModelName  = "embedding.model_name"
	EmbeddingEmbeddings = "embedding.embeddings"
	EmbeddingText       = "embedding.text"

	// Metadata attributes
	MetadataKey = "metadata"
	TagsKey     = "tag.tags"

	// Session/Thread attributes
	SessionID = "session.id"
	UserID    = "user.id"
)

OpenInference attribute keys.

View Source
const (
	EnvCollectorEndpoint = "PHOENIX_COLLECTOR_ENDPOINT"
	EnvProjectName       = "PHOENIX_PROJECT_NAME"
	EnvAPIKey            = "PHOENIX_API_KEY"
	EnvSpaceID           = "PHOENIX_SPACE_ID"
	EnvClientHeaders     = "PHOENIX_CLIENT_HEADERS"
	EnvGRPCPort          = "PHOENIX_GRPC_PORT"

	// Standard OTEL fallback
	EnvOTELEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
)

Environment variable names matching Phoenix Python SDK.

View Source
const (
	DefaultEndpoint    = "http://localhost:6006"
	DefaultProjectName = "default"
	DefaultGRPCPort    = 4317
	DefaultHTTPPath    = "/v1/traces"
)

Default values.

Variables

This section is empty.

Functions

func AgentSpanAttributes

func AgentSpanAttributes() []attribute.KeyValue

AgentSpanAttributes returns common attributes for an agent span.

func ChainSpanAttributes

func ChainSpanAttributes() []attribute.KeyValue

ChainSpanAttributes returns common attributes for a chain span.

func LLMSpanAttributes

func LLMSpanAttributes(model, provider string, promptTokens, completionTokens int) []attribute.KeyValue

LLMSpanAttributes returns common attributes for an LLM span.

func RetrieverSpanAttributes

func RetrieverSpanAttributes() []attribute.KeyValue

RetrieverSpanAttributes returns common attributes for a retriever span.

func ToolSpanAttributes

func ToolSpanAttributes(name, description string) []attribute.KeyValue

ToolSpanAttributes returns common attributes for a tool span.

func WithInput

func WithInput(input string) attribute.KeyValue

WithInput sets the input value attribute.

func WithLLMProvider

func WithLLMProvider(provider string) attribute.KeyValue

WithLLMProvider sets the LLM provider name.

func WithMetadata

func WithMetadata(metadata string) attribute.KeyValue

WithMetadata sets a metadata attribute as JSON string.

func WithModelName

func WithModelName(model string) attribute.KeyValue

WithModelName sets the LLM model name.

func WithOutput

func WithOutput(output string) attribute.KeyValue

WithOutput sets the output value attribute.

func WithSessionID

func WithSessionID(id string) attribute.KeyValue

WithSessionID sets the session ID attribute.

func WithSpanKind

func WithSpanKind(kind string) attribute.KeyValue

WithSpanKind sets the OpenInference span kind.

func WithTokenCounts

func WithTokenCounts(prompt, completion, total int) []attribute.KeyValue

WithTokenCounts sets the token count attributes.

func WithToolName

func WithToolName(name string) attribute.KeyValue

WithToolName sets the tool name attribute.

func WithUserID

func WithUserID(id string) attribute.KeyValue

WithUserID sets the user ID attribute.

Types

type Config

type Config struct {
	// Endpoint is the Phoenix collector endpoint.
	// For Phoenix Cloud, use https://app.phoenix.arize.com
	Endpoint string

	// SpaceID is the space identifier for Phoenix Cloud.
	// When set, the endpoint is constructed as {Endpoint}/s/{SpaceID}.
	SpaceID string

	// ProjectName is the project name for traces.
	ProjectName string

	// APIKey is the API key for authentication.
	APIKey string

	// Headers are additional headers to send with requests.
	Headers map[string]string

	// Protocol specifies the transport protocol (http or grpc).
	Protocol Protocol

	// Batch enables batch span processing (recommended for production).
	Batch bool

	// BatchTimeout is the maximum time to wait before exporting a batch.
	BatchTimeout time.Duration

	// BatchSize is the maximum number of spans to batch.
	BatchSize int

	// SetGlobalProvider sets the tracer provider as global.
	SetGlobalProvider bool

	// ServiceName is the service name for the resource.
	ServiceName string

	// ServiceVersion is the service version for the resource.
	ServiceVersion string

	// Insecure disables TLS for gRPC connections.
	Insecure bool
}

Config holds the configuration for Phoenix OTEL integration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with default values and environment overrides.

func (*Config) EffectiveEndpoint

func (c *Config) EffectiveEndpoint() string

EffectiveEndpoint returns the full endpoint with space ID if configured.

type Option

type Option func(*Config)

Option configures the Phoenix OTEL integration.

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey sets the API key for authentication. This is required for Arize cloud.

func WithBatch

func WithBatch(batch bool) Option

WithBatch enables batch span processing. Recommended for production environments.

func WithBatchSize

func WithBatchSize(size int) Option

WithBatchSize sets the maximum number of spans to batch.

func WithBatchTimeout

func WithBatchTimeout(timeout time.Duration) Option

WithBatchTimeout sets the maximum time to wait before exporting a batch.

func WithEndpoint

func WithEndpoint(endpoint string) Option

WithEndpoint sets the Phoenix collector endpoint.

Example endpoints:

func WithGlobalProvider

func WithGlobalProvider(global bool) Option

WithGlobalProvider sets whether to register as the global tracer provider. Defaults to true.

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders sets additional headers to send with requests.

func WithInsecure

func WithInsecure(insecure bool) Option

WithInsecure disables TLS for gRPC connections.

func WithProjectName

func WithProjectName(name string) Option

WithProjectName sets the project name for traces.

func WithProtocol

func WithProtocol(protocol Protocol) Option

WithProtocol sets the transport protocol. Use ProtocolHTTP, ProtocolGRPC, or ProtocolInfer.

func WithServiceName

func WithServiceName(name string) Option

WithServiceName sets the service name for the resource.

func WithServiceVersion

func WithServiceVersion(version string) Option

WithServiceVersion sets the service version for the resource.

func WithSpaceID

func WithSpaceID(spaceID string) Option

WithSpaceID sets the space identifier for Phoenix Cloud. When using Phoenix Cloud (app.phoenix.arize.com), set this to your space ID. The endpoint will be constructed as {Endpoint}/s/{SpaceID}.

type Protocol

type Protocol string

Protocol specifies the OTLP transport protocol.

const (
	// ProtocolHTTP uses HTTP/protobuf transport.
	ProtocolHTTP Protocol = "http/protobuf"

	// ProtocolGRPC uses gRPC transport.
	ProtocolGRPC Protocol = "grpc"

	// ProtocolInfer automatically infers the protocol from the endpoint.
	ProtocolInfer Protocol = "infer"
)

type TracerProvider

type TracerProvider struct {
	*sdktrace.TracerProvider
	// contains filtered or unexported fields
}

TracerProvider wraps the OpenTelemetry TracerProvider with Phoenix-specific functionality.

func Register

func Register(opts ...Option) (*TracerProvider, error)

Register creates and configures an OpenTelemetry TracerProvider for Phoenix.

This is the main entry point for Phoenix OTEL integration. It:

  • Creates an OTLP HTTP exporter configured for Phoenix
  • Sets up a TracerProvider with Phoenix resource attributes
  • Optionally registers as the global tracer provider

Example:

tp, err := otel.Register(
	otel.WithProjectName("my-app"),
	otel.WithBatch(true),
)
if err != nil {
	log.Fatal(err)
}
defer tp.Shutdown(context.Background())

func (*TracerProvider) Config

func (tp *TracerProvider) Config() *Config

Config returns the configuration used by this tracer provider.

func (*TracerProvider) Shutdown

func (tp *TracerProvider) Shutdown(ctx context.Context) error

Shutdown shuts down the tracer provider, flushing any remaining spans.

Jump to

Keyboard shortcuts

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