metrics

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package metrics provides an enhanced metrics system for SRouter. It includes a fluent API, first-class tags support, and separation of collection and exposition.

Package metrics provides an enhanced metrics system for SRouter. This file contains the Prometheus implementation of the metrics system.

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)

	// Value returns the current value of the counter.
	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)

	// Value returns the current value of the gauge.
	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)

	// Buckets returns the bucket boundaries.
	Buckets() []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

	// WithTags returns a new metric with the given tags.
	WithTags(tags Tags) Metric
}

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 MetricsExporter

type MetricsExporter interface {
	// Export metrics to the backend.
	Export(snapshot MetricsSnapshot) error

	// Start the exporter.
	Start() error

	// Stop the exporter.
	Stop() error

	// Return an HTTP handler for exposing metrics.
	Handler() http.Handler
}

MetricsExporter is an exporter for metrics.

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 interface {
	// Wrap an HTTP handler with metrics collection.
	Handler(name string, handler http.Handler) http.Handler

	// Configure the middleware.
	Configure(config MetricsMiddlewareConfig) MetricsMiddleware

	// Add a filter to the middleware.
	WithFilter(filter MetricsFilter) MetricsMiddleware

	// Add a sampler to the middleware.
	WithSampler(sampler MetricsSampler) MetricsMiddleware
}

MetricsMiddleware is a middleware for collecting metrics.

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.

type MetricsRegistry

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

	// Get a metric by name.
	Get(name string) (Metric, bool)

	// Unregister a metric from the registry.
	Unregister(name string) bool

	// Clear all metrics from the registry.
	Clear()

	// Get a snapshot of all metrics.
	Snapshot() MetricsSnapshot

	// Create a new registry with the given tags.
	WithTags(tags Tags) MetricsRegistry

	// 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 MetricsSnapshot

type MetricsSnapshot interface {
	// Counters returns all counters in the snapshot.
	Counters() []Counter

	// Gauges returns all gauges in the snapshot.
	Gauges() []Gauge

	// Histograms returns all histograms in the snapshot.
	Histograms() []Histogram

	// Summaries returns all summaries in the snapshot.
	Summaries() []Summary
}

MetricsSnapshot is a snapshot of all metrics in a registry.

type PrometheusCounter

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

PrometheusCounter implements Counter using Prometheus.

func (*PrometheusCounter) Add

func (c *PrometheusCounter) Add(value float64)

Add adds the given value to the counter.

func (*PrometheusCounter) Description

func (c *PrometheusCounter) Description() string

Description returns the metric description.

func (*PrometheusCounter) Inc

func (c *PrometheusCounter) Inc()

Inc increments the counter by 1.

func (*PrometheusCounter) Name

func (c *PrometheusCounter) Name() string

Name returns the metric name.

func (*PrometheusCounter) Tags

func (c *PrometheusCounter) Tags() Tags

Tags returns the metric tags.

func (*PrometheusCounter) Type

func (c *PrometheusCounter) Type() MetricType

Type returns the metric type.

func (*PrometheusCounter) Value

func (c *PrometheusCounter) Value() float64

Value returns the current value of the counter.

func (*PrometheusCounter) WithTags

func (c *PrometheusCounter) WithTags(tags Tags) Metric

WithTags returns a new metric with the given tags.

type PrometheusCounterBuilder

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

PrometheusCounterBuilder implements CounterBuilder using Prometheus.

func (*PrometheusCounterBuilder) Build

func (b *PrometheusCounterBuilder) Build() Counter

Build creates the counter.

func (*PrometheusCounterBuilder) Description

func (b *PrometheusCounterBuilder) Description(desc string) CounterBuilder

Description sets the counter description.

func (*PrometheusCounterBuilder) Name

Name sets the counter name.

func (*PrometheusCounterBuilder) Tag

func (b *PrometheusCounterBuilder) Tag(key, value string) CounterBuilder

Tag adds a tag to the counter.

type PrometheusExporter

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

PrometheusExporter implements MetricsExporter using Prometheus.

func NewPrometheusExporter

func NewPrometheusExporter(registry *PrometheusRegistry) *PrometheusExporter

NewPrometheusExporter creates a new PrometheusExporter.

func (*PrometheusExporter) Export

func (e *PrometheusExporter) Export(snapshot MetricsSnapshot) error

Export metrics to the backend.

func (*PrometheusExporter) Handler

func (e *PrometheusExporter) Handler() http.Handler

Handler returns an HTTP handler for exposing metrics.

func (*PrometheusExporter) Start

func (e *PrometheusExporter) Start() error

Start the exporter.

func (*PrometheusExporter) Stop

func (e *PrometheusExporter) Stop() error

Stop the exporter.

type PrometheusGauge

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

PrometheusGauge implements Gauge using Prometheus.

