Documentation
¶
Overview ¶
Package trace provides Langfuse tracing integration for LLM observability.
Package trace provides distributed tracing support. This is a lightweight stub for future OpenTelemetry integration.
Index ¶
- func EndSpanWithError(span *Span, err error)
- type LangfuseClient
- type OTelProviders
- func (p *OTelProviders) FlushOTel(ctx context.Context) error
- func (p *OTelProviders) OTelTracer() interface{}
- func (p *OTelProviders) RecordMetric(name string, value int64, attrs ...interface{})
- func (p *OTelProviders) ShutdownOTel(ctx context.Context) error
- func (p *OTelProviders) StartOTelSpan(ctx context.Context, name string, attrs ...interface{}) (context.Context, interface{})
- type Providers
- type SessionSpan
- type SessionTrace
- func (st *SessionTrace) CurrentSpanID() string
- func (st *SessionTrace) EndSpan(spanID string, status SpanStatus)
- func (st *SessionTrace) FormatTree() string
- func (st *SessionTrace) GetSpan(spanID string) *SessionSpan
- func (st *SessionTrace) StartSpan(name string, attrs map[string]string) string
- func (st *SessionTrace) Summary() TraceSummary
- func (st *SessionTrace) ToJSON() ([]byte, error)
- type Span
- func SpanFromContext(ctx context.Context) (*Span, bool)
- func StartAPICallSpan(ctx context.Context, t *Tracer, provider, model string) (context.Context, *Span)
- func StartAgentLoopSpan(ctx context.Context, t *Tracer, provider, model string, messageCount int) (context.Context, *Span)
- func StartCompactSpan(ctx context.Context, t *Tracer, strategy string, tokensBefore int) (context.Context, *Span)
- func StartSessionSpan(ctx context.Context, t *Tracer, sessionID string) (context.Context, *Span)
- func StartToolSpan(ctx context.Context, t *Tracer, toolName, toolID string) (context.Context, *Span)
- type SpanEvent
- type SpanStatus
- type TelemetryConfig
- type TraceEvent
- type TraceSummary
- type Tracer
- type UsageInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EndSpanWithError ¶
EndSpanWithError finishes a span and marks it as errored if err is non-nil.
Types ¶
type LangfuseClient ¶ added in v0.2.0
type LangfuseClient struct {
// contains filtered or unexported fields
}
LangfuseClient sends traces to Langfuse for LLM observability.
func NewLangfuseClient ¶ added in v0.2.0
func NewLangfuseClient() *LangfuseClient
NewLangfuseClient creates a client from environment variables. Requires LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, and optionally LANGFUSE_HOST.
func (*LangfuseClient) Enabled ¶ added in v0.2.0
func (c *LangfuseClient) Enabled() bool
Enabled reports whether Langfuse credentials are configured.
func (*LangfuseClient) Flush ¶ added in v0.2.0
func (c *LangfuseClient) Flush(ctx context.Context) error
Flush sends all batched events to Langfuse.
func (*LangfuseClient) Trace ¶ added in v0.2.0
func (c *LangfuseClient) Trace(ctx context.Context, ev TraceEvent)
Trace records an LLM call event.
type OTelProviders ¶
type OTelProviders struct{}
OTelProviders is a no-op stub when built without the otel tag.
func InitOTelSDK ¶
func InitOTelSDK(cfg TelemetryConfig) (*OTelProviders, error)
InitOTelSDK returns a no-op provider set.
func (*OTelProviders) OTelTracer ¶
func (p *OTelProviders) OTelTracer() interface{}
func (*OTelProviders) RecordMetric ¶
func (p *OTelProviders) RecordMetric(name string, value int64, attrs ...interface{})
func (*OTelProviders) ShutdownOTel ¶
func (p *OTelProviders) ShutdownOTel(ctx context.Context) error
func (*OTelProviders) StartOTelSpan ¶
type Providers ¶
type Providers struct {
// contains filtered or unexported fields
}
Providers holds the initialized telemetry providers.
func InitTelemetry ¶
func InitTelemetry(cfg TelemetryConfig) (*Providers, error)
InitTelemetry initializes telemetry based on configuration. When OTel SDK is available (future), this will create real OTLP exporters. Currently uses the built-in Tracer as a lightweight fallback.
type SessionSpan ¶
type SessionSpan struct {
ID string `json:"id"`
ParentID string `json:"parent_id,omitempty"`
Name string `json:"name"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
Status SpanStatus `json:"status"`
Attributes map[string]string `json:"attributes,omitempty"`
Children []*SessionSpan `json:"children,omitempty"`
}
SessionSpan represents a single operation within a session trace tree. It forms a tree structure via the Children slice, with ParentID linking back to the parent span.
type SessionTrace ¶
type SessionTrace struct {
SessionID string `json:"session_id"`
StartTime time.Time `json:"start_time"`
Root *SessionSpan `json:"root"`
// contains filtered or unexported fields
}
SessionTrace groups all operations in a session into a parent-child hierarchy, inspired by Helicone's session-level trace trees.
func NewSessionTrace ¶
func NewSessionTrace(sessionID string) *SessionTrace
NewSessionTrace creates a new trace for a session. It initializes a root span that represents the entire session.
func (*SessionTrace) CurrentSpanID ¶
func (st *SessionTrace) CurrentSpanID() string
CurrentSpanID returns the ID of the currently active span.
func (*SessionTrace) EndSpan ¶
func (st *SessionTrace) EndSpan(spanID string, status SpanStatus)
EndSpan completes the span identified by spanID and returns the current active span to its parent. If spanID does not match the current active span, the span is still ended but the active span pointer is only adjusted if it matches.
func (*SessionTrace) FormatTree ¶
func (st *SessionTrace) FormatTree() string
FormatTree renders the trace as an indented tree string using box-drawing characters, similar to the `tree` command output.
func (*SessionTrace) GetSpan ¶
func (st *SessionTrace) GetSpan(spanID string) *SessionSpan
GetSpan retrieves a span by ID. Returns nil if not found.
func (*SessionTrace) StartSpan ¶
func (st *SessionTrace) StartSpan(name string, attrs map[string]string) string
StartSpan begins a new span as a child of the current active span. It returns the new span's ID. The new span becomes the current active span, forming a stack-like push behavior.
func (*SessionTrace) Summary ¶
func (st *SessionTrace) Summary() TraceSummary
Summary returns aggregate statistics for the session trace by walking all spans and parsing their attributes for token counts and cost.
func (*SessionTrace) ToJSON ¶
func (st *SessionTrace) ToJSON() ([]byte, error)
ToJSON exports the full session trace as JSON for external tools such as Jaeger, Grafana, or custom dashboards.
type Span ¶
type Span struct {
Name string `json:"name"`
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
ParentID string `json:"parent_id,omitempty"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Events []SpanEvent `json:"events,omitempty"`
}
Span represents a trace span.
func SpanFromContext ¶
SpanFromContext retrieves a span from context.
func StartAPICallSpan ¶
func StartAPICallSpan(ctx context.Context, t *Tracer, provider, model string) (context.Context, *Span)
StartAPICallSpan creates a span for an LLM API call.
func StartAgentLoopSpan ¶
func StartAgentLoopSpan(ctx context.Context, t *Tracer, provider, model string, messageCount int) (context.Context, *Span)
StartAgentLoopSpan creates a span for the agent loop iteration.
func StartCompactSpan ¶
func StartCompactSpan(ctx context.Context, t *Tracer, strategy string, tokensBefore int) (context.Context, *Span)
StartCompactSpan creates a span for a compaction operation.
func StartSessionSpan ¶
StartSessionSpan creates a span for a full session.
func StartToolSpan ¶
func StartToolSpan(ctx context.Context, t *Tracer, toolName, toolID string) (context.Context, *Span)
StartToolSpan creates a span for a tool execution.
type SpanEvent ¶
type SpanEvent struct {
Name string `json:"name"`
Timestamp time.Time `json:"timestamp"`
Tags map[string]string `json:"tags,omitempty"`
}
SpanEvent represents an event within a span.
type SpanStatus ¶
type SpanStatus int
SpanStatus represents the outcome of a span.
const ( // SpanOK indicates the span completed successfully. SpanOK SpanStatus = iota // SpanError indicates the span completed with an error. SpanError )
func (SpanStatus) MarshalJSON ¶
func (s SpanStatus) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for SpanStatus.
func (SpanStatus) String ¶
func (s SpanStatus) String() string
String returns a human-readable representation of the SpanStatus.
func (*SpanStatus) UnmarshalJSON ¶
func (s *SpanStatus) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler for SpanStatus.
type TelemetryConfig ¶
type TelemetryConfig struct {
Enabled bool
ServiceName string
ServiceVersion string
ExporterProto string
Endpoint string
Headers map[string]string
MetricsInterval time.Duration
TracesInterval time.Duration
LogsInterval time.Duration
ShutdownTimeout time.Duration
}
TelemetryConfig controls OpenTelemetry initialization.
func DefaultTelemetryConfig ¶
func DefaultTelemetryConfig() TelemetryConfig
DefaultTelemetryConfig returns a config populated from environment variables.
type TraceEvent ¶ added in v0.2.0
type TraceEvent struct {
ID string `json:"id"`
Name string `json:"name"`
Input string `json:"input,omitempty"`
Output string `json:"output,omitempty"`
Model string `json:"model,omitempty"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Usage *UsageInfo `json:"usage,omitempty"`
}
TraceEvent represents a single LLM call trace.
type TraceSummary ¶
type TraceSummary struct {
TotalDuration time.Duration `json:"total_duration"`
LLMCalls int `json:"llm_calls"`
ToolCalls int `json:"tool_calls"`
TotalTokensIn int `json:"total_tokens_in"`
TotalTokensOut int `json:"total_tokens_out"`
TotalCostUSD float64 `json:"total_cost_usd"`
Errors int `json:"errors"`
}
TraceSummary holds aggregate statistics for a session trace.