observability

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package observability provides OpenTelemetry metrics and tracing for the SDK.

Index

Constants

View Source
const (
	ClassUnsupportedHookEvent  errclass.Class = "unsupported_hook_event"
	ClassUnsupportedHookOutput errclass.Class = "unsupported_hook_output"
	ClassExecution             errclass.Class = "execution"
)

SDK-local error classes. Define only where distinguishing the class on dashboards is worth the extra cardinality.

Variables

View Source
var (
	StatusClassKey    = attribute.Key("status_class")
	RetryKey          = attribute.Key("retry")
	RetryAttemptKey   = attribute.Key("retry.attempt")
	RetryDelayKey     = attribute.Key("retry.delay")
	OutcomeKey        = attribute.Key("outcome")
	CheckpointOpKey   = attribute.Key("checkpoint.op")
	HookEventKey      = attribute.Key("hook.event")
	ThinkingTokensKey = attribute.Key("thinking.tokens")
)

SDK-local attribute keys. Not covered by any OTel spec; OpenRouter-specific operational labels. Value sets must stay bounded (cardinality discipline).

Functions

func CheckpointOp

func CheckpointOp(v string) attribute.KeyValue

func FinishReasons

func FinishReasons(reasons ...string) attribute.KeyValue

FinishReasons returns the gen_ai.response.finish_reasons span attribute. Per GenAI semconv this is a string array; OpenRouter has a single stop reason.

func HookEvent

func HookEvent(v string) attribute.KeyValue

func NewPrometheusMeterProvider

func NewPrometheusMeterProvider(reg prometheus.Registerer) (metric.MeterProvider, error)

func Outcome

func Outcome(v string) attribute.KeyValue

func Retry

func Retry(v bool) attribute.KeyValue

func RetryAttempt

func RetryAttempt(v int) attribute.KeyValue

func RetryDelay

func RetryDelay(v time.Duration) attribute.KeyValue

func StatusClass

func StatusClass(v string) attribute.KeyValue

func StatusClassOf

func StatusClassOf(code int) string

StatusClassOf returns "2xx"/"3xx"/"4xx"/"5xx"/"other" for an HTTP status code.

func ThinkingTokens

func ThinkingTokens(v int64) attribute.KeyValue

Types

type Config

type Config struct {
	MeterProvider  metric.MeterProvider
	TracerProvider trace.TracerProvider
	Logger         *slog.Logger
}

Config holds the providers and logger used when constructing an Observer.

type HTTPStatusError

type HTTPStatusError struct {
	StatusCode int
	Body       string
}

HTTPStatusError is returned by the transport for non-2xx HTTP responses.

func (*HTTPStatusError) Error

func (e *HTTPStatusError) Error() string

type Observer

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

Observer holds the meter/tracer and pre-constructed instruments.

func New

func New(cfg Config) (*Observer, error)

New returns an Observer. Providers default to noop when nil. The upstream instrument constructors can return errors; New propagates them.

func Noop

func Noop() *Observer

Noop returns an Observer with noop providers. A noop meter cannot produce a real instrument-construction error; a non-nil error here would be a library bug, so we panic rather than force every caller to handle an impossible case.

func (*Observer) Classify

func (o *Observer) Classify(err error) agenterrclass.Class

Classify returns the error.type class for err. Returns "" for nil.

func (*Observer) RecordCheckpointOp

func (o *Observer) RecordCheckpointOp(ctx context.Context, op, outcome string)

RecordCheckpointOp increments the checkpoint-ops counter and emits a span event.

func (*Observer) RecordHTTPRequest

func (o *Observer) RecordHTTPRequest(ctx context.Context, statusClass string, isRetry bool)

RecordHTTPRequest increments the HTTP request counter.

func (*Observer) RecordHTTPRequestDuration

func (o *Observer) RecordHTTPRequestDuration(ctx context.Context, seconds float64, statusClass string, isRetry bool)

RecordHTTPRequestDuration records the HTTP request duration histogram.

func (*Observer) RecordHookDuration

func (o *Observer) RecordHookDuration(ctx context.Context, seconds float64, event, outcome string)

RecordHookDuration records the hook dispatch duration histogram.

func (*Observer) RecordOperationDuration

func (o *Observer) RecordOperationDuration(
	ctx context.Context,
	seconds float64,
	operationName upstreamgenai.OperationNameAttr,
	model string,
	class agenterrclass.Class,
)

RecordOperationDuration records the spec gen_ai.client.operation.duration histogram with provider+operation positional attributes and optional request.model / error.type extras.

func (*Observer) RecordRateLimitEvent

func (o *Observer) RecordRateLimitEvent(ctx context.Context)

RecordRateLimitEvent increments the rate-limit counter and emits a span event.

func (*Observer) RecordTTFT

func (o *Observer) RecordTTFT(ctx context.Context, seconds float64, model string)

RecordTTFT records time-to-first-token on the SDK-local histogram.

func (*Observer) RecordTokenUsage

func (o *Observer) RecordTokenUsage(
	ctx context.Context,
	tokens int64,
	tokenType upstreamgenai.TokenTypeAttr,
	operationName upstreamgenai.OperationNameAttr,
	model string,
)

RecordTokenUsage records the spec gen_ai.client.token.usage histogram. Emits a span event for "thinking" tokens.

func (*Observer) RecordToolCall

func (o *Observer) RecordToolCall(ctx context.Context, toolName, outcome string)

RecordToolCall increments the tool-call counter.

func (*Observer) RecordToolCallDuration

func (o *Observer) RecordToolCallDuration(ctx context.Context, seconds float64, toolName string)

RecordToolCallDuration records the tool-call duration histogram.

func (*Observer) RegisterMatcher

func (o *Observer) RegisterMatcher(matcher agenterrclass.Matcher)

RegisterMatcher appends a custom matcher to the registry.

func (*Observer) RegisterSentinel

func (o *Observer) RegisterSentinel(err error, class agenterrclass.Class)

RegisterSentinel wires a sentinel error to a class.

func (*Observer) StartHTTPSpan

func (o *Observer) StartHTTPSpan(
	ctx context.Context,
	endpoint string,
) (context.Context, *agenttracer.Span)

StartHTTPSpan opens a child span for a vLLM HTTP request. The transport only issues POSTs, so method is hardcoded.

func (*Observer) StartHookSpan

func (o *Observer) StartHookSpan(ctx context.Context, event string) (context.Context, *agenttracer.Span)

StartHookSpan opens a child span for a hook dispatch.

func (*Observer) StartQuerySpan

func (o *Observer) StartQuerySpan(
	ctx context.Context,
	operationName upstreamgenai.OperationNameAttr,
	model string,
	conversationID string,
) (context.Context, *agenttracer.Span)

StartQuerySpan opens the top-level span for a Query()/Client.Query() call.

func (*Observer) StartToolSpan

func (o *Observer) StartToolSpan(ctx context.Context, toolName, callID string) (context.Context, *agenttracer.Span)

StartToolSpan opens a child span for a tool invocation. Spec-conformant "execute_tool {name}" span name and gen_ai.operation.name=execute_tool attribute per GenAI semantic conventions. provider.name is set so tool spans carry the same filterable labels as query spans. callID is optional; pass "" if the tool call has no upstream identifier.

Jump to

Keyboard shortcuts

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