metrics

package
v0.30.3 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package metrics owns the Prometheus registry and the /metrics HTTP handler. Consumers declare their own metrics (typically in their package's metrics.go), register them via Register, and use prometheus.Counter/Gauge/Histogram normally.

Conventions

Every metric must set Namespace to metrics.Namespace ("relay") and pick a Subsystem matching the package name (e.g. "eventlog", "auth", "usage").

Naming:

  • Counters end in _total (prometheus convention; client_golang enforces this)
  • Gauges describe a current value (e.g. queue_depth)
  • Histograms end in _seconds or _bytes

Labels: use sparingly. High-cardinality labels (e.g. per-request IDs) explode memory. Prefer separate counters over a label with O(N) values.

Index

Constants

View Source
const (
	CatalogCheckUnchanged     = "unchanged"
	CatalogCheckUpdated       = "updated"
	CatalogCheckFailed        = "failed"
	CatalogCheckSkippedLocked = "skipped_locked"
	CatalogCheckSkippedOlder  = "skipped_older"
	// CatalogCheckHintUnseen is a re-check after a release webhook whose
	// index still didn't list the announced release.
	CatalogCheckHintUnseen = "hint_unseen"
)

Results of one catalog check, the values of CatalogChecks' result label.

View Source
const (
	// DropUnsupportedByModel: the routed model declares the param unsupported.
	DropUnsupportedByModel = "unsupported_by_model"
	// DropSupersededByRename: the host renames the param to a key the caller
	// also sent, so the caller's value under the new key wins.
	DropSupersededByRename = "superseded_by_rename"
)

Reasons a request parameter is dropped before the upstream call; the same strings name the reason in the response warning header.

View Source
const (
	KVRefused  = "refused"
	KVShed     = "shed"
	KVFallback = "fallback"
	KVDropped  = "dropped"
)

KV error outcomes.

View Source
const Namespace = "relay"

Namespace is the shared Prometheus namespace for every Relay metric. Concrete metrics SHOULD set this as their Namespace and pick a Subsystem matching their package (e.g. "eventlog", "auth", "usage").

Variables

View Source
var (
	// RecordsLost counts background records dropped because a bounded
	// emitter queue was full or a WAL dropped segments over its disk cap
	// (usage rows, payload captures). The async contract mandates this
	// counter: a drop you can't see is a billing or audit hole. `kind` is
	// the emitter queue: "usage" | "usage_refused" | "payload" | "payload_refused",
	// or "last_used" for a key the last-used recorder had no room for.
	RecordsLost = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "records_lost_total",
			Help:      "Background records dropped due to a full emitter queue or a WAL over its segment cap, by kind (usage, payload, and usage_refused/payload_refused for requests that never reached an upstream), or a key the last-used recorder had no room for (last_used).",
		},
		[]string{"kind"},
	)

	// FlushLag is the age of the oldest un-flushed WAL segment, by kind
	// (usage/payload). RecordsLost fires only when data is already gone
	// and emit_queue_depth watches the in-memory channel — but a sink
	// outage (ClickHouse down) grows the on-disk WAL for hours with both
	// of those green. This gauge answers "how stale is my usage/billing
	// data": 0 = fully drained; rising = the sink is not accepting.
	FlushLag = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: Namespace,
			Name:      "flush_lag_seconds",
			Help:      "Age in seconds of the oldest un-flushed WAL segment, by kind (usage/payload). 0 = drained; rising = the sink is not accepting data.",
		},
		[]string{"kind"},
	)

	// ProviderKeysDown counts the times a pooled provider key was placed
	// into cooldown by a failure — the leading indicator of heading
	// toward "no healthy keys in pool". `reason` is the failure class
	// (auth/rate_limit/server_error/network).
	//
	// A counter, not a gauge: breaker state lives in shared kv, so a
	// per-pod gauge of "keys down right now" would be inconsistent across
	// the fleet. Trip counts sum cleanly. A faithful global gauge is
	// deferred (would need a kv scan — not lightweight).
	ProviderKeysDown = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "provider_keys_down_total",
			Help:      "Times a pooled provider key was put into cooldown by a failure, by reason.",
		},
		[]string{"reason"},
	)
)

Health signals: the two "am I about to have a problem" metrics — silent data loss (Q3) and provider keys dying (Q4). Both are one-liner emitters the owning packages call at the relevant moment; nothing here reaches back into them.