func (*PrometheusGauge) Add

func (g *PrometheusGauge) Add(value float64)

Add adds the given value to the gauge.

func (*PrometheusGauge) Dec

func (g *PrometheusGauge) Dec()

Dec decrements the gauge by 1.

func (*PrometheusGauge) Description

func (g *PrometheusGauge) Description() string

Description returns the metric description.

func (*PrometheusGauge) Inc

func (g *PrometheusGauge) Inc()

Inc increments the gauge by 1.

func (*PrometheusGauge) Name

func (g *PrometheusGauge) Name() string

Name returns the metric name.

func (*PrometheusGauge) Set

func (g *PrometheusGauge) Set(value float64)

Set sets the gauge to the given value.

func (*PrometheusGauge) Sub

func (g *PrometheusGauge) Sub(value float64)

Sub subtracts the given value from the gauge.

func (*PrometheusGauge) Tags

func (g *PrometheusGauge) Tags() Tags

Tags returns the metric tags.

func (*PrometheusGauge) Type

func (g *PrometheusGauge) Type() MetricType

Type returns the metric type.

func (*PrometheusGauge) Value

func (g *PrometheusGauge) Value() float64

Value returns the current value of the gauge.

func (*PrometheusGauge) WithTags

func (g *PrometheusGauge) WithTags(tags Tags) Metric

WithTags returns a new metric with the given tags.

type PrometheusGaugeBuilder

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

PrometheusGaugeBuilder implements GaugeBuilder using Prometheus.

func (*PrometheusGaugeBuilder) Build

func (b *PrometheusGaugeBuilder) Build() Gauge

Build creates the gauge.

func (*PrometheusGaugeBuilder) Description

func (b *PrometheusGaugeBuilder) Description(desc string) GaugeBuilder

Description sets the gauge description.

func (*PrometheusGaugeBuilder) Name

Name sets the gauge name.

func (*PrometheusGaugeBuilder) Tag

func (b *PrometheusGaugeBuilder) Tag(key, value string) GaugeBuilder

Tag adds a tag to the gauge.

type PrometheusHistogram

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

PrometheusHistogram implements Histogram using Prometheus.

func (*PrometheusHistogram) Buckets

func (h *PrometheusHistogram) Buckets() []float64

Buckets returns the bucket boundaries.

func (*PrometheusHistogram) Description

func (h *PrometheusHistogram) Description() string

Description returns the metric description.

func (*PrometheusHistogram) Name

func (h *PrometheusHistogram) Name() string

Name returns the metric name.

func (*PrometheusHistogram) Observe

func (h *PrometheusHistogram) Observe(value float64)

Observe adds a single observation to the histogram.

func (*PrometheusHistogram) Tags

func (h *PrometheusHistogram) Tags() Tags

Tags returns the metric tags.

func (*PrometheusHistogram) Type

func (h *PrometheusHistogram) Type() MetricType

Type returns the metric type.

func (*PrometheusHistogram) WithTags

func (h *PrometheusHistogram) WithTags(tags Tags) Metric

WithTags returns a new metric with the given tags.

type PrometheusHistogramBuilder

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

PrometheusHistogramBuilder implements HistogramBuilder using Prometheus.

func (*PrometheusHistogramBuilder) Buckets

func (b *PrometheusHistogramBuilder) Buckets(buckets []float64) HistogramBuilder

Buckets sets the bucket boundaries.

func (*PrometheusHistogramBuilder) Build

Build creates the histogram.

func (*PrometheusHistogramBuilder) Description

func (b *PrometheusHistogramBuilder) Description(desc string) HistogramBuilder

Description sets the histogram description.

func (*PrometheusHistogramBuilder) Name

Name sets the histogram name.

func (*PrometheusHistogramBuilder) Tag

Tag adds a tag to the histogram.

type PrometheusMiddleware

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

PrometheusMiddleware implements MetricsMiddleware using Prometheus.

func NewPrometheusMiddleware

func NewPrometheusMiddleware(registry *PrometheusRegistry, config MetricsMiddlewareConfig) *PrometheusMiddleware

NewPrometheusMiddleware creates a new PrometheusMiddleware.

func (*PrometheusMiddleware) Configure

Configure updates the middleware configuration.

func (*PrometheusMiddleware) Handler

func (m *PrometheusMiddleware) Handler(name string, handler http.Handler) http.Handler

Handler wraps an HTTP handler with metrics collection.

func (*PrometheusMiddleware) WithFilter

WithFilter sets the filter for the middleware.

func (*PrometheusMiddleware) WithSampler

func (m *PrometheusMiddleware) WithSampler(sampler MetricsSampler) *PrometheusMiddleware

WithSampler sets the sampler for the middleware.

type PrometheusRegistry

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

