llmobs

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TagKeySessionID is the tag key used to set the session ID for LLMObs spans.
	TagKeySessionID = "session_id"
)

Variables

This section is empty.

Functions

func ContextWithPropagatedLLMSpan

func ContextWithPropagatedLLMSpan(ctx context.Context, span *PropagatedLLMSpan) context.Context

ContextWithPropagatedLLMSpan returns a new context with the given PropagatedLLMSpan attached.

func Flush

func Flush()

Flush forces a flush of all buffered LLMObs data to the transport.

func PublicResourceBaseURL

func PublicResourceBaseURL() string

PublicResourceBaseURL returns the base URL to access a resource (experiments, projects, etc.)

func Start

func Start(cfg config.Config, tracer Tracer) (err error)

Start starts the global LLMObs instance with the given configuration and tracer. Returns an error if LLMObs is already running or if configuration is invalid.

func Stop

func Stop()

Stop stops the active LLMObs instance and cleans up resources.

Types

type APMSpan

type APMSpan interface {
	// Finish finishes the span with the given configuration.
	Finish(cfg FinishAPMSpanConfig)
	// AddLink adds a span link to this span.
	AddLink(link SpanLink)
	// SpanID returns the span ID.
	SpanID() string
	// TraceID returns the trace ID.
	TraceID() string
	// SetBaggageItem sets a baggage item on the span.
	SetBaggageItem(key string, value string)
}

APMSpan represents the interface for an APM span.

type EmbeddedDocument

type EmbeddedDocument struct {
	// Text is the text content of the document.
	Text string `json:"text"`
	// Name is the name or title of the document.
	Name string `json:"name,omitempty"`
	// Score is the relevance score of the document (typically 0.0-1.0).
	Score float64 `json:"score,omitempty"`
	// ID is the unique identifier of the document.
	ID string `json:"id,omitempty"`
}

EmbeddedDocument represents a document used for embedding operations.

type EvaluationConfig

type EvaluationConfig struct {
	// Method 1: Direct span/trace ID join
	// SpanID is the span ID to evaluate.
	SpanID string
	// TraceID is the trace ID to evaluate.
	TraceID string

	// Method 2: Tag-based join
	// TagKey is the tag key to search for spans.
	TagKey string
	// TagValue is the tag value to match for spans.
	TagValue string

	// Required fields
	// Label is the name of the evaluation metric.
	Label string

	// Value fields (exactly one must be provided)
	// CategoricalValue is the categorical value of the evaluation metric.
	CategoricalValue *string
	// ScoreValue is the score value of the evaluation metric.
	ScoreValue *float64
	// BooleanValue is the boolean value of the evaluation metric.
	BooleanValue *bool

	// Optional fields
	// Tags are optional string key-value pairs to tag the evaluation metric.
	Tags []string
	// MLApp is the ML application name. If empty, uses the global config.
	MLApp string
	// TimestampMS is the timestamp in milliseconds. If zero, uses current time.
	TimestampMS int64
}

EvaluationConfig contains configuration for submitting evaluation metrics.

type FinishAPMSpanConfig

type FinishAPMSpanConfig struct {
	// FinishTime is the finish time for the span.
	FinishTime time.Time
	// Error is an error to set on the span when finishing.
	Error error
}

FinishAPMSpanConfig contains configuration options for finishing an APM span.

type FinishSpanConfig

type FinishSpanConfig struct {
	// FinishTime sets a custom finish time for the span. If zero, uses current time.
	FinishTime time.Time
	// Error sets an error on the span when finishing.
	Error error
}

FinishSpanConfig contains configuration options for finishing an LLMObs span.

type LLMMessage

type LLMMessage struct {
	// Role is the role of the message sender (e.g., "user", "assistant", "system").
	Role string `json:"role"`
	// Content is the text content of the message.
	Content string `json:"content"`
	// ToolCalls are the tool calls made in this message.
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
	// ToolResults are the results of tool calls in this message.
	ToolResults []ToolResult `json:"tool_results,omitempty"`
}

LLMMessage represents a message in an LLM conversation.

type LLMObs

type LLMObs struct {
	// Config contains the LLMObs configuration.
	Config *config.Config
	// Transport handles sending data to the Datadog backend.
	Transport *transport.Transport
	// Tracer is the underlying APM tracer.
	Tracer Tracer
	// contains filtered or unexported fields
}

LLMObs represents the main LLMObs instance that handles span collection and transport.

func ActiveLLMObs

func ActiveLLMObs() (*LLMObs, error)

ActiveLLMObs returns the current active LLMObs instance, or an error if LLMObs is not enabled or started.

func (*LLMObs) Flush

func (l *LLMObs) Flush()

Flush forces an immediate flush of anything currently buffered. It does not wait for new items to arrive.