View Source
var (
	// RequestsTotal answers "how much traffic, how many errors" (Q1/Q2).
	// `status` is a bounded class (2xx/4xx/5xx), never the raw code.
	RequestsTotal = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: Namespace,
			Name:      "requests_total",
			Help:      "Total requests handled, by source runner and status class (2xx/4xx/5xx).",
		},
		[]string{"source", "status"},
	)

	// RequestSeconds is total end-to-end time (Q2 — the "whose time is
	// it" numerator). Wide buckets span cached/error responses through
	// long generation runs.
	RequestSeconds = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: Namespace,
			Name:      "request_seconds",
			Help:      "End-to-end request latency (handler entry to response closed).",
			Buckets: []float64{
				0.001, 0.005, 0.01, 0.05, 0.1,
				0.25, 0.5, 1, 2, 5, 10, 30, 60, 120,
			},
		},
		[]string{"source"},
	)

	// OverheadSeconds is THE wedge metric (Q1): Relay's own time, total
	// minus the upstream call. The performance contract puts a hard SLO
	// here (p99 < 10ms live). Tight sub-second buckets: 100µs → 500ms.
	OverheadSeconds = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: Namespace,
			Name:      "overhead_seconds",
			Help:      "Relay overhead = total handler time minus the upstream call. Observed only when upstream was reached.",
			Buckets:   []float64{0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.25, 0.5},
		},
		[]string{"source"},
	)

	// InflightRequests answers "how many streams are open right now /
	// where does concurrency plateau" (Q5). A streamed request counts
	// until its body closes (post-flight fires at Close).
	InflightRequests = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: Namespace,
			Name:      "inflight_requests",
			Help:      "Currently-open requests, by source runner. Streams count until body close.",
		},
		[]string{"source"},
	)

	// InflightLimit is the configured admission cap — the denominator that
	// turns InflightRequests into headroom ("how close am I to shedding").
	// Without it the cap lives only in an env var and no dashboard can
	// plot inflight/limit or alert on approach. Constant after boot.
	InflightLimit = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: Namespace,
			Name:      "inflight_limit",
			Help:      "Hard per-pod in-flight admission cap. relay_inflight_adaptive_limit never exceeds it.",
		},
	)

	// InflightAdaptiveLimit is the limit requests are shed at: the hard cap, lowered while the pod is CPU-saturated. Its distance below InflightLimit is why a pod sheds before its cap.
	InflightAdaptiveLimit = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: Namespace,
			Name:      "inflight_adaptive_limit",
			Help:      "Effective per-pod in-flight admission limit: relay_inflight_limit, lowered while the process is CPU-saturated. Compare against relay_inflight_requests for shed headroom.",
		},
	)

	// SchedLatencyP99Seconds is the CPU-saturation signal the adaptive limit acts on, published so an operator can see what it saw.
	SchedLatencyP99Seconds = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: Namespace,
			Name:      "sched_latency_p99_seconds",
			Help:      "p99 time a runnable goroutine waited for a CPU over the adaptive limit's last 100ms window. 0 while the adaptive limit is off.",
		},
	)

	// ProcessCPUShare is the adaptive limit's main signal, published so an operator can see what it saw.
	ProcessCPUShare = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: Namespace,
			Name:      "process_cpu_share",
			Help:      "Process CPU time (user + system) over the adaptive limit's last 100ms window, as a share of GOMAXPROCS. 0 while the adaptive limit is off.",
		},
	)

	// AdmissionSeconds is Timing.Start → upstream handoff: auth +
	// rate-limit reserve + key selection — the Redis-contention proxy
	// (Q5). Skipped when the request never reached upstream. Same
	// sub-second bucket style as OverheadSeconds: 100µs → 250ms.
	AdmissionSeconds = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: Namespace,
			Name:      "admission_seconds",
			Help:      "Time from request accept to upstream handoff (auth + rate-limit reserve + key selection). Observed only when upstream was reached.",
			Buckets:   []float64{0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.25},
		},
		[]string{"source"},
	)

	// PostFlightSeconds is the duration of one full detached post-flight
	// goroutine — token extraction, the observer fan-out (Registry.Finalize),
	// and the rate-limit / host-health commit phase that runs after it. Same
	// Q5 question ("how long does relay's bookkeeping run after a request"),
	// now including the Valkey commit RTTs that dominate it.
	PostFlightSeconds = prometheus.NewHistogram(
		prometheus.HistogramOpts{
			Namespace: Namespace,
			Name:      "post_flight_seconds",
			Help:      "Duration of the detached post-flight goroutine (token extraction + observer fan-out + rate-limit/host-health commits) per request.",
			Buckets:   []float64{0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5},
		},
	)
)

The request-flow metrics (Rate / Errors / Duration) plus the Relay-vs- upstream split.

Labelled by `source` (the runner: pipeline/proxy/ws/batch) — the lowest-cardinality dimension already on the lifecycle Context. Wire `shape` is deliberately NOT a label: it isn't carried on the Context and plumbing it would touch the inference handler. Add it the day the question "openai vs anthropic traffic split" is actually asked.

View Source
var CatalogApplyErrors = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Namespace: Namespace,
		Name:      "catalog_apply_errors_total",
		Help:      "Catalog change events that failed to apply incrementally, by kind.",
	},
	[]string{"kind"},
)

