metrics

package
v1.3.10 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package metrics provides an interface-based metrics system for SRouter. It defines interfaces for metrics collection and exposition, allowing users to provide their own implementations while the framework uses the methods exposed by these interfaces to aggregate metrics. This approach maintains separation of concerns, where the framework defines the interfaces and the users provide the implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter interface {
	Metric

	// Inc increments the counter by 1.
	Inc()

	// Add adds the given value to the counter.
	Add(value float64)
}

Counter is a metric that represents a monotonically increasing value.

type CounterBuilder

type CounterBuilder interface {
	// Name sets the counter name.
	Name(name string) CounterBuilder

	// Description sets the counter description.
	Description(desc string) CounterBuilder

	// Tag adds a tag to the counter.
	Tag(key, value string) CounterBuilder

	// Build creates the counter.
	Build() Counter
}

CounterBuilder is a builder for creating counters.

type Gauge

type Gauge interface {
	Metric

	// Set sets the gauge to the given value.
	Set(value float64)

	// Inc increments the gauge by 1.
	Inc()

	// Dec decrements the gauge by 1.
	Dec()

	// Add adds the given value to the gauge.
	Add(value float64)

	// Sub subtracts the given value from the gauge.
	Sub(value float64)
}

Gauge is a metric that represents a value that can go up and down.

type GaugeBuilder

type GaugeBuilder interface {
	// Name sets the gauge name.
	Name(name string) GaugeBuilder

	// Description sets the gauge description.
	Description(desc string) GaugeBuilder

	// Tag adds a tag to the gauge.
	Tag(key, value string) GaugeBuilder

	// Build creates the gauge.
	Build() Gauge
}

GaugeBuilder is a builder for creating gauges.

type Histogram

type Histogram interface {
	Metric

	// Observe adds a single observation to the histogram.
	Observe(value float64)
}

Histogram is a metric that samples observations and counts them in configurable buckets.

type HistogramBuilder

type HistogramBuilder interface {
	// Name sets the histogram name.
	Name(name string) HistogramBuilder

	// Description sets the histogram description.
	Description(desc string) HistogramBuilder

	// Tag adds a tag to the histogram.
	Tag(key, value string) HistogramBuilder

	// Buckets sets the bucket boundaries.
	Buckets(buckets []float64) HistogramBuilder

	// Build creates the histogram.
	Build() Histogram
}

HistogramBuilder is a builder for creating histograms.

type Metric

type Metric interface {
	// Name returns the metric name.
	Name() string

	// Description returns the metric description.
	Description() string

	// Type returns the metric type.
	Type() MetricType

	// Tags returns the metric tags.
	Tags() Tags
}

Metric is the base interface for all metrics.

type MetricType

type MetricType string

MetricType represents the type of a metric.

const (
	// CounterType represents a counter metric.
	CounterType MetricType = "counter"

	// GaugeType represents a gauge metric.
	GaugeType MetricType = "gauge"

	// HistogramType represents a histogram metric.
	HistogramType MetricType = "histogram"

	// SummaryType represents a summary metric.
	SummaryType MetricType = "summary"
)

type MetricsFilter

type MetricsFilter interface {
	// Filter returns true if metrics should be collected for the request.
	Filter(r *http.Request) bool
}

MetricsFilter is a filter for metrics collection.

type MetricsMiddleware

type MetricsMiddleware[T comparable, U any] interface {
	// Wrap an HTTP handler with metrics collection.
	Handler(name string, handler http.Handler) http.Handler

	// Configure the middleware.
	Configure(config MetricsMiddlewareConfig) MetricsMiddleware[T, U]

	// Add a filter to the middleware.
	WithFilter(filter MetricsFilter) MetricsMiddleware[T, U]

	// Add a sampler to the middleware.
	WithSampler(sampler MetricsSampler) MetricsMiddleware[T, U]
}

MetricsMiddleware is a generic middleware for collecting metrics. T is the UserID type (comparable), U is the User object type (any).

type MetricsMiddlewareConfig

type MetricsMiddlewareConfig struct {
	// EnableLatency enables latency metrics.
	EnableLatency bool

	// EnableThroughput enables throughput metrics.
	EnableThroughput bool

	// EnableQPS enables queries per second metrics.
	EnableQPS bool

	// EnableErrors enables error metrics.
	EnableErrors bool

	// SamplingRate is the rate at which to sample requests.
	SamplingRate float64

	// DefaultTags are tags to add to all metrics.
	DefaultTags Tags
}

