tracing

package
v0.1.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TraceNameKey is used to store the trace name in context
	TraceNameKey contextKey = "trace_name"

	// TraceIDKey is used to store the trace ID in context
	TraceIDKey contextKey = "trace_id"

	// RequestIDKey is used to store the request ID in context
	RequestIDKey contextKey = "request_id"

	// AgentNameKey is used to store the current agent name in context
	AgentNameKey contextKey = "agent_name"
)

Variables

This section is empty.

Functions

func AddToolCallToContext

func AddToolCallToContext(ctx context.Context, toolCall ToolCall)

AddToolCallToContext adds a tool call to the context for tracing

func GetAgentName

func GetAgentName(ctx context.Context) (string, bool)

GetAgentName retrieves the agent name from context

func GetRequestID

func GetRequestID(ctx context.Context) (string, bool)

GetRequestID retrieves the request ID from context

func GetSpanNameOrDefault

func GetSpanNameOrDefault(ctx context.Context, defaultName string) string

GetSpanNameOrDefault gets span name based on agent name or returns default

func GetTraceID

func GetTraceID(ctx context.Context) (string, bool)

GetTraceID retrieves the trace ID from context

func GetTraceName

func GetTraceName(ctx context.Context) (string, bool)

GetTraceName retrieves the trace name from context

func GetTraceNameOrDefault

func GetTraceNameOrDefault(ctx context.Context, defaultName string) string

GetTraceNameOrDefault gets trace name from context or returns a default

func NewLLMMiddleware

func NewLLMMiddleware(llm interfaces.LLM, tracer *LangfuseTracer) interfaces.LLM

@deprecated Use NewTracedLLM - removing in v1.0.0

func NewMemoryOTelMiddleware

func NewMemoryOTelMiddleware(memory interfaces.Memory, tracer *OTelTracer) interfaces.Memory

@deprecated Use NewTracedLLM - removing in v1.0.0

func NewOTELLLMMiddleware

func NewOTELLLMMiddleware(llm interfaces.LLM, tracer *OTELLangfuseTracer) interfaces.LLM

@deprecated Use NewTracedLLM - removing in v1.0.0

func NewOTELLangfuseTracerAsInterface

func NewOTELLangfuseTracerAsInterface(customConfig ...LangfuseConfig) (interfaces.Tracer, error)

Helper function to create and return the adapter in one call This makes it easy to migrate existing code

func NewOTELTracerAdapter

func NewOTELTracerAdapter(otelTracer *OTELLangfuseTracer) interfaces.Tracer

NewOTELTracerAdapter creates a new adapter for OTELLangfuseTracer

func NewTracedLLM

func NewTracedLLM(llm interfaces.LLM, tracer interfaces.Tracer) interfaces.LLM

NewTracedLLM creates a new LLM middleware with unified tracing

func StartRequestTracing

func StartRequestTracing(ctx context.Context, tracer interfaces.Tracer, requestID string) (context.Context, interfaces.Span)

StartRequestTracing starts a trace session for a request and returns the updated context This should be called at the beginning of each request to group all operations under one trace

func WithAgentName

func WithAgentName(ctx context.Context, agentName string) context.Context

WithAgentName adds an agent name to the context for span naming

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID adds a request ID to the context for tracing

func WithRequestTracing

func WithRequestTracing(ctx context.Context, tracer interfaces.Tracer, requestID string, orgID string) (context.Context, interfaces.Span)

WithRequestTracing is a convenience function that combines context setup and trace session creation

func WithToolCallsCollection

func WithToolCallsCollection(ctx context.Context) context.Context

WithToolCallsCollection adds a tool calls collector to the context

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID adds a trace ID to the context

func WithTraceName

func WithTraceName(ctx context.Context, traceName string) context.Context

WithTraceName adds a trace name to the context

Types

type LangfuseConfig