func (*LLMObs) Run

func (l *LLMObs) Run()

Run starts the worker loop that processes span events and metrics.

func (*LLMObs) StartExperimentSpan

func (l *LLMObs) StartExperimentSpan(ctx context.Context, name string, experimentID string, cfg StartSpanConfig) (*Span, context.Context)

StartExperimentSpan starts a new experiment span with the given name, experiment ID, and configuration. Returns the created span and a context containing the span.

func (*LLMObs) StartSpan

func (l *LLMObs) StartSpan(ctx context.Context, kind SpanKind, name string, cfg StartSpanConfig) (*Span, context.Context)

StartSpan starts a new LLMObs span with the given kind, name, and configuration. Returns the created span and a context containing the span.

func (*LLMObs) Stop

func (l *LLMObs) Stop()

Stop requests shutdown, drains what’s already in the channels, flushes, and waits.

func (*LLMObs) SubmitEvaluation

func (l *LLMObs) SubmitEvaluation(cfg EvaluationConfig) (err error)

SubmitEvaluation submits an evaluation metric for a span. The span can be identified either by span/trace IDs or by tag key-value pairs.

type Prompt

type Prompt struct {
	// Template is the prompt template string.
	Template string `json:"template,omitempty"`
	// ID is the unique identifier for the prompt.
	ID string `json:"id,omitempty"`
	// Version is the version of the prompt.
	Version string `json:"version,omitempty"`
	// Variables contains the variables used in the prompt template.
	Variables map[string]string `json:"variables,omitempty"`
	// RAGContextVariables specifies which variables contain RAG context.
	RAGContextVariables []string `json:"rag_context_variables,omitempty"`
	// RAGQueryVariables specifies which variables contain RAG queries.
	RAGQueryVariables []string `json:"rag_query_variables,omitempty"`
}

Prompt represents a prompt template used with LLM spans.

type PropagatedLLMSpan

type PropagatedLLMSpan struct {
	// MLApp is the ML application name.
	MLApp string
	// TraceID is the LLMObs trace ID.
	TraceID string
	// SpanID is the span ID.
	SpanID string
}

PropagatedLLMSpan represents LLMObs span context that can be propagated across process boundaries.

func PropagatedLLMSpanFromContext

func PropagatedLLMSpanFromContext(ctx context.Context) (*PropagatedLLMSpan, bool)

PropagatedLLMSpanFromContext retrieves a PropagatedLLMSpan from the context. Returns the span and true if found, nil and false otherwise.

type RetrievedDocument

type RetrievedDocument struct {
	// Text is the text content of the document.
	Text string `json:"text"`
	// Name is the name or title of the document.
	Name string `json:"name,omitempty"`
	// Score is the relevance score of the document (typically 0.0-1.0).
	Score float64 `json:"score,omitempty"`
	// ID is the unique identifier of the document.
	ID string `json:"id,omitempty"`
}

RetrievedDocument represents a document for retrieval operations.

type Span

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

Span represents an LLMObs span with its associated metadata and context.

func ActiveLLMSpanFromContext

func ActiveLLMSpanFromContext(ctx context.Context) (*Span, bool)

ActiveLLMSpanFromContext retrieves the active LLMObs span from the context. Returns the span and true if found, nil and false otherwise.

func (*Span) APMTraceID

func (s *Span) APMTraceID() string

APMTraceID returns the trace ID of the underlying APM span.

func (s *Span) AddLink(link SpanLink)

AddLink adds a span link to this span.

func (*Span) Annotate

func (s *Span) Annotate(a SpanAnnotations)

Annotate adds annotations to the span using the provided SpanAnnotations.

func (*Span) Finish

func (s *Span) Finish(cfg FinishSpanConfig)

Finish finishes the span with the provided configuration.

func (*Span) FinishTime

func (s *Span) FinishTime() time.Time

FinishTime returns the finish time of this span.

func (*Span) Kind

func (s *Span) Kind() string

func (*Span) MLApp

func (s *Span) MLApp() string

MLApp returns the ML application name for this span.

func (*Span) Name

func (s *Span) Name() string

func (*Span) SpanID

func (s *Span) SpanID() string

SpanID returns the span ID of the underlying APM span.

func (*Span) StartTime

func (s *Span) StartTime() time.Time

StartTime returns the start time of this span.

func (*Span) TraceID

func (s *Span) TraceID() string

TraceID returns the LLMObs trace ID for this span.

type SpanAnnotations

type SpanAnnotations struct {
	// InputText is the text input for the span.
	InputText string
	// InputMessages are the input messages for LLM spans.
	InputMessages []LLMMessage
	// InputEmbeddedDocs are the input documents for embedding spans.
	InputEmbeddedDocs []EmbeddedDocument

	// OutputText is the text output for the span.
	OutputText string
	// OutputMessages are the output messages for LLM spans.
	OutputMessages []LLMMessage
	// OutputRetrievedDocs are the output documents for retrieval spans.
	OutputRetrievedDocs []RetrievedDocument

	// ExperimentInput is the input data for experiment spans.
	ExperimentInput any
	// ExperimentOutput is the output data for experiment spans.
	ExperimentOutput any
	// ExperimentExpectedOutput is the expected output for experiment spans.
	ExperimentExpectedOutput any

	// Prompt is the prompt information for LLM spans.
	Prompt *Prompt
	// ToolDefinitions are the tool definitions for LLM spans.
	ToolDefinitions []ToolDefinition

	// Intent is a description of a reason for calling an MCP tool on tool spans
	Intent string

	// AgentManifest is the agent manifest for agent spans.
	AgentManifest string

	// Metadata contains arbitrary metadata key-value pairs.
	Metadata map[string]any
	// Metrics contains numeric metrics key-value pairs.
	Metrics map[string]float64
	// Tags contains string tags key-value pairs.
	Tags map[string]string
}

SpanAnnotations contains data to annotate an LLMObs span with.

type SpanKind

type SpanKind string

SpanKind represents the type of an LLMObs span.

const (
	// SpanKindExperiment represents an experiment span for testing and evaluation.
	SpanKindExperiment SpanKind = "experiment"
	// SpanKindWorkflow represents a workflow span that orchestrates multiple operations.
	SpanKindWorkflow SpanKind = "workflow"
	// SpanKindLLM represents a span for Large Language Model operations.
	SpanKindLLM SpanKind = "llm"
	// SpanKindEmbedding represents a span for embedding generation operations.
	SpanKindEmbedding SpanKind = "embedding"
	// SpanKindAgent represents a span for AI agent operations.
	SpanKindAgent SpanKind = "agent"
	// SpanKindRetrieval represents a span for document retrieval operations.
	SpanKindRetrieval SpanKind = "retrieval"
	// SpanKindTask represents a span for general task operations.
	SpanKindTask SpanKind = "task"
	// SpanKindTool represents a span for tool usage operations.
	SpanKindTool SpanKind = "tool"
)
type SpanLink = transport.SpanLink

SpanLink represents a link between spans, aliased from the transport package.

type StartAPMSpanConfig

type StartAPMSpanConfig struct {
	// SpanType is the type of the APM span.
	SpanType string
	// StartTime is the start time for the span.
	StartTime time.Time
}

StartAPMSpanConfig contains configuration options for starting an APM span.

type StartSpanConfig

type StartSpanConfig struct {
	// SessionID sets the session ID for the span.
	SessionID string
	// ModelName sets the model name for LLM and embedding spans.
	ModelName string
	// ModelProvider sets the model provider for LLM and embedding spans.
	ModelProvider string
	// MLApp sets the ML application name for the span.
	MLApp string
	// StartTime sets a custom start time for the span. If zero, uses current time.
	StartTime time.Time
	// Name of the tracing integration.
	Integration string
}

StartSpanConfig contains configuration options for starting an LLMObs span.

type ToolCall

type ToolCall struct {
	// Name is the name of the tool being called.
	Name string `json:"name"`
	// Arguments are the JSON-encoded arguments passed to the tool.
	Arguments json.RawMessage `json:"arguments"`
	// ToolID is the unique identifier for this tool call.
	ToolID string `json:"tool_id,omitempty"`
	// Type is the type of the tool call.
	Type string `json:"type,omitempty"`
}

ToolCall represents a call to a tool within an LLM message.

type ToolDefinition

type ToolDefinition struct {
	// Name is the name of the tool.
	Name string `json:"name"`
	// Description is the description of what the tool does.
	Description string `json:"description,omitempty"`
	// Schema is the JSON schema defining the tool's parameters.
	Schema json.RawMessage `json:"schema,omitempty"`
}

ToolDefinition represents a tool definition for LLM spans.

type ToolResult

type ToolResult struct {
	// Result is the result returned by the tool.
	Result any `json:"result"`
	// Name is the name of the tool that was called.
	Name string `json:"name,omitempty"`
	// ToolID is the unique identifier for the tool call this result corresponds to.
	ToolID string `json:"tool_id,omitempty"`
	// Type is the type of the tool result.
	Type string `json:"type,omitempty"`
}

ToolResult represents the result of a tool call within an LLM message.

type Tracer

type Tracer interface {
	// StartSpan starts a new APM span with the given name and configuration.
	StartSpan(ctx context.Context, name string, cfg StartAPMSpanConfig) (APMSpan, context.Context)
}

Tracer represents the interface for the underlying APM tracer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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