MetricsMiddlewareConfig is the configuration for metrics middleware. This config itself doesn't need to be generic, as the T and U types are relevant to the middleware instance, not the configuration values.

type MetricsMiddlewareImpl added in v1.0.4

type MetricsMiddlewareImpl[T comparable, U any] struct {
	// contains filtered or unexported fields
}

MetricsMiddlewareImpl is a concrete generic implementation of the MetricsMiddleware interface. T is the UserID type (comparable), U is the User object type (any).

func NewMetricsMiddleware added in v1.0.4

func NewMetricsMiddleware[T comparable, U any](registry MetricsRegistry, config MetricsMiddlewareConfig) *MetricsMiddlewareImpl[T, U]

NewMetricsMiddleware creates a new generic MetricsMiddlewareImpl. T is the UserID type (comparable), U is the User object type (any).

func (*MetricsMiddlewareImpl[T, U]) Configure added in v1.0.4

func (m *MetricsMiddlewareImpl[T, U]) Configure(config MetricsMiddlewareConfig) MetricsMiddleware[T, U]

Configure configures the middleware.

func (*MetricsMiddlewareImpl[T, U]) Handler added in v1.0.4

func (m *MetricsMiddlewareImpl[T, U]) Handler(name string, handler http.Handler) http.Handler

Handler wraps an HTTP handler with metrics collection. It captures metrics such as request latency, throughput, QPS, and errors based on the middleware configuration. The metrics are collected using the registry provided to the middleware. The 'name' parameter can be used as a fallback identifier if route template information is not available. This is now a method on the generic MetricsMiddlewareImpl[T, U].

func (*MetricsMiddlewareImpl[T, U]) WithFilter added in v1.0.4

func (m *MetricsMiddlewareImpl[T, U]) WithFilter(filter MetricsFilter) MetricsMiddleware[T, U]

WithFilter adds a filter to the middleware.

func (*MetricsMiddlewareImpl[T, U]) WithSampler added in v1.0.4

func (m *MetricsMiddlewareImpl[T, U]) WithSampler(sampler MetricsSampler) MetricsMiddleware[T, U]

WithSampler adds a sampler to the middleware.

type MetricsRegistry

type MetricsRegistry interface {
	// Register a metric with the registry.
	Register(metric Metric) error

	// Create a new counter builder.
	NewCounter() CounterBuilder

	// Create a new gauge builder.
	NewGauge() GaugeBuilder

	// Create a new histogram builder.
	NewHistogram() HistogramBuilder

	// Create a new summary builder.
	NewSummary() SummaryBuilder
}

MetricsRegistry is a registry for metrics.

type MetricsSampler

type MetricsSampler interface {
	// Sample returns true if the request should be sampled.
	Sample() bool
}

MetricsSampler is a sampler for metrics collection.

type RandomSampler

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

RandomSampler is a sampler that randomly samples requests.

func NewRandomSampler

func NewRandomSampler(rate float64) *RandomSampler

NewRandomSampler creates a new random sampler.

func NewRandomSamplerWithRand added in v1.2.13

func NewRandomSamplerWithRand(rate float64, r *rand.Rand) *RandomSampler

NewRandomSamplerWithRand returns a RandomSampler that uses the provided `rand.Rand` for deterministic sampling. The `rate` parameter specifies the sampling rate, where 0 means no sampling and 1 means always sample.

func (*RandomSampler) Sample

func (s *RandomSampler) Sample() bool

Sample returns true if the request should be sampled.

type Summary

type Summary interface {
	Metric

	// Observe adds a single observation to the summary.
	Observe(value float64)
}

Summary is a metric that samples observations and calculates quantiles over a sliding time window.

type SummaryBuilder

type SummaryBuilder interface {
	// Name sets the summary name.
	Name(name string) SummaryBuilder

	// Description sets the summary description.
	Description(desc string) SummaryBuilder

	// Tag adds a tag to the summary.
	Tag(key, value string) SummaryBuilder

	// Objectives sets the quantile objectives.
	Objectives(objectives map[float64]float64) SummaryBuilder

	// MaxAge sets the maximum age of observations.
	MaxAge(maxAge time.Duration) SummaryBuilder

	// AgeBuckets sets the number of age buckets.
	AgeBuckets(ageBuckets int) SummaryBuilder

	// Build creates the summary.
	Build() Summary
}

SummaryBuilder is a builder for creating summaries.

type Tags

type Tags map[string]string

Tags represents a map of key-value pairs for metric tags.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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