observability

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Default service name for traces
	DefaultServiceName = "aixgo"

	// Langfuse default endpoint
	LangfuseEndpoint = "https://cloud.langfuse.com/api/public/otel"
)

Variables

This section is empty.

Functions

func Init

func Init(config Config) error

Init initializes the observability system with the given configuration

func InitFromEnv

func InitFromEnv() error

InitFromEnv initializes observability from environment variables Supports standard OpenTelemetry environment variables: - OTEL_SERVICE_NAME: Service name (default: "aixgo") - OTEL_TRACES_EXPORTER: Exporter type - "otlp", "stdout", or "none" (default: "otlp") - OTEL_EXPORTER_OTLP_ENDPOINT: OTLP endpoint (default: Langfuse endpoint) - OTEL_EXPORTER_OTLP_HEADERS: Headers in format "key1=value1,key2=value2" - LANGFUSE_PUBLIC_KEY: Langfuse public key (for Basic Auth) - LANGFUSE_SECRET_KEY: Langfuse secret key (for Basic Auth)

func InitLangfuse

func InitLangfuse() error

InitLangfuse initializes the Langfuse client from environment variables

func Shutdown

func Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the observability system

func StartSpanWithOtel

func StartSpanWithOtel(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

StartSpanWithOtel creates a new span with the given name and OpenTelemetry options. This is the preferred method for context-aware tracing. Returns a context with the span and the raw OpenTelemetry span.

Types

type Config

type Config struct {
	// ServiceName is the name of the service (defaults to "aixgo")
	ServiceName string

	// Enabled controls whether tracing is enabled (defaults to true)
	Enabled bool

	// ExporterType specifies the exporter: "otlp", "stdout", or "none"
	ExporterType string

	// OTLPEndpoint is the OTLP endpoint URL (defaults to Langfuse)
	OTLPEndpoint string

	// OTLPHeaders are additional headers for OTLP requests (e.g., authorization)
	OTLPHeaders map[string]string
}

Config holds observability configuration

type Generation

type Generation struct {
	ID              string                 `json:"id,omitempty"`
	Name            string                 `json:"name,omitempty"`
	StartTime       time.Time              `json:"startTime"`
	EndTime         time.Time              `json:"endTime,omitempty"`
	Model           string                 `json:"model"`
	ModelParameters map[string]interface{} `json:"modelParameters,omitempty"`
	Input           interface{}            `json:"input,omitempty"`
	Output          interface{}            `json:"output,omitempty"`
	Usage           *LangfuseUsage         `json:"usage,omitempty"`
	Metadata        map[string]interface{} `json:"metadata,omitempty"`
	Level           string                 `json:"level,omitempty"` // "DEBUG", "DEFAULT", "WARNING", "ERROR"
	StatusMessage   string                 `json:"statusMessage,omitempty"`
	Version         string                 `json:"version,omitempty"`
	TraceID         string                 `json:"traceId,omitempty"`
	ParentID        string                 `json:"parentObservationId,omitempty"`
}

Generation represents an LLM generation event in Langfuse

func NewGeneration

func NewGeneration(name, model string, startTime time.Time) *Generation

NewGeneration creates a new Generation event

func (*Generation) Finish

func (g *Generation) Finish() *Generation

Finish marks the generation as complete

func (*Generation) WithInput

func (g *Generation) WithInput(input interface{}) *Generation

WithInput adds input to a generation

func (*Generation) WithMetadata

func (g *Generation) WithMetadata(metadata map[string]interface{}) *Generation

WithMetadata adds metadata to a generation

func (*Generation) WithOutput

func (g *Generation) WithOutput(output interface{}) *Generation

WithOutput adds output to a generation

func (*Generation) WithTraceID

func (g *Generation) WithTraceID(traceID string) *Generation

WithTraceID associates the generation with a trace

func (*Generation) WithUsage

func (g *Generation) WithUsage(promptTokens, completionTokens int, inputCost, outputCost float64) *Generation

WithUsage adds usage information to a generation

type LangfuseClient

type LangfuseClient struct {
	// contains filtered or unexported fields
}

LangfuseClient provides direct integration with Langfuse SDK features beyond what's available through OpenTelemetry. This includes LLM-specific features like Generations, Scores, and Feedback.

var (
	// DefaultLangfuseClient is the global Langfuse client instance
	DefaultLangfuseClient *LangfuseClient
)

func NewLangfuseClient

func NewLangfuseClient(config LangfuseConfig) (*LangfuseClient, error)

NewLangfuseClient creates a new Langfuse client

func (*LangfuseClient) Close

func (c *LangfuseClient) Close() error

Close closes the Langfuse client

func (*LangfuseClient) Flush

func (c *LangfuseClient) Flush(ctx context.Context) error

Flush ensures all pending events are sent (no-op for HTTP client)

func (*LangfuseClient) TrackGeneration

func (c *LangfuseClient) TrackGeneration(ctx context.Context, gen *Generation) error

TrackGeneration tracks an LLM generation in Langfuse

func (*LangfuseClient) TrackScore

func (c *LangfuseClient) TrackScore(ctx context.Context, score *Score) error

TrackScore tracks a score/evaluation in Langfuse

type LangfuseConfig

type LangfuseConfig struct {
	// BaseURL is the Langfuse API endpoint (defaults to cloud.langfuse.com)
	BaseURL string

	// PublicKey is the Langfuse public key
	PublicKey string

	// SecretKey is the Langfuse secret key
	SecretKey string

	// Enabled controls whether Langfuse integration is active
	Enabled bool
}

LangfuseConfig contains configuration for Langfuse integration

type LangfuseUsage

type LangfuseUsage struct {
	PromptTokens     int     `json:"promptTokens,omitempty"`
	CompletionTokens int     `json:"completionTokens,omitempty"`
	TotalTokens      int     `json:"totalTokens,omitempty"`
	Unit             string  `json:"unit,omitempty"` // "TOKENS", "CHARACTERS", "MILLISECONDS", etc.
	InputCost        float64 `json:"inputCost,omitempty"`
	OutputCost       float64 `json:"outputCost,omitempty"`
	TotalCost        float64 `json:"totalCost,omitempty"`
}

LangfuseUsage represents token usage and cost

type Score

type Score struct {
	ID            string                 `json:"id,omitempty"`
	TraceID       string                 `json:"traceId"`
	Name          string                 `json:"name"`
	Value         float64                `json:"value"`
	Comment       string                 `json:"comment,omitempty"`
	ObservationID string                 `json:"observationId,omitempty"`
	Metadata      map[string]interface{} `json:"metadata,omitempty"`
}

Score represents a score/evaluation for a trace or generation

func NewScore

func NewScore(traceID, name string, value float64) *Score

NewScore creates a new Score event

func (*Score) WithComment

func (s *Score) WithComment(comment string) *Score

WithComment adds a comment to a score

func (*Score) WithObservationID

func (s *Score) WithObservationID(observationID string) *Score

WithObservationID associates the score with a specific observation

type Span

type Span struct {
	// contains filtered or unexported fields
}

Span represents an observability span

func StartSpan

func StartSpan(name string, data map[string]any) *Span

StartSpan creates a new span with the given name and attributes (legacy version with map) Deprecated: Use StartSpanWithContext for context-aware tracing

func StartSpanWithContext

func StartSpanWithContext(ctx context.Context, name string, data map[string]any) (context.Context, *Span)

StartSpanWithContext creates a new span from a parent context

func (*Span) Context

func (s *Span) Context() context.Context

Context returns the span's context

func (*Span) Data

func (s *Span) Data() map[string]any

Data returns the span data

func (*Span) End

func (s *Span) End()

End finishes the span

func (*Span) IsEnded

func (s *Span) IsEnded() bool

IsEnded returns whether the span has been ended

func (*Span) Name

func (s *Span) Name() string

Name returns the span name

func (*Span) SetAttribute

func (s *Span) SetAttribute(key string, value any)

SetAttribute adds an attribute to the span

func (*Span) SetError

func (s *Span) SetError(err error)

SetError marks the span as having an error

Jump to

Keyboard shortcuts

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