tracing

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 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 added in v0.0.22

func AddToolCallToContext(ctx context.Context, toolCall ToolCall)

AddToolCallToContext adds a tool call to the context for tracing

func GetAgentName added in v0.0.22

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

GetAgentName retrieves the agent name from context

func GetRequestID added in v0.0.22

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

GetRequestID retrieves the request ID from context

func GetSpanNameOrDefault added in v0.0.22

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

GetSpanNameOrDefault gets span name based on agent name or returns default

func GetTraceID added in v0.0.22

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

GetTraceID retrieves the trace ID from context

func GetTraceName added in v0.0.22

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

GetTraceName retrieves the trace name from context

func GetTraceNameOrDefault added in v0.0.22

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 added in v0.0.22

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

@deprecated Use NewTracedLLM - removing in v1.0.0

func NewOTELLangfuseTracerAsInterface added in v0.0.22

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 added in v0.0.22

func NewOTELTracerAdapter(otelTracer *OTELLangfuseTracer) interfaces.Tracer

NewOTELTracerAdapter creates a new adapter for OTELLangfuseTracer

func NewTracedLLM added in v0.1.3

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

NewTracedLLM creates a new LLM middleware with unified tracing

func StartRequestTracing added in v0.0.22

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 added in v0.0.22

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

WithAgentName adds an agent name to the context for span naming

func WithRequestID added in v0.0.22

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

WithRequestID adds a request ID to the context for tracing

func WithRequestTracing added in v0.0.22

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 added in v0.0.22

func WithToolCallsCollection(ctx context.Context) context.Context

WithToolCallsCollection adds a tool calls collector to the context

func WithTraceID added in v0.0.22

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

WithTraceID adds a trace ID to the context

func WithTraceName added in v0.0.22

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 added in v0.0.22

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 added in v0.0.22

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 added in v0.0.22

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

Message represents a chat message with role and content

type NoOpSpan added in v0.0.22

type NoOpSpan struct{}

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

func (*NoOpSpan) AddEvent added in v0.0.22

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

func (*NoOpSpan) End added in v0.0.22

func (s *NoOpSpan) End()

func (*NoOpSpan) RecordError added in v0.1.3

func (s *NoOpSpan) RecordError(err error)

func (*NoOpSpan) SetAttribute added in v0.0.22

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

type OTELLangfuseSpan added in v0.0.22

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

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

func (*OTELLangfuseSpan) AddEvent added in v0.0.22

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

AddEvent implements interfaces.Span

func (*OTELLangfuseSpan) End added in v0.0.22

func (s *OTELLangfuseSpan) End()

End implements interfaces.Span

func (*OTELLangfuseSpan) RecordError added in v0.1.3

func (s *OTELLangfuseSpan) RecordError(err error)

func (*OTELLangfuseSpan) SetAttribute added in v0.0.22

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

SetAttribute implements interfaces.Span

type OTELLangfuseTracer added in v0.0.22

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

OTELLangfuseTracer implements tracing using OpenTelemetry sending to Langfuse

func NewOTELLangfuseTracer added in v0.0.22

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

NewOTELLangfuseTracer creates a new OTEL-based Langfuse tracer

func (*OTELLangfuseTracer) Flush added in v0.0.22

func (t *OTELLangfuseTracer) Flush() error

Flush flushes the OTEL tracer provider

func (*OTELLangfuseTracer) Shutdown added in v0.0.22

func (t *OTELLangfuseTracer) Shutdown() error

Shutdown shuts down the tracer provider

func (*OTELLangfuseTracer) StartSpan added in v0.0.22

StartSpan implements interfaces.Tracer

func (*OTELLangfuseTracer) StartTraceSession added in v0.0.22

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 added in v0.0.22

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 added in v0.0.22

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 added in v0.0.22

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 added in v0.0.22

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 added in v0.0.22

StartSpan implements interfaces.Tracer by delegating to OTELLangfuseTracer

func (*OTELTracerAdapter) StartTraceSession added in v0.0.22

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 added in v0.1.3

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

OTelSpan wraps an OpenTelemetry span to implement interfaces.Span

func (*OTelSpan) AddEvent added in v0.1.3

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

AddEvent implements interfaces.Span

func (*OTelSpan) End added in v0.1.3

func (s *OTelSpan) End()

End implements interfaces.Span

func (*OTelSpan) RecordError added in v0.1.3

func (s *OTelSpan) RecordError(err error)

func (*OTelSpan) SetAttribute added in v0.1.3

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 added in v0.1.4

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 added in v0.1.3

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

StartTraceSession implements interfaces.Tracer

type ToolCall added in v0.0.22

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 added in v0.0.22

func GetToolCallsFromContext(ctx context.Context) []ToolCall

GetToolCallsFromContext retrieves tool calls from the context

type TracedLLM added in v0.1.3

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

TracedLLM implements middleware for LLM calls with unified tracing

func (*TracedLLM) Generate added in v0.1.3

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 added in v0.1.13

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 added in v0.1.3

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

GenerateStream implements interfaces.StreamingLLM.GenerateStream

func (*TracedLLM) GenerateWithTools added in v0.1.3

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 added in v0.1.13

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 added in v0.1.3

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 added in v0.1.9

func (m *TracedLLM) GetModel() string

GetModel returns the model name from the underlying LLM

func (*TracedLLM) Name added in v0.1.3

func (m *TracedLLM) Name() string

Name implements interfaces.LLM.Name

func (*TracedLLM) SupportsStreaming added in v0.1.3

func (m *TracedLLM) SupportsStreaming() bool

SupportsStreaming implements interfaces.LLM.SupportsStreaming

type TracedMemory added in v0.1.3

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

TracedMemory implements middleware for memory operations with unified tracing

func NewTracedMemory added in v0.1.3

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

NewTracedMemory creates a new memory middleware with unified tracing

func (*TracedMemory) AddMessage added in v0.1.3

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

AddMessage adds a message to memory with tracing

func (*TracedMemory) Clear added in v0.1.3

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

Clear clears memory with tracing

func (*TracedMemory) GetMessages added in v0.1.3

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