type LangfuseConfig struct {
	// Enabled determines whether Langfuse tracing is enabled
	Enabled bool

	// SecretKey is the Langfuse secret key
	SecretKey string

	// PublicKey is the Langfuse public key
	PublicKey string

	// Host is the Langfuse host (optional)
	Host string

	// Environment is the environment name (e.g., "production", "staging")
	Environment string
}

LangfuseConfig contains configuration for Langfuse

type LangfuseTracer

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

LangfuseTracer implements tracing using Langfuse via OTEL (backward compatibility wrapper) This replaces the old buggy henomis/langfuse-go implementation with our reliable OTEL-based one

func NewLangfuseTracer

func NewLangfuseTracer(customConfig ...LangfuseConfig) (*LangfuseTracer, error)

NewLangfuseTracer creates a new Langfuse tracer (backward compatibility wrapper) This now uses the reliable OTEL-based implementation internally

func (*LangfuseTracer) AsInterfaceTracer

func (t *LangfuseTracer) AsInterfaceTracer() interfaces.Tracer

AsInterfaceTracer returns an interfaces.Tracer compatible adapter This allows the backward-compatible tracer to work with Agents

func (*LangfuseTracer) Flush

func (t *LangfuseTracer) Flush() error

Flush flushes the Langfuse tracer (delegates to OTEL implementation)

func (*LangfuseTracer) Shutdown

func (t *LangfuseTracer) Shutdown() error

Shutdown shuts down the tracer (delegates to OTEL implementation)

func (*LangfuseTracer) TraceEvent

func (t *LangfuseTracer) TraceEvent(ctx context.Context, name string, input interface{}, output interface{}, level string, metadata map[string]interface{}, parentID string) (string, error)

TraceEvent traces an event (delegates to OTEL implementation)

func (*LangfuseTracer) TraceGeneration

func (t *LangfuseTracer) TraceGeneration(ctx context.Context, modelName string, prompt string, response string, startTime time.Time, endTime time.Time, metadata map[string]interface{}) (string, error)

TraceGeneration traces an LLM generation (delegates to OTEL implementation)

func (*LangfuseTracer) TraceSpan

func (t *LangfuseTracer) TraceSpan(ctx context.Context, name string, startTime time.Time, endTime time.Time, metadata map[string]interface{}, parentID string) (string, error)

TraceSpan traces a span of execution (delegates to OTEL implementation)

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message represents a chat message with role and content

type NoOpSpan

type NoOpSpan struct{}

NoOpSpan is a no-operation span implementation for when tracing is disabled

func (*NoOpSpan) AddEvent

func (s *NoOpSpan) AddEvent(name string, attributes map[string]interface{})

func (*NoOpSpan) End

func (s *NoOpSpan) End()

func (*NoOpSpan) RecordError

func (s *NoOpSpan) RecordError(err error)

func (*NoOpSpan) SetAttribute

func (s *NoOpSpan) SetAttribute(key string, value interface{})

type OTELLangfuseSpan

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

OTELLangfuseSpan wraps an OTEL span to implement the interfaces.Span interface

func (*OTELLangfuseSpan) AddEvent

func (s *OTELLangfuseSpan) AddEvent(name string, attributes map[string]interface{})

AddEvent implements interfaces.Span

func (*OTELLangfuseSpan) End

func (s *OTELLangfuseSpan) End()

End implements interfaces.Span

func (*OTELLangfuseSpan) RecordError

func (s *OTELLangfuseSpan) RecordError(err error)

func (*OTELLangfuseSpan) SetAttribute

func (s *OTELLangfuseSpan) SetAttribute(key string, value interface{})

SetAttribute implements interfaces.Span

type OTELLangfuseTracer

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

OTELLangfuseTracer implements tracing using OpenTelemetry sending to Langfuse

func NewOTELLangfuseTracer

func NewOTELLangfuseTracer(customConfig ...LangfuseConfig) (*OTELLangfuseTracer, error)

NewOTELLangfuseTracer creates a new OTEL-based Langfuse tracer

func (*OTELLangfuseTracer) Flush