CatalogApplyErrors counts catalog NOTIFY events that failed to apply incrementally. Each failure falls back to a full reload, so a rising rate means the store is flaky and pods are paying for rebuilds.

View Source
var CatalogChecks = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Namespace: Namespace,
		Name:      "catalog_checks_total",
		Help:      "Catalog index checks made while running, by result (unchanged, updated, failed, skipped_locked, skipped_older, hint_unseen).",
	},
	[]string{"result"},
)

CatalogChecks counts the catalog index checks a relay following the latest catalog release makes while running, by result.

View Source
var CatalogSeededVersion = prometheus.NewGaugeVec(
	prometheus.GaugeOpts{
		Namespace: Namespace,
		Name:      "catalog_seeded_version",
		Help:      "1 for the catalog release the database was last seeded from (label version).",
	},
	[]string{"version"},
)

CatalogSeededVersion is an info gauge: 1 for the catalog release the database was last seeded from, as this pod last read it.

View Source
var DroppedParams = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Namespace: Namespace,
		Name:      "dropped_params_total",
		Help:      "Request parameters stripped pre-upstream, by param, model and reason (unsupported_by_model, superseded_by_rename).",
	},
	[]string{"param", "model", "reason"},
)

DroppedParams counts request parameters the relay stripped before the upstream call: params the routed model declares unsupported (Capabilities.UnsupportedParams), and params a host rename makes redundant. A sustained rate names the models whose callers still send params the upstream would reject — the strip is deliberate, but it should be visible, never silent.

View Source
var KVErrors = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Namespace: Namespace,
		Name:      "kv_errors_total",
		Help:      "Failed kv calls on the inference path, by op and outcome (refused = request rejected, shed = request rejected with the call unsent because the connection wait queue was full, fallback = decided without the stored state, dropped = write abandoned).",
	},
	[]string{"op", "outcome"},
)

KVErrors counts kv calls on the inference path that failed, by call site and by what relay did instead. A store that stalls or drops connections shows up here before it shows up as refused traffic.

View Source
var SettleQueueFull = prometheus.NewCounter(prometheus.CounterOpts{
	Namespace: Namespace,
	Name:      "settle_queue_full_total",
	Help:      "Post-flight settles that found the settle queue full and ran on their own Valkey round trip instead of a shared one.",
})

SettleQueueFull counts settles that found the post-flight settle queue full and ran on a round trip of their own. Nothing is dropped; a rate means the queue's writers fall behind.

View Source
var SettleRetries = prometheus.NewCounter(prometheus.CounterOpts{
	Namespace: Namespace,
	Name:      "settle_retries_total",
	Help:      "Queued post-flight settles resent on their own Valkey round trip after their shared round trip failed in transport.",
})

SettleRetries counts queued settles whose shared round trip failed in transport (store unavailable, deadline) and were resent on a round trip of their own; the commit guard makes a resent commit that already ran a no-op.

View Source
var StreamEventsDropped = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Namespace: Namespace,
		Name:      "stream_events_dropped_total",
		Help:      "Streamed events skipped because a shape translator could not convert them, by adapter, direction and reason.",
	},
	[]string{"adapter", "direction", "reason"},
)

StreamEventsDropped counts streamed events relay skipped because a shape translator could not convert them. Bytes have usually reached the caller by then, so the frame is skipped rather than the stream failed; this counter keeps the skip visible. adapter is the shape whose translator failed, direction is to_canonical (upstream frame) or from_canonical (inbound frame).

View Source
var UpstreamConnections = prometheus.NewCounterVec(
	prometheus.CounterOpts{
		Namespace: Namespace,
		Name:      "upstream_connections_total",
		Help:      "Upstream connections obtained per attempt, by kind (new = dialed, reused = keep-alive pool). new ≈ request rate means connection churn.",
	},
	[]string{"kind"},
)

UpstreamConnections answers "is Relay re-dialing the provider on every request" — the connection-churn fear. The stdlib's MaxIdleConnsPerHost default of 2 silently cost ~40ms of upstream p50 before the 2026-07-08 campaign found it by profiling; a new:reused ratio approaching the request rate names that failure in one glance. `kind` is "new" (dialed) or "reused" (from the keep-alive pool).

Functions

func AddRecordsLost added in v0.11.7

func AddRecordsLost(kind string, n int)

AddRecordsLost counts n records lost at once, e.g. a WAL segment dropped over its disk cap.

func CatalogApplyFailed added in v0.9.25

func CatalogApplyFailed(kind string)

CatalogApplyFailed is the one-liner the catalog listener calls when an event fails to apply.

func CatalogChecked added in v0.20.1

func CatalogChecked(result string)

CatalogChecked records one catalog check's result.

func DroppedParam added in v0.9.12

func DroppedParam(param, model, reason string)

DroppedParam records one stripped parameter for one request.

func Handler

func Handler() http.Handler

Handler returns the http.Handler serving /metrics in Prometheus text format.

func InflightDec added in v0.3.0

func InflightDec(source string)

InflightDec marks a request finalized. Callers must pair with a prior InflightInc for the same request or the gauge goes negative.

func InflightInc added in v0.3.0

func InflightInc(source string)

InflightInc marks a request admitted. Hot path: O(1), allocation-free.

func KVError added in v0.11.7

func KVError(op, outcome string)

KVError is the one-liner a kv consumer calls when a call fails.

func ProviderKeyDown

func ProviderKeyDown(reason string)

ProviderKeyDown is the one-liner keypool calls when a key transitions into cooldown.

func RecordAdmission added in v0.3.0

func RecordAdmission(source string, admission time.Duration)

RecordAdmission is the one-liner the post-flight metrics observer calls when the request reached upstream (Timing.Upstream.Start was stamped).

func RecordLost

func RecordLost(kind string)

RecordLost is the one-liner an emitter calls at its drop site.

func RecordPostFlight added in v0.3.0

func RecordPostFlight(time.Duration)

RecordPostFlight was the lifecycle.Registry finalize-observer feed for post_flight_seconds, timing only the Registry.Finalize fan-out. The metric now spans the whole detached post-flight goroutine — including the rate-limit / host-health commit phase that runs *after* Finalize returns, which an observer firing inside Finalize cannot see. The runners record it via RecordPostFlightTotal; this callback is retained as a deliberate no-op so the existing SetFinalizeObserver wiring can't double-count the histogram. Drop the SetFinalizeObserver call when convenient.

func RecordPostFlightTotal added in v0.9.8

func RecordPostFlightTotal(d time.Duration)

RecordPostFlightTotal records the wall-clock duration of one detached post-flight goroutine (token extraction + observer fan-out + commits), called by the runners once the goroutine finishes.

func RecordServed

func RecordServed(source string, status int, total, upstream time.Duration)

RecordServed is the one-liner the post-flight metrics observer calls once per finalized request. total is the full handler time; upstream is the upstream-call duration (zero when upstream wasn't reached, in which case the overhead split is not observed — it'd be meaningless).

func Register

func Register(collectors ...prometheus.Collector)

Register adds collectors to the shared registry. Panics on duplicate registration (consistent with prometheus.MustRegister).

func RegisterQueueDepth added in v0.3.0

func RegisterQueueDepth(kind string, fn func() float64)

RegisterQueueDepth exposes a bounded emitter's queue depth as relay_emit_queue_depth{kind=...} — the leading signal for the drops RecordsLost counts after the fact. Same kinds as records_lost_total. fn is sampled at scrape time (chan len is concurrency-safe). Call once per kind at boot, where the emitter is constructed.

func ReportFlushLag added in v0.9.8

func ReportFlushLag(kind string, seconds float64)

ReportFlushLag is the one-liner a WAL queue calls after each flush pass with the age of its oldest remaining segment (0 when drained).

func SafeLabel

func SafeLabel(s string) string

SafeLabel returns "unknown" if s is empty, preventing empty-string Prometheus labels.

func SetCatalogSeededVersion added in v0.20.1

func SetCatalogSeededVersion(version string)

SetCatalogSeededVersion makes version the only series of the info gauge; an empty version (never seeded from a release) leaves none.

func SetInflightAdaptiveLimit added in v0.20.0

func SetInflightAdaptiveLimit(n int)

SetInflightAdaptiveLimit publishes the effective in-flight limit.

func SetInflightLimit added in v0.9.8

func SetInflightLimit(n int)

SetInflightLimit publishes the admission cap. Called once at boot where the Admission semaphore is constructed.

func SetProcessCPUShare added in v0.20.0

func SetProcessCPUShare(share float64)

SetProcessCPUShare publishes the process CPU share of the last window.

func SetSchedLatencyP99 added in v0.20.0

func SetSchedLatencyP99(d time.Duration)

SetSchedLatencyP99 publishes the scheduler latency p99 of the last window.

func StatusClass

func StatusClass(code int) string

StatusClass returns a bounded status label ("2xx", "4xx", "5xx", etc.) for an HTTP status code. Using a class avoids high-cardinality label explosion.

func StreamEventDropped added in v0.10.1

func StreamEventDropped(adapter, direction, reason string)

StreamEventDropped records one skipped stream event.

func UpstreamConn added in v0.9.8

func UpstreamConn(reused bool)

UpstreamConn is the one-liner the upstream transport's httptrace hook calls when a connection is handed to an attempt.

Types

This section is empty.

Jump to

Keyboard shortcuts

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