metrics

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package metrics is Ken's tiny, dependency-free Prometheus exposition: a handful of atomic counters plus on-scrape gauge collectors, rendered as text/plain exposition format. It deliberately does NOT use prometheus/client_golang — that would add ~1-2 MB and several transitive deps; Ken values a flat footprint, so the counters are plain atomics and the encoder is ~100 lines. Metrics are pull-based and in-memory: Ken stores nothing on disk for them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OutcomeClass

func OutcomeClass(code int) string

OutcomeClass maps an HTTP status to a coarse, low-cardinality outcome bucket (the conventional informational/success/redirection/client_error/server_error).

Types

type Collector

type Collector func(context.Context) []Family

Collector produces gauge families at scrape time (e.g. DB row counts). It is given the scrape request's context so a slow DB can't wedge the handler.

type Family

type Family struct {
	Name, Type, Help string
	Series           []Series
}

Family is a metric family: one HELP/TYPE header plus N series. Collectors return these for on-scrape gauges.

func Gauge

func Gauge(name, help string, v float64) Family

Gauge is a convenience constructor for a single-series unlabelled gauge family.

type Label

type Label struct{ Name, Value string }

Label is a single name=value pair on a metric series.

type Registry

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

Registry holds Ken's counters and gauge collectors. The zero value is not usable; call New.

func New

func New(version string) *Registry

New builds an empty registry stamped with the build version.

func (*Registry) AddCollector

func (r *Registry) AddCollector(c Collector)

AddCollector registers a gauge source evaluated on each scrape.

func (*Registry) AuthFailure

func (r *Registry) AuthFailure(surface string)

AuthFailure records a rejected authentication on a surface (e.g. a bad token).

func (*Registry) Counting

func (r *Registry) Counting(surface string, next http.Handler) http.Handler

Counting wraps next, recording each served request under surface. Health and metrics paths are skipped so probes/scrapes don't inflate the counters.

func (*Registry) RateLimitBlocked

func (r *Registry) RateLimitBlocked()

RateLimitBlocked records a 403 refusal from an auto-blocked IP.

func (*Registry) RateLimitRejected

func (r *Registry) RateLimitRejected()

RateLimitRejected records a 429 throttle.

func (*Registry) RecordHTTP

func (r *Registry) RecordHTTP(surface, outcome string, d time.Duration)

RecordHTTP records one served HTTP request and its duration for a surface.

func (*Registry) RecordMCP

func (r *Registry) RecordMCP(tool string, ok bool)

RecordMCP records one MCP tool call and whether it succeeded.

func (*Registry) RecordMCPDuration added in v1.3.0

func (r *Registry) RecordMCPDuration(tool string, d time.Duration)

RecordMCPDuration records how long a tool's HANDLER ran — its work time, not the SSE stream lifetime. The caller must NOT call this for a tool that intentionally blocks (a long-poll): a parked wait is not latency, and bucketing it would drown the real work time. See the addTool wrappers, which skip the blocking tools.

func (*Registry) WriteText

func (r *Registry) WriteText(ctx context.Context, w io.Writer)

WriteText renders the current metrics in Prometheus text exposition format.

type Series

type Series struct {
	Labels []Label
	Value  float64
}

Series is one labelled value within a metric family.

Jump to

Keyboard shortcuts

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