func (t *OTELLangfuseTracer) Flush() error

Flush flushes the OTEL tracer provider

func (*OTELLangfuseTracer) Shutdown

func (t *OTELLangfuseTracer) Shutdown() error

Shutdown shuts down the tracer provider

func (*OTELLangfuseTracer) StartSpan

StartSpan implements interfaces.Tracer

func (*OTELLangfuseTracer) StartTraceSession

func (t *OTELLangfuseTracer) StartTraceSession(ctx context.Context, contextID string) (context.Context, interfaces.Span)

StartTraceSession starts a root trace session for a request with the given contextID/requestID This creates a root span that will group all subsequent LLM calls and operations

func (*OTELLangfuseTracer) TraceEvent

func (t *OTELLangfuseTracer) TraceEvent(ctx context.Context, name string, input interface{}, output interface{}, level string, metadata map[string]interface{}, parentID string) (string, error)

TraceEvent traces an event

func (*OTELLangfuseTracer) TraceGeneration

func (t *OTELLangfuseTracer) TraceGeneration(ctx context.Context, modelName string, prompt string, response string, startTime time.Time, endTime time.Time, metadata map[string]interface{}) (string, error)

TraceGeneration traces an LLM generation using OTEL spans

func (*OTELLangfuseTracer) TraceSpan

func (t *OTELLangfuseTracer) TraceSpan(ctx context.Context, name string, startTime time.Time, endTime time.Time, metadata map[string]interface{}, parentID string) (string, error)

TraceSpan traces a span of execution

type OTELTracerAdapter

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

OTELTracerAdapter adapts OTELLangfuseTracer to implement interfaces.Tracer This allows the OTEL-based Langfuse tracer to be used with Agents

func (*OTELTracerAdapter) StartSpan

StartSpan implements interfaces.Tracer by delegating to OTELLangfuseTracer

func (*OTELTracerAdapter) StartTraceSession

func (a *OTELTracerAdapter) StartTraceSession(ctx context.Context, contextID string) (context.Context, interfaces.Span)

StartTraceSession implements interfaces.Tracer by delegating to OTELLangfuseTracer

type OTelConfig

type OTelConfig struct {
	// Enabled determines whether OpenTelemetry tracing is enabled
	Enabled bool

	// ServiceName is the name of the service
	ServiceName string

	// CollectorEndpoint is the endpoint of the OpenTelemetry collector
	CollectorEndpoint string

	// Tracer allows passing a pre-built tracer instead of creating one
	Tracer trace.Tracer
}

OTelConfig contains configuration for OpenTelemetry

type OTelSpan

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

OTelSpan wraps an OpenTelemetry span to implement interfaces.Span

func (*OTelSpan) AddEvent

func (s *OTelSpan) AddEvent(name string, attributes map[string]interface{})

AddEvent implements interfaces.Span

func (*OTelSpan) End

func (s *OTelSpan) End()

End implements interfaces.Span

func (*OTelSpan) RecordError

func (s *OTelSpan) RecordError(err error)

func (*OTelSpan) SetAttribute

func (s *OTelSpan) SetAttribute(key string, value interface{})

SetAttribute implements interfaces.Span

type OTelTracer

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

OTelTracer implements tracing using OpenTelemetry

func NewOTelTracer

func NewOTelTracer(config OTelConfig) (*OTelTracer, error)

NewOTelTracer creates a new OpenTelemetry tracer

func NewOTelTracerWrapper

func NewOTelTracerWrapper(tracer trace.Tracer) *OTelTracer

NewOTelTracerWrapper creates a new OpenTelemetry tracer wrapper from existing tracer

func (*OTelTracer) StartSpan

func (t *OTelTracer) StartSpan(ctx context.Context, name string) (context.Context, interfaces.Span)

StartSpan implements interfaces.Tracer

func (*OTelTracer) StartTraceSession

func (t *OTelTracer) StartTraceSession(ctx context.Context, contextID string) (context.Context, interfaces.Span)

