Documentation
¶
Overview ¶
Package metrics provides utilities for working with Prometheus metrics.
It includes a name builder for creating valid Prometheus metric names from arbitrary strings, and a pusher for sending metrics to a Prometheus Pushgateway.
Name Builder ¶
The BuildName function creates valid Prometheus metric names by joining strings with underscores, converting to lowercase, and replacing illegal characters:
name := metrics.BuildName("my-service", "request_count")
// Result: "my_service_request_count"
name = name.Add("total")
// Result: "my_service_request_count_total"
Metrics Pusher ¶
The Pusher interface provides a fluent API for pushing metrics to a Prometheus Pushgateway:
func pushMetrics(ctx context.Context) error {
registry := prometheus.NewRegistry()
// ... register your metrics ...
pusher := metrics.NewPusher(url, jobName).
Gatherer(registry).
Push(ctx)
return pusher.Push(ctx)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Name ¶
type Name string
Name represents a valid Prometheus metric name.
func BuildName ¶
BuildName creates a valid Prometheus metric name from the given strings. It joins the strings with underscores, converts to lowercase, replaces leading numbers and illegal characters with underscores, and collapses multiple consecutive underscores into one.
type Pusher ¶
type Pusher interface {
// Push pushes all registered metrics to the Pushgateway.
Push(ctx context.Context) error
// Gatherer sets the Gatherer to use for collecting metrics and returns the Pusher
// for method chaining.
Gatherer(gatherer prometheus.Gatherer) Pusher
// Collector adds a Collector to the Pusher and returns the Pusher for method chaining.
Collector(collector prometheus.Collector) Pusher
// Client sets a custom HTTP client for the Pusher and returns the Pusher for
// method chaining.
Client(httpClient push.HTTPDoer) Pusher
}
Pusher pushes metrics to a Prometheus Pushgateway using a fluent API pattern. Configure the pusher by chaining Gatherer(), Collector(), or Client() calls before invoking Push() to send the metrics.
Example:
pusher := metrics.NewPusher(url, jobName).
Gatherer(registry).
Collector(myCollector).
Client(customHTTPClient)
if err := pusher.Push(ctx); err != nil {
return err
}