metrics

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package metrics centralizes the Prometheus collectors Parsec exposes.

Cardinality budget: channel names, subject IDs, and bearer tokens are NEVER labels. Visibility (public/private), result enums, and bounded method/sink names are the only acceptable label values.

Every subsystem that records a metric reaches for a singleton Metrics value via New (typically constructed once at parsec.New and wired into the subsystem at construction). The package never registers against the global prometheus.DefaultRegisterer — callers supply their own registry or take the package-default one returned by NewWithRegistry(nil).

Index

Constants

View Source
const (
	ResultSuccess = "success"
	ResultFailure = "failure"

	VisibilityPublic  = "public"
	VisibilityPrivate = "private"
	VisibilityUnknown = "unknown"

	TokenTypeAccess  = "access"
	TokenTypeRefresh = "refresh"
	TokenTypeMgmt    = "mgmt"

	KeyActionGenerated = "generated"
	KeyActionPromoted  = "promoted"
	KeyActionRetired   = "retired"

	RPCNonRPC = "non_rpc"
)

LabelValue constants keep call-site labels consistent and prevent typos from blowing the cardinality budget.

Variables

This section is empty.

Functions

func Middleware

func Middleware(m *Metrics, next http.Handler) http.Handler

Middleware returns an http.Handler that records request count + duration against the supplied Metrics bundle.

The method label is the last URL segment for Twirp paths (e.g. "Publish", "ListChannels") and the literal "non_rpc" for anything else. This keeps cardinality bounded — even a brute-force scan of arbitrary paths can't inflate the label set beyond the known method names.

func VisibilityLabel

func VisibilityLabel(v string) string

VisibilityLabel maps a channels.Visibility-typed string to a stable label value. Falls back to VisibilityUnknown for unrecognized inputs so we never accept an unbounded user-controlled label.

func WrapSink

func WrapSink(inner sinks.Sink, m *Metrics) sinks.Sink

WrapSink returns a sinks.Sink that observes Send calls into m. When m is nil, inner is returned unchanged.

Types

type Metrics

type Metrics struct {
	Registry *prometheus.Registry

	PublishesTotal         *prometheus.CounterVec
	PublishDuration        *prometheus.HistogramVec
	SubscribersActive      *prometheus.GaugeVec
	ChannelsActive         *prometheus.GaugeVec
	TokenVerificationTotal *prometheus.CounterVec
	SinkAttemptsTotal      *prometheus.CounterVec
	SinkDuration           *prometheus.HistogramVec
	DLQSize                *prometheus.GaugeVec
	KeyRotationsTotal      *prometheus.CounterVec
	RPCRequestsTotal       *prometheus.CounterVec
	RPCDuration            *prometheus.HistogramVec
	RateLimitDecisions     *prometheus.CounterVec
}

Metrics is the bundle of Parsec collectors. Exported fields make call sites read like prom client code: metrics.PublishesTotal.With(...).Inc().

Construct with New. Each instance owns its own *prometheus.Registry so tests can spin up independent metric universes.

func New

func New() *Metrics

New constructs a fresh Metrics bundle backed by a fresh registry. The registry includes Go runtime + process collectors for free.

func NewWithRegistry

func NewWithRegistry(reg *prometheus.Registry) *Metrics

NewWithRegistry constructs a Metrics bundle bound to reg. If reg is nil a fresh prometheus.Registry is created.

All collectors are registered eagerly so /metrics output is stable even before the first publish. If reg already has any of these collectors registered, the duplicate registrations are skipped — useful when callers share a registry across subsystems.

func NewWithRegistryAndRegion

func NewWithRegistryAndRegion(reg *prometheus.Registry, region string) *Metrics

NewWithRegistryAndRegion is NewWithRegistry plus a constant region label applied to every Parsec-defined collector via prometheus.WrapRegistererWith. Pass region="" (the default) to skip the label entirely so single-region deployments stay label-free. The Go/process collectors are NOT region-labeled — they describe the host, not Parsec semantics.

func Noop

func Noop() *Metrics

Noop returns a process-wide no-op Metrics value. It still registers collectors so increments don't panic; the registry is just unreachable.

Jump to

Keyboard shortcuts

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