Documentation
¶
Index ¶
- func DurationBuckets() []float64
- func NewCounter(registry prometheus.Registerer, name, help string) prometheus.Counter
- func NewCounterVec(registry prometheus.Registerer, name, help string, labels []string) *prometheus.CounterVec
- func NewGauge(registry prometheus.Registerer, name, help string) prometheus.Gauge
- func NewGaugeVec(registry prometheus.Registerer, name, help string, labels []string) *prometheus.GaugeVec
- func NewHistogram(registry prometheus.Registerer, name, help string) prometheus.Histogram
- func NewHistogramWithBuckets(registry prometheus.Registerer, name, help string, buckets []float64) prometheus.Histogram
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DurationBuckets ¶
func DurationBuckets() []float64
DurationBuckets returns histogram buckets suitable for duration metrics in seconds.
The buckets cover a range from 10ms to 10s, which is appropriate for most API and processing durations.
Buckets: [0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]
Example:
registry := prometheus.NewRegistry()
latency := metrics.NewHistogramWithBuckets(
registry,
"operation_duration_seconds",
"Operation duration in seconds",
metrics.DurationBuckets(),
)
func NewCounter ¶
func NewCounter(registry prometheus.Registerer, name, help string) prometheus.Counter
NewCounter creates and registers a counter metric.
A counter is a cumulative metric that represents a single monotonically increasing value. Use counters for values that only increase, such as the number of requests served, tasks completed, or errors.
Parameters:
- registry: The Prometheus registry to register with (use prometheus.NewRegistry())
- name: Metric name (e.g., "http_requests_total")
- help: Human-readable description of the metric
Example:
registry := prometheus.NewRegistry() requestsTotal := metrics.NewCounter(registry, "http_requests_total", "Total HTTP requests") requestsTotal.Inc()
func NewCounterVec ¶
func NewCounterVec(registry prometheus.Registerer, name, help string, labels []string) *prometheus.CounterVec
NewCounterVec creates and registers a counter vector with labels.
A counter vector is a collection of counters with the same name but different label dimensions. Use counter vectors when you need to track the same counter across different categories.
Parameters:
- registry: The Prometheus registry to register with
- name: Metric name
- help: Human-readable description
- labels: Label names (e.g., []string{"method", "status"})
Example:
registry := prometheus.NewRegistry()
httpRequests := metrics.NewCounterVec(
registry,
"http_requests_total",
"Total HTTP requests",
[]string{"method", "status"},
)
httpRequests.WithLabelValues("GET", "200").Inc()
httpRequests.WithLabelValues("POST", "201").Inc()
func NewGauge ¶
func NewGauge(registry prometheus.Registerer, name, help string) prometheus.Gauge
NewGauge creates and registers a gauge metric.
A gauge is a metric that represents a single numerical value that can arbitrarily go up and down. Use gauges for values that can increase or decrease, such as temperature, memory usage, or number of concurrent requests.
Parameters:
- registry: The Prometheus registry to register with
- name: Metric name (e.g., "concurrent_requests")
- help: Human-readable description of the metric
Example:
registry := prometheus.NewRegistry() activeConnections := metrics.NewGauge(registry, "active_connections", "Number of active connections") activeConnections.Set(42)
func NewGaugeVec ¶
func NewGaugeVec(registry prometheus.Registerer, name, help string, labels []string) *prometheus.GaugeVec
NewGaugeVec creates and registers a gauge vector with labels.
A gauge vector is a collection of gauges with the same name but different label dimensions. Use gauge vectors when you need to track the same metric across different categories.
Parameters:
- registry: The Prometheus registry to register with
- name: Metric name
- help: Human-readable description
- labels: Label names (e.g., []string{"method", "status"})
Example:
registry := prometheus.NewRegistry()
queueSize := metrics.NewGaugeVec(
registry,
"queue_size",
"Size of queue by type",
[]string{"queue_type"},
)
queueSize.WithLabelValues("high_priority").Set(10)
queueSize.WithLabelValues("low_priority").Set(50)
func NewHistogram ¶
func NewHistogram(registry prometheus.Registerer, name, help string) prometheus.Histogram
NewHistogram creates and registers a histogram metric with default buckets.
A histogram samples observations (e.g., request durations or response sizes) and counts them in configurable buckets. Use histograms for measuring distributions of values.
Default buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10]
Parameters:
- registry: The Prometheus registry to register with
- name: Metric name (e.g., "http_request_duration_seconds")
- help: Human-readable description of the metric
Example:
registry := prometheus.NewRegistry() duration := metrics.NewHistogram(registry, "request_duration_seconds", "Request duration") duration.Observe(0.5)
func NewHistogramWithBuckets ¶
func NewHistogramWithBuckets(registry prometheus.Registerer, name, help string, buckets []float64) prometheus.Histogram
NewHistogramWithBuckets creates and registers a histogram with custom buckets.
Use this when default buckets don't match your use case. For duration metrics, consider using DurationBuckets() as a starting point.
Parameters:
- registry: The Prometheus registry to register with
- name: Metric name
- help: Human-readable description
- buckets: Bucket boundaries (e.g., []float64{0.1, 0.5, 1.0, 5.0})
Example:
registry := prometheus.NewRegistry()
duration := metrics.NewHistogramWithBuckets(
registry,
"api_latency_seconds",
"API latency distribution",
metrics.DurationBuckets(),
)
duration.Observe(0.25)
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server serves Prometheus metrics over HTTP.
IMPORTANT: Server is instance-based (not global). Create one per application lifecycle to ensure metrics are garbage collected when the server stops.
The server provides a /metrics endpoint for Prometheus scraping and gracefully shuts down when the context is cancelled.
func NewServer ¶
func NewServer(addr string, registry prometheus.Gatherer) *Server
NewServer creates a new metrics server.
IMPORTANT: Pass an instance-based registry (prometheus.NewRegistry()), NOT prometheus.DefaultRegisterer. This ensures metrics are garbage collected when the server stops, which is critical for applications that reinitialize on configuration changes.
Parameters:
- addr: TCP address to listen on (e.g., ":9090" or "localhost:9090")
- registry: The Prometheus registry to serve (use prometheus.NewRegistry())
Example:
registry := prometheus.NewRegistry() // Instance-based, not global!
server := metrics.NewServer(":9090", registry)
go server.Start(ctx)
func (*Server) Start ¶
Start starts the HTTP server and blocks until the context is cancelled.
This method should typically be run in a goroutine:
go server.Start(ctx)
The server performs graceful shutdown when the context is cancelled, waiting for active connections to complete (up to a 10-second timeout).