metrics

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package metrics is a zero-dependency, lock-free performance counter registry embedded in the agent. It tracks turns, tokens, tool calls, cache hits, errors, and latencies using atomic counters so it's safe to read from any goroutine without blocking the hot path.

The counters are exposed via `ok doctor --metrics` and can be introspected by the agent itself at runtime (self-aware infrastructure).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheHit

func CacheHit()

CacheHit records a cache hit (no compaction needed).

func CacheHitRate

func CacheHitRate() float64

CacheHitRate returns the cache hit ratio (0..1), or 0 if no data.

func CacheMiss

func CacheMiss()

CacheMiss records a compaction (cache eviction).

func Compaction

func Compaction()

Compaction records a session compaction.

func ErrorNonRetry

func ErrorNonRetry()

ErrorNonRetry records a permanent error.

func ErrorRate

func ErrorRate() float64

ErrorRate returns error ratio per tool call.

func ErrorRetry

func ErrorRetry()

ErrorRetry records a transient, retried error.

func HealthScore

func HealthScore() int

HealthScore returns 0-100 reflecting overall agent health.

func MeanToolLatency

func MeanToolLatency() time.Duration

MeanToolLatency returns the average tool execution duration, or 0.

func MeanTurnLatency

func MeanTurnLatency() time.Duration

MeanTurnLatency returns the average turn duration, or 0.

func Panic

func Panic()

Panic records a recovered goroutine panic.

func Snapshot

func Snapshot()

Snapshot records a session snapshot write.

func Start

func Start()

Start marks the agent as alive.

func Tokens

func Tokens(prompt, output int64)

Tokens records token usage for a turn.

func ToolCall

func ToolCall()

ToolCall records a tool dispatch.

func ToolDenied

func ToolDenied()

ToolDenied records a tool denied by the permission gate.

func ToolError

func ToolError()

ToolError records a tool that returned an error.

func ToolLatency

func ToolLatency(d time.Duration)

ToolLatency records the duration of a tool execution in microseconds.

func ToolResult

func ToolResult()

ToolResult records a successful tool return.

func Turn

func Turn()

Turn records a turn submission.

func TurnCancelled

func TurnCancelled()

TurnCancelled records a user-cancelled turn.

func TurnLatency

func TurnLatency(d time.Duration)

TurnLatency records the duration of a completed turn in microseconds.

func TurnSucceeded

func TurnSucceeded()

TurnSucceeded records a successful turn completion.

func Uptime

func Uptime() time.Duration

Uptime returns the duration since Start.

func WriteReport

func WriteReport(w io.Writer)

WriteReport writes a human-readable metrics report to w.

Types

type Registry

type Registry struct {
	Turns          atomic.Int64 // total turns submitted
	TurnsSucceeded atomic.Int64 // turns that completed without error
	TurnsCancelled atomic.Int64 // turns cancelled by user
	TokensPrompt   atomic.Int64 // cumulative prompt tokens
	TokensOutput   atomic.Int64 // cumulative output tokens
	ToolCalls      atomic.Int64 // total tool dispatch attempts
	ToolResults    atomic.Int64 // tool calls that returned successfully
	ToolErrors     atomic.Int64 // tool calls that returned errors
	ToolDenied     atomic.Int64 // tool calls denied by permission gate
	CacheHits      atomic.Int64 // estimated prefix-cache hits (turns where tokens were >0 and last turn was compacted)
	CacheMisses    atomic.Int64 // turns where compaction ran (cache was evicted)
	ErrorsNonRetry atomic.Int64 // permanent errors (auth, quota)
	ErrorsRetry    atomic.Int64 // transient errors that were retried
	Compactions    atomic.Int64 // session compactions performed
	Snapshots      atomic.Int64 // session snapshots written
	Panics         atomic.Int64 // goroutine panics recovered
	Uptime         atomic.Int64 // agent start time unix nano

	// Latency histograms are approximated via sum+count for mean calculation.
	TurnLatencySum   atomic.Int64 // cumulative turn duration in microseconds
	TurnLatencyCount atomic.Int64 // number of turns with measured latency
	ToolLatencySum   atomic.Int64 // cumulative tool duration in microseconds
	ToolLatencyCount atomic.Int64 // number of tools with measured latency
}

Registry holds all atomic performance counters.

Jump to

Keyboard shortcuts

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