PrometheusRegistry implements MetricsRegistry using Prometheus.

func NewPrometheusRegistry

func NewPrometheusRegistry() *PrometheusRegistry

NewPrometheusRegistry creates a new PrometheusRegistry.

func (*PrometheusRegistry) Clear

func (r *PrometheusRegistry) Clear()

Clear all metrics from the registry.

func (*PrometheusRegistry) Get

func (r *PrometheusRegistry) Get(name string) (Metric, bool)

Get a metric by name.

func (*PrometheusRegistry) NewCounter

func (r *PrometheusRegistry) NewCounter() CounterBuilder

NewCounter creates a new counter builder.

func (*PrometheusRegistry) NewGauge

func (r *PrometheusRegistry) NewGauge() GaugeBuilder

NewGauge creates a new gauge builder.

func (*PrometheusRegistry) NewHistogram

func (r *PrometheusRegistry) NewHistogram() HistogramBuilder

NewHistogram creates a new histogram builder.

func (*PrometheusRegistry) NewSummary

func (r *PrometheusRegistry) NewSummary() SummaryBuilder

NewSummary creates a new summary builder.

func (*PrometheusRegistry) Register

func (r *PrometheusRegistry) Register(metric Metric) error

Register a metric with the registry.

func (*PrometheusRegistry) Snapshot

func (r *PrometheusRegistry) Snapshot() MetricsSnapshot

Snapshot returns a point-in-time snapshot of all metrics.

func (*PrometheusRegistry) Unregister

func (r *PrometheusRegistry) Unregister(name string) bool

Unregister a metric from the registry.

func (*PrometheusRegistry) WithTags

func (r *PrometheusRegistry) WithTags(tags Tags) MetricsRegistry

WithTags returns a tagged registry that adds the given tags to all metrics.

type PrometheusSnapshot

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

PrometheusSnapshot implements MetricsSnapshot.

func (*PrometheusSnapshot) Counters

func (s *PrometheusSnapshot) Counters() []Counter

Counters returns all counters in the snapshot.

func (*PrometheusSnapshot) Gauges

func (s *PrometheusSnapshot) Gauges() []Gauge

Gauges returns all gauges in the snapshot.

func (*PrometheusSnapshot) Histograms

func (s *PrometheusSnapshot) Histograms() []Histogram

Histograms returns all histograms in the snapshot.

func (*PrometheusSnapshot) Summaries

func (s *PrometheusSnapshot) Summaries() []Summary

Summaries returns all summaries in the snapshot.

type PrometheusSummary

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

PrometheusSummary implements Summary using Prometheus.

func (*PrometheusSummary) Description

func (s *PrometheusSummary) Description() string

Description returns the metric description.

func (*PrometheusSummary) Name

func (s *PrometheusSummary) Name() string

Name returns the metric name.

func (*PrometheusSummary) Objectives

func (s *PrometheusSummary) Objectives() map[float64]float64

Objectives returns the quantile objectives.

func (*PrometheusSummary) Observe

func (s *PrometheusSummary) Observe(value float64)

Observe adds a single observation to the summary.

func (*PrometheusSummary) Tags

func (s *PrometheusSummary) Tags() Tags

Tags returns the metric tags.

func (*PrometheusSummary) Type

func (s *PrometheusSummary) Type() MetricType

Type returns the metric type.

func (*PrometheusSummary) WithTags

func (s *PrometheusSummary) WithTags(tags Tags) Metric

WithTags returns a new metric with the given tags.

type PrometheusSummaryBuilder

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

PrometheusSummaryBuilder implements SummaryBuilder using Prometheus.

func (*PrometheusSummaryBuilder) AgeBuckets

func (b *PrometheusSummaryBuilder) AgeBuckets(ageBuckets int) SummaryBuilder

AgeBuckets sets the number of age buckets.

func (*PrometheusSummaryBuilder) Build

func (b *PrometheusSummaryBuilder) Build() Summary

Build creates the summary.

func (*PrometheusSummaryBuilder) Description

func (b *PrometheusSummaryBuilder) Description(desc string) SummaryBuilder

Description sets the summary description.

func (*PrometheusSummaryBuilder) MaxAge

MaxAge sets the maximum age of observations.

func (*PrometheusSummaryBuilder) Name

Name sets the summary name.

func (*PrometheusSummaryBuilder) Objectives

func (b *PrometheusSummaryBuilder) Objectives(objectives map[float64]float64) SummaryBuilder

Objectives sets the quantile objectives.

func (*PrometheusSummaryBuilder) Tag

func (b *PrometheusSummaryBuilder) Tag(key, value string) SummaryBuilder

Tag adds a tag to the summary.

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 (*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)

	// Objectives returns the quantile objectives.
	Objectives() map[float64]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.

Jump to

Keyboard shortcuts

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