StartTraceSession implements interfaces.Tracer

type ToolCall

type ToolCall struct {
	Name       string        `json:"name"`
	Arguments  string        `json:"arguments"`
	ID         string        `json:"id,omitempty"`
	Result     string        `json:"result,omitempty"`
	Error      string        `json:"error,omitempty"`
	Timestamp  string        `json:"timestamp"`
	DurationMs int64         `json:"duration_ms,omitempty"` // Duration in milliseconds for JSON
	StartTime  time.Time     `json:"-"`                     // Not included in JSON, used for span timing
	Duration   time.Duration `json:"-"`                     // Execution duration, not directly serialized
}

ToolCall represents a tool call made by the LLM

func GetToolCallsFromContext

func GetToolCallsFromContext(ctx context.Context) []ToolCall

GetToolCallsFromContext retrieves tool calls from the context

type TracedLLM

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

TracedLLM implements middleware for LLM calls with unified tracing

func (*TracedLLM) Generate

func (m *TracedLLM) Generate(ctx context.Context, prompt string, options ...interfaces.GenerateOption) (string, error)

Generate generates text from a prompt with tracing

func (*TracedLLM) GenerateDetailed

func (m *TracedLLM) GenerateDetailed(ctx context.Context, prompt string, options ...interfaces.GenerateOption) (*interfaces.LLMResponse, error)

GenerateDetailed generates text and returns detailed response information including token usage

func (*TracedLLM) GenerateStream

func (m *TracedLLM) GenerateStream(ctx context.Context, prompt string, options ...interfaces.GenerateOption) (<-chan interfaces.StreamEvent, error)

GenerateStream implements interfaces.StreamingLLM.GenerateStream

func (*TracedLLM) GenerateWithTools

func (m *TracedLLM) GenerateWithTools(ctx context.Context, prompt string, tools []interfaces.Tool, options ...interfaces.GenerateOption) (string, error)

GenerateWithTools generates text from a prompt with tools using unified tracing

func (*TracedLLM) GenerateWithToolsDetailed

func (m *TracedLLM) GenerateWithToolsDetailed(ctx context.Context, prompt string, tools []interfaces.Tool, options ...interfaces.GenerateOption) (*interfaces.LLMResponse, error)

GenerateWithToolsDetailed generates text with tools and returns detailed response information including token usage

func (*TracedLLM) GenerateWithToolsStream

func (m *TracedLLM) GenerateWithToolsStream(ctx context.Context, prompt string, tools []interfaces.Tool, options ...interfaces.GenerateOption) (<-chan interfaces.StreamEvent, error)

GenerateWithToolsStream implements interfaces.StreamingLLM.GenerateWithToolsStream

func (*TracedLLM) GetModel

func (m *TracedLLM) GetModel() string

GetModel returns the model name from the underlying LLM

func (*TracedLLM) Name

func (m *TracedLLM) Name() string

Name implements interfaces.LLM.Name

func (*TracedLLM) SupportsStreaming

func (m *TracedLLM) SupportsStreaming() bool

SupportsStreaming implements interfaces.LLM.SupportsStreaming

type TracedMemory

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

TracedMemory implements middleware for memory operations with unified tracing

func NewTracedMemory

func NewTracedMemory(memory interfaces.Memory, tracer interfaces.Tracer) *TracedMemory

NewTracedMemory creates a new memory middleware with unified tracing

func (*TracedMemory) AddMessage

func (m *TracedMemory) AddMessage(ctx context.Context, message interfaces.Message) error

AddMessage adds a message to memory with tracing

func (*TracedMemory) Clear

func (m *TracedMemory) Clear(ctx context.Context) error

Clear clears memory with tracing

func (*TracedMemory) GetMessages

func (m *TracedMemory) GetMessages(ctx context.Context, options ...interfaces.GetMessagesOption) ([]interfaces.Message, error)

GetMessages gets messages from memory with tracing

Jump to

Keyboard shortcuts

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