metrics

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package metrics implements per-tool latency reservoirs, error counters, and aggregate summaries used by the reporter and compare subcommand.

Index

Constants

View Source
const DefaultReservoirSize = 1_000_000

DefaultReservoirSize is the default number of latency samples retained per tool. With a 1M ring buffer the p99 is accurate up to ~3M requests per run (within ±0.5%). Users can lower this with NewReservoir for memory-tight scenarios.

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregateSummary

type AggregateSummary struct {
	Overall  ToolSummary            `json:"overall"`
	PerTool  map[string]ToolSummary `json:"per_tool"`
	ToolKeys []string               `json:"tool_keys"`
}

AggregateSummary is the full finalized report fed to formatters and compare.

type Aggregator

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

Aggregator holds a global reservoir plus per-tool stats.

func NewAggregator

func NewAggregator(reservoirSize int) *Aggregator

NewAggregator creates a new aggregator with the specified per-reservoir capacity.

func (*Aggregator) Global

func (a *Aggregator) Global() *ToolStats

Global returns the global overall stats.

func (*Aggregator) Record

func (a *Aggregator) Record(tool string, dur time.Duration, err error)

Record a single observation against both the per-tool and global reservoir.

func (*Aggregator) SummarizeAll

func (a *Aggregator) SummarizeAll() AggregateSummary

SummarizeAll produces a finalized view of all tools plus overall.

func (*Aggregator) Tool

func (a *Aggregator) Tool(name string) *ToolStats

Tool returns the stats for a single tool, or nil if unknown.

func (*Aggregator) ToolNames

func (a *Aggregator) ToolNames() []string

ToolNames returns a sorted slice of tool names tracked.

type Reservoir

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

Reservoir stores up to Size samples; when full, new samples replace old slots in round-robin order. This is a fixed-window approximation — the test suite documents expected behaviour.

func NewReservoir

func NewReservoir(size int) *Reservoir

NewReservoir constructs a reservoir with the specified capacity. size must be positive; negative or zero inputs are treated as 1.

func (*Reservoir) Count

func (r *Reservoir) Count() int

Count returns the total number of samples observed.

func (*Reservoir) Insert

func (r *Reservoir) Insert(d time.Duration)

Insert adds a duration sample.

func (*Reservoir) Len

func (r *Reservoir) Len() int

Len returns the number of samples currently retained.

func (*Reservoir) Snapshot

func (r *Reservoir) Snapshot() Summary

Snapshot returns a finalized summary. The reservoir is not mutated.

type Summary

type Summary struct {
	Count  int           `json:"count"`
	Min    time.Duration `json:"min"`
	Max    time.Duration `json:"max"`
	Mean   time.Duration `json:"mean"`
	StdDev time.Duration `json:"stddev"`
	P50    time.Duration `json:"p50"`
	P90    time.Duration `json:"p90"`
	P95    time.Duration `json:"p95"`
	P99    time.Duration `json:"p99"`
	// SampleCount is the number of samples retained (may be less than Count
	// when the reservoir overflowed).
	SampleCount int `json:"sample_count"`
}

Summary is the finalized statistics for one reservoir.

type ToolStats

type ToolStats struct {
	Name      string
	Reservoir *Reservoir
	// contains filtered or unexported fields
}

ToolStats aggregates all metrics for a single tool name.

func NewToolStats

func NewToolStats(name string, reservoirSize int) *ToolStats

NewToolStats creates a ToolStats with an empty reservoir of the specified size.

func (*ToolStats) ErrorBreakdown

func (s *ToolStats) ErrorBreakdown() map[mcperrors.Category]int64

ErrorBreakdown returns a copy of per-category counts.

func (*ToolStats) ErrorCount

func (s *ToolStats) ErrorCount() int64

ErrorCount returns total errors across all categories.

func (*ToolStats) OKCount

func (s *ToolStats) OKCount() int64

OKCount returns the number of successful requests.

func (*ToolStats) RPCErrorCodes

func (s *ToolStats) RPCErrorCodes() map[int]int64

RPCErrorCodes returns a copy of the count-by-code map for jsonrpc errors.

func (*ToolStats) Record

func (s *ToolStats) Record(dur time.Duration, err error)

Record a single observation.

func (*ToolStats) Summarize

func (s *ToolStats) Summarize() ToolSummary

Summarize produces a finalized, serialization-ready snapshot.

type ToolSummary

type ToolSummary struct {
	Name      string                       `json:"name"`
	Latency   Summary                      `json:"latency"`
	OK        int64                        `json:"ok"`
	Errors    int64                        `json:"errors"`
	ErrorPct  float64                      `json:"error_pct"`
	Breakdown map[mcperrors.Category]int64 `json:"breakdown"`
	RPCCodes  map[int]int64                `json:"rpc_codes"`
}

ToolSummary finalizes a ToolStats into a printable record.

Jump to

Keyboard shortcuts

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