Documentation
¶
Overview ¶
Package metrics provides a hand-rolled Prometheus text-format exposition library. It requires only the Go standard library.
Both Prometheus text format (0.0.4) and OpenMetrics text format (1.0.0) are supported. Use Handler() for Prometheus format, OpenMetricsHandler() for OpenMetrics, or NegotiateHandler() for automatic content negotiation based on the Accept header.
Unsupported by design (SKIP list):
- Summary metric type: Prometheus best practices recommend histograms
- Exemplars (OpenMetrics): niche; requires tracing integration
- Push / remote-write: all consumers are pull-based
- Protobuf exposition format: text format is default in Prometheus 3.0
- Native histograms (exponential buckets): requires protobuf format
- Unregister / dynamic metric lifecycle: all consumers have static metric sets
- Float64 counter: integer counters are sufficient
- Gzip response compression: use standard HTTP middleware
- Gauge.SetToCurrentTime(): trivial one-liner
Example ¶
package main
import (
"net/http"
"github.com/cplieger/metrics"
)
func main() {
r := metrics.NewRegistry("myapp")
reqs := metrics.NewLabeledCounter("myapp_http_requests_total", "Total HTTP requests", []string{"method", "status"})
dur := metrics.NewHistogram("myapp_http_duration_seconds", "Request latency", metrics.WithBuckets([]float64{0.01, 0.05, 0.1, 0.5, 1, 5}))
r.RegisterLabeledCounter(reqs)
r.RegisterHistogram(dur)
reqs.Inc("GET", "200")
timer := metrics.NewTimer(dur)
_ = timer
timer.ObserveDuration()
http.Handle("/metrics", r.Handler())
}
Output:
Index ¶
- Constants
- Variables
- func FormatBound(v float64) string
- func WriteCounter(b *strings.Builder, c *Counter)
- func WriteGauge(b *strings.Builder, g *Gauge)
- func WriteHistogram(b *strings.Builder, h *Histogram)
- func WriteLabeledCounter(b *strings.Builder, lc *LabeledCounter)
- func WriteLabeledGauge(b *strings.Builder, lg *LabeledGauge)
- func WriteLabeledHistogram(b *strings.Builder, lh *LabeledHistogram)
- func WriteProcessMetrics(b *strings.Builder, startTime time.Time)
- type Counter
- type Gauge
- type Histogram
- type LabeledCounter
- type LabeledGauge
- type LabeledHistogram
- type Option
- type Registry
- func (r *Registry) Handler() http.HandlerFunc
- func (r *Registry) NegotiateHandler() http.HandlerFunc
- func (r *Registry) OpenMetricsHandler() http.HandlerFunc
- func (r *Registry) RegisterCounter(c *Counter)
- func (r *Registry) RegisterGauge(g *Gauge)
- func (r *Registry) RegisterHistogram(h *Histogram)
- func (r *Registry) RegisterLabeledCounter(lc *LabeledCounter)
- func (r *Registry) RegisterLabeledGauge(lg *LabeledGauge)
- func (r *Registry) RegisterLabeledHistogram(lh *LabeledHistogram)
- type Timer
Examples ¶
Constants ¶
const OpenMetricsContentType = "application/openmetrics-text; version=1.0.0; charset=utf-8"
OpenMetricsContentType is the content type per the OpenMetrics specification.
Variables ¶
var DefaultBuckets = []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0}
DefaultBuckets are the default histogram bucket boundaries (HTTP latency).
Functions ¶
func FormatBound ¶
FormatBound formats a bucket boundary for Prometheus output.
func WriteCounter ¶
WriteCounter writes a counter in Prometheus text format.
func WriteGauge ¶
WriteGauge writes a gauge in Prometheus text format.
func WriteHistogram ¶
WriteHistogram writes a histogram in Prometheus text format.
func WriteLabeledCounter ¶
func WriteLabeledCounter(b *strings.Builder, lc *LabeledCounter)
WriteLabeledCounter writes a labeled counter in Prometheus text format.
func WriteLabeledGauge ¶
func WriteLabeledGauge(b *strings.Builder, lg *LabeledGauge)
WriteLabeledGauge writes a labeled gauge in Prometheus text format.
func WriteLabeledHistogram ¶
func WriteLabeledHistogram(b *strings.Builder, lh *LabeledHistogram)
WriteLabeledHistogram writes all child histograms in Prometheus text format.
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter is a monotonically increasing counter.
type Gauge ¶
type Gauge struct {
// contains filtered or unexported fields
}
Gauge is a value that can go up and down (float64).
type Histogram ¶
type Histogram struct {
// contains filtered or unexported fields
}
Histogram tracks a distribution using cumulative buckets and atomic CAS for sum.
func NewHistogram ¶
NewHistogram creates a histogram with the given name and help text. By default it uses DefaultBuckets; use WithBuckets to override.
type LabeledCounter ¶
type LabeledCounter struct {
// contains filtered or unexported fields
}
LabeledCounter tracks counts per label combination.
func NewLabeledCounter ¶
func NewLabeledCounter(name, help string, labels []string) *LabeledCounter
NewLabeledCounter creates a labeled counter with the given label names.
func (*LabeledCounter) Inc ¶
func (lc *LabeledCounter) Inc(labelVals ...string)
Inc increments the counter for the given label values.
type LabeledGauge ¶
type LabeledGauge struct {
// contains filtered or unexported fields
}
LabeledGauge tracks gauges per label combination.
func NewLabeledGauge ¶
func NewLabeledGauge(name, help string, labels []string) *LabeledGauge
NewLabeledGauge creates a labeled gauge.
func (*LabeledGauge) Delete ¶
func (lg *LabeledGauge) Delete(labelVals ...string)
Delete removes a single label combination from the gauge. It panics if the number of label values does not match the label count.
func (*LabeledGauge) Reset ¶
func (lg *LabeledGauge) Reset()
Reset removes all label combinations from the gauge.
func (*LabeledGauge) Set ¶
func (lg *LabeledGauge) Set(v float64, labelVals ...string)
Set sets the gauge for the given label values.
type LabeledHistogram ¶
type LabeledHistogram struct {
// contains filtered or unexported fields
}
LabeledHistogram tracks histograms per label combination.
func NewLabeledHistogram ¶
func NewLabeledHistogram(name, help string, labels []string, opts ...Option) *LabeledHistogram
NewLabeledHistogram creates a labeled histogram with the given name, help, and label names. By default it uses DefaultBuckets; use WithBuckets to override.
func (*LabeledHistogram) Observe ¶
func (lh *LabeledHistogram) Observe(seconds float64, labelVals ...string)
Observe records a value for the given label values.
type Option ¶
type Option func(*histogramCfg)
Option configures optional histogram parameters.
func WithBuckets ¶
WithBuckets returns an Option that sets custom bucket boundaries for a histogram.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds a collection of metrics to be served.
func NewRegistry ¶
NewRegistry creates a new metrics registry.
func (*Registry) Handler ¶
func (r *Registry) Handler() http.HandlerFunc
Handler returns an HTTP handler serving Prometheus text format.
func (*Registry) NegotiateHandler ¶
func (r *Registry) NegotiateHandler() http.HandlerFunc
NegotiateHandler returns an HTTP handler that performs content negotiation. If the client sends an Accept header preferring OpenMetrics, it responds in OpenMetrics text format; otherwise it falls back to Prometheus text format 0.0.4.
func (*Registry) OpenMetricsHandler ¶
func (r *Registry) OpenMetricsHandler() http.HandlerFunc
OpenMetricsHandler returns an HTTP handler that always serves OpenMetrics text format.
func (*Registry) RegisterCounter ¶
RegisterCounter adds a counter to the registry.
func (*Registry) RegisterGauge ¶
RegisterGauge adds a gauge to the registry.
func (*Registry) RegisterHistogram ¶
RegisterHistogram adds a histogram to the registry.
func (*Registry) RegisterLabeledCounter ¶
func (r *Registry) RegisterLabeledCounter(lc *LabeledCounter)
RegisterLabeledCounter adds a labeled counter to the registry.
func (*Registry) RegisterLabeledGauge ¶
func (r *Registry) RegisterLabeledGauge(lg *LabeledGauge)
RegisterLabeledGauge adds a labeled gauge to the registry.
func (*Registry) RegisterLabeledHistogram ¶
func (r *Registry) RegisterLabeledHistogram(lh *LabeledHistogram)
RegisterLabeledHistogram adds a labeled histogram to the registry.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer measures elapsed time and reports to a Histogram.
func (*Timer) ObserveDuration ¶
ObserveDuration records the elapsed time since the timer was created.