telemetry

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package telemetry defines the observability exporter boundary for AgentKit. Session JSONL remains the source of truth; exporters mirror turn/step/tool activity to external systems such as Langfuse.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginObservation

func BeginObservation(ctx context.Context, meta ObservationMeta) (context.Context, func(ObservationEnd))

BeginObservation starts a nested observation on the exporter stored in ctx.

func BeginTurn

func BeginTurn(ctx context.Context, meta TurnMeta) (context.Context, func(TurnEnd))

BeginTurn starts a turn on the exporter stored in ctx.

func EnrichEventAttrs added in v0.1.5

func EnrichEventAttrs(ctx context.Context, attrs map[string]string) map[string]string

EnrichEventAttrs adds agent_id and session_id from ctx when missing.

func ParentObservationIDFrom added in v0.1.5

func ParentObservationIDFrom(ctx context.Context) string

ParentObservationIDFrom is an alias for ToolParentFrom.

func PreparePayload

func PreparePayload(raw string, maxBytes int, redact bool) string

PreparePayload applies optional redaction and truncation for exporter payloads.

func RecordEvent

func RecordEvent(ctx context.Context, name string, attrs map[string]string)

RecordEvent records a point-in-time event on the active trace.

func RedactJSON

func RedactJSON(raw string) string

RedactJSON scrubs sensitive keys from JSON objects before export.

func ScopeParentFrom added in v0.1.5

func ScopeParentFrom(ctx context.Context) string

ScopeParentFrom returns the active scope parent observation id when present.

func SummarizeMessage

func SummarizeMessage(msg agentkit.ModelMessage) string

SummarizeMessage returns a compact text summary of a model message.

func SummarizeMessages

func SummarizeMessages(messages []agentkit.ModelMessage, maxBytes int, redact bool) string

SummarizeMessages JSON-encodes a message list for exporter input.

func ToolParentFrom added in v0.1.5

func ToolParentFrom(ctx context.Context) string

ToolParentFrom returns the active tool parent observation id when present.

func TruncatePayload

func TruncatePayload(raw string, maxBytes int) string

TruncatePayload limits payload size for external export.

func TurnIDFrom

func TurnIDFrom(ctx context.Context) string

TurnIDFrom returns the active turn id when present.

func WithExporter

func WithExporter(ctx context.Context, exp Exporter) context.Context

WithExporter stores an exporter on the context for downstream runtime code.

func WithParentObservationID added in v0.1.5

func WithParentObservationID(ctx context.Context, observationID string) context.Context

WithParentObservationID is an alias for WithToolParent.

func WithScopeParent added in v0.1.5

func WithScopeParent(ctx context.Context, observationID string) context.Context

WithScopeParent marks the span that should parent nested generations.

func WithToolParent added in v0.1.5

func WithToolParent(ctx context.Context, observationID string) context.Context

WithToolParent marks the observation that should parent nested tool spans.

func WithTurnID

func WithTurnID(ctx context.Context, turnID string) context.Context

WithTurnID stores the active turn id for slog and exporter correlation.

Types

type Exporter

type Exporter interface {
	BeginTurn(ctx context.Context, meta TurnMeta) (context.Context, func(TurnEnd))
	BeginObservation(ctx context.Context, meta ObservationMeta) (context.Context, func(ObservationEnd))
	RecordEvent(ctx context.Context, name string, attrs map[string]string)
	Flush(ctx context.Context) error
	Shutdown(ctx context.Context) error
}

Exporter mirrors agent activity to an external observability backend.

var Noop Exporter = noopExporter{}

Noop is the default exporter that discards all telemetry.

func ExporterFrom

func ExporterFrom(ctx context.Context) Exporter

ExporterFrom returns the exporter on ctx, or Noop when unset.

type ObservationEnd

type ObservationEnd struct {
	Output string
	Err    error
	Usage  *Usage
}

ObservationEnd closes an observation.

type ObservationKind

type ObservationKind string

ObservationKind selects how an observation is exported.

const (
	KindSpan       ObservationKind = "span"
	KindGeneration ObservationKind = "generation"
	KindTool       ObservationKind = "tool"
)

type ObservationMeta

type ObservationMeta struct {
	Name  string
	Kind  ObservationKind
	Model string
	Input string
	// AgentID labels which agent produced this observation.
	AgentID string
	// SessionID labels the session that produced this observation.
	SessionID string
	// Scope marks a span that should parent nested generations (e.g. subagent.turn).
	Scope bool
}

ObservationMeta describes a nested observation such as an LLM call or tool.

func ObservationMetaFromContext added in v0.1.5

func ObservationMetaFromContext(ctx context.Context, meta ObservationMeta) ObservationMeta

ObservationMetaFromContext fills agent and session ids from ctx when unset.

type RecordedEvent

type RecordedEvent struct {
	Name  string
	Attrs map[string]string
}

type RecordedObservation

type RecordedObservation struct {
	ID       string
	Meta     ObservationMeta
	End      ObservationEnd
	ParentID string
}

type RecordedTurn

type RecordedTurn struct {
	Meta TurnMeta
	End  TurnEnd
}

type RecordingExporter

type RecordingExporter struct {
	Turns         []RecordedTurn
	Observations  []RecordedObservation
	Events        []RecordedEvent
	FlushCalls    int
	ShutdownCalls int
	// contains filtered or unexported fields
}

RecordingExporter captures telemetry calls for tests.

func (*RecordingExporter) BeginObservation

func (r *RecordingExporter) BeginObservation(ctx context.Context, meta ObservationMeta) (context.Context, func(ObservationEnd))

func (*RecordingExporter) BeginTurn

func (r *RecordingExporter) BeginTurn(ctx context.Context, meta TurnMeta) (context.Context, func(TurnEnd))

func (*RecordingExporter) Flush

func (*RecordingExporter) RecordEvent

func (r *RecordingExporter) RecordEvent(_ context.Context, name string, attrs map[string]string)

func (*RecordingExporter) Shutdown

func (r *RecordingExporter) Shutdown(context.Context) error

func (*RecordingExporter) Snapshot

func (r *RecordingExporter) Snapshot() (turns []RecordedTurn, observations []RecordedObservation, events []RecordedEvent)

type TurnEnd

type TurnEnd struct {
	Output string
	Err    error
}

TurnEnd closes a turn trace.

type TurnMeta

type TurnMeta struct {
	TurnID            string
	SessionID         string
	DeliverySessionID string
	AgentID           string
	PlatformID        string
	UserID            string
	Input             string
}

TurnMeta describes one agent RunTurn boundary.

type Usage

type Usage struct {
	InputTokens  int
	OutputTokens int
	TotalTokens  int
}

Usage carries token accounting for generations.

Jump to

Keyboard shortcuts

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