metrics

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package metrics is a small, dependency-free Prometheus metrics registry. It implements just enough of the text exposition format (counters, histograms, gauges) to instrument Legant without pulling in the full client library — in keeping with the project's lean-dependency posture. The default registry is pre-loaded with HTTP request metrics, a Go-runtime collector, and the product-level counters (token exchanges, revocations, gateway calls).

Index

Constants

This section is empty.

Variables

View Source
var (
	HTTPRequestsTotal = Default.NewCounter(
		"legant_http_requests_total",
		"Total HTTP requests handled, by method, route pattern, and status code.",
		"method", "route", "code")

	HTTPRequestDuration = Default.NewHistogram(
		"legant_http_request_duration_seconds",
		"HTTP request latency in seconds, by method and route pattern.",
		DefaultBuckets, "method", "route")

	HTTPRequestsInFlight = Default.NewGauge(
		"legant_http_requests_in_flight",
		"Requests currently being served.")

	// TokenExchangesTotal counts RFC 8693 token-exchange attempts by outcome.
	TokenExchangesTotal = Default.NewCounter(
		"legant_token_exchanges_total",
		"RFC 8693 token-exchange attempts, by result (success|error).",
		"result")

	// DelegationsTotal counts delegations created (the top of the funnel): a user
	// consenting to delegate to an agent, or an agent re-delegating to a sub-agent.
	DelegationsTotal = Default.NewCounter(
		"legant_delegations_total",
		"Delegations created, by kind (consent|redelegate).",
		"kind")

	// TokensMintedTotal counts composite delegation tokens actually signed.
	TokensMintedTotal = Default.NewCounter(
		"legant_tokens_minted_total",
		"Composite delegation tokens minted, by source (exchange|gateway).",
		"source")

	// RevocationsTotal counts revocation events.
	RevocationsTotal = Default.NewCounter(
		"legant_revocations_total",
		"Revocation events, by kind (token|delegation).",
		"kind")

	// RevocationCheckErrorsTotal counts revocation-store lookup failures. These
	// fail closed (the request is denied), so without a metric they are invisible —
	// a spike means the store is unhealthy and tokens are being rejected.
	RevocationCheckErrorsTotal = Default.NewCounter(
		"legant_revocation_check_errors_total",
		"Revocation-store lookup failures, by component (gateway|introspection).",
		"component")

	// GatewayCallsTotal counts MCP gateway tool calls by upstream and decision.
	GatewayCallsTotal = Default.NewCounter(
		"legant_gateway_calls_total",
		"MCP gateway tool calls, by upstream and decision (allow|deny|unauthorized|error).",
		"upstream", "decision")
)

Product-level metrics. These are incremented at the relevant choke points (token exchange, revocation, gateway tool calls) to give an at-a-glance view of delegation activity that raw HTTP counts cannot.

View Source
var Default = NewRegistry()

Default is the process-wide registry. It is pre-loaded with HTTP request metrics, a Go-runtime collector, and the product-level counters below.

View Source
var DefaultBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}

DefaultBuckets are the request-duration histogram buckets, in seconds.

Functions

func Handler

func Handler() http.Handler

Handler serves the default registry's metrics in the Prometheus text format.

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware records request count and latency for every HTTP request, labeled by method, the chi route pattern (bounded cardinality — never the raw path), and status code.

func SetBuildInfo

func SetBuildInfo(version string)

SetBuildInfo records the running version as a build_info gauge.

Types

type CounterVec

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

CounterVec is a family of monotonically increasing counters partitioned by a fixed set of label names.

func (*CounterVec) Add

func (c *CounterVec) Add(n uint64, labelValues ...string)

Add adds n to the counter for the given label values.

func (*CounterVec) Inc

func (c *CounterVec) Inc(labelValues ...string)

Inc increments the counter for the given label values by one.

type GaugeVec

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

GaugeVec is a family of gauges (values that go up and down) partitioned by a fixed set of label names. Values are integers, sufficient for counts such as in-flight requests.

func (*GaugeVec) Dec

func (g *GaugeVec) Dec(labelValues ...string)

Dec decrements the gauge for the given label values by one.

func (*GaugeVec) Inc

func (g *GaugeVec) Inc(labelValues ...string)

Inc increments the gauge for the given label values by one.

type HistogramVec

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

HistogramVec is a family of histograms partitioned by a fixed set of labels.

func (*HistogramVec) Observe

func (h *HistogramVec) Observe(v float64, labelValues ...string)

Observe records a single observation for the given label values.

type Registry

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

Registry holds a set of metric families and renders them in the Prometheus text exposition format. It is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) AddCollector

func (r *Registry) AddCollector(fn func(w io.Writer))

AddCollector registers a callback that writes additional exposition lines at scrape time (used for runtime gauges sampled on demand).

func (*Registry) AddGoCollector

func (r *Registry) AddGoCollector()

AddGoCollector registers gauges sampled from the Go runtime at scrape time.

func (*Registry) Handler

func (r *Registry) Handler() http.Handler

Handler returns an http.Handler that serves the registry's metrics.

func (*Registry) NewCounter

func (r *Registry) NewCounter(name, help string, labels ...string) *CounterVec

NewCounter registers and returns a labeled counter family.

func (*Registry) NewGauge

func (r *Registry) NewGauge(name, help string, labels ...string) *GaugeVec

NewGauge registers and returns a labeled gauge family (a value that can go up and down, e.g. in-flight requests).

func (*Registry) NewHistogram

func (r *Registry) NewHistogram(name, help string, buckets []float64, labels ...string) *HistogramVec

NewHistogram registers and returns a labeled histogram family.

func (*Registry) Render

func (r *Registry) Render(w io.Writer)

Render renders all registered metrics in the text exposition format.

func (*Registry) SetBuildInfo

func (r *Registry) SetBuildInfo(version, goVersion string)

SetBuildInfo registers a constant build_info gauge labeled with the version.

Jump to

Keyboard shortcuts

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