Documentation
¶
Overview ¶
Package otel provides OpenTelemetry-based observability for LLM token usage. It implements metrics, traces, and logs collection with a collector/exporter architecture for efficient batch processing.
Index ¶
- Variables
- type Config
- type MeterSetup
- type OTLPConfig
- type SQLiteConfig
- type SinkConfig
- type StoreRefs
- type Tracer
- func (t *Tracer) EndSpan(span trace.Span, err error)
- func (t *Tracer) RecordError(ctx context.Context, err error, attrs ...attribute.KeyValue)
- func (t *Tracer) RecordTokenUsageEvent(ctx context.Context, inputTokens, outputTokens int)
- func (t *Tracer) SetSpanAttributes(ctx context.Context, attrs ...attribute.KeyValue)
- func (t *Tracer) StartRequestSpan(ctx context.Context, provider, model, scenario string) (context.Context, trace.Span)
- func (t *Tracer) StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
Constants ¶
This section is empty.
Variables ¶
var ( // AttrLLMProvider identifies the LLM provider (e.g., "openai", "anthropic", "google") AttrLLMProvider = attribute.Key("llm.provider") // AttrLLMModel identifies the actual model used (e.g., "gpt-4", "claude-3-opus") AttrLLMModel = attribute.Key("llm.model") // AttrLLMRequestModel identifies the model requested by the user AttrLLMRequestModel = attribute.Key("llm.request.model") // AttrLLMTokenType identifies the type of token (input/output) // Note: Uses underscore (llm.token_type) for backward compatibility with internal/obs/otel AttrLLMTokenType = attribute.Key("llm.token_type") // AttrLLMScenario identifies the API scenario (e.g., "openai", "anthropic", "claude_code") AttrLLMScenario = attribute.Key("llm.scenario") // AttrLLMStreaming indicates whether the request was streaming AttrLLMStreaming = attribute.Key("llm.streaming") // AttrLLMResponseStatus indicates the response status (success, error, canceled) AttrLLMResponseStatus = attribute.Key("llm.response.status") // AttrLLMErrorCode contains the error code if status is error AttrLLMErrorCode = attribute.Key("llm.error.code") // AttrLLMRuleUUID identifies the load balancer rule used AttrLLMRuleUUID = attribute.Key("llm.rule.uuid") // AttrLLMProviderUUID identifies the provider UUID AttrLLMProviderUUID = attribute.Key("llm.provider.uuid") // AttrLLMUserTier identifies low-cardinality user class for enterprise traffic. AttrLLMUserTier = attribute.Key("llm.user.tier") // AttrLLMLatencyMs identifies the request latency in milliseconds AttrLLMLatencyMs = attribute.Key("llm.latency.ms") )
Semantic convention attributes following OpenLLMetry and OpenTelemetry standards These attributes are used to annotate metrics with consistent, meaningful labels.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Enabled enables or disables OTel tracking
Enabled bool
// ExportInterval is the time between exports. Default: 10s
ExportInterval time.Duration
// ExportTimeout is the timeout for each export. Default: 30s
ExportTimeout time.Duration
// BufferSize is the max number of metrics to buffer. Default: 10000
BufferSize int
// SQLite exporter configuration
SQLite SQLiteConfig
// OTLP exporter configuration
OTLP OTLPConfig
// Sink exporter configuration
Sink SinkConfig
}
Config holds the configuration for the OTel observability setup.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a config with sensible defaults
type MeterSetup ¶
type MeterSetup struct {
// contains filtered or unexported fields
}
MeterSetup holds the meter provider, tracer provider, and token tracker.
func NewMeterSetup ¶
NewMeterSetup creates a new meter setup with the provided config and stores.
func (*MeterSetup) Shutdown ¶
func (ms *MeterSetup) Shutdown(ctx context.Context) error
Shutdown shuts down the meter and tracer providers.
func (*MeterSetup) Tracker ¶
func (ms *MeterSetup) Tracker() *tracker.TokenTracker
Tracker returns the token tracker.
type OTLPConfig ¶
type OTLPConfig struct {
// Enabled enables OTLP export
Enabled bool
// Endpoint is the OTLP endpoint (gRPC or HTTP)
Endpoint string
// Protocol is the OTLP protocol ("grpc" or "http/protobuf")
Protocol string
// Insecure disables TLS for the connection
Insecure bool
// Headers are optional headers to send with each request
Headers map[string]string
}
OTLPConfig holds OTLP exporter configuration
type SQLiteConfig ¶
type SQLiteConfig struct {
// Enabled enables SQLite export
Enabled bool
}
SQLiteConfig holds SQLite exporter configuration
type SinkConfig ¶
type SinkConfig struct {
// Enabled enables JSONL sink export
Enabled bool
}
SinkConfig holds JSONL sink exporter configuration
type StoreRefs ¶
type StoreRefs struct {
StatsStore *db.StatsStore
UsageStore *db.UsageStore
Sink *obs.Sink
}
StoreRefs holds references to the storage backends for exporters.
type Tracer ¶
type Tracer struct {
// contains filtered or unexported fields
}
Tracer provides distributed tracing capabilities for LLM requests.
func NewTracer ¶
func NewTracer(tp trace.TracerProvider) *Tracer
NewTracer creates a new Tracer with the provided tracer provider.
func (*Tracer) RecordError ¶
RecordError records an error to the current span.
func (*Tracer) RecordTokenUsageEvent ¶
RecordTokenUsageEvent records token usage as a span event.
func (*Tracer) SetSpanAttributes ¶
SetSpanAttributes sets attributes on the current span.