Go library for working with Prometheus metrics.
- Name Builder: Create valid Prometheus metric names from arbitrary strings
- Pusher: Push metrics to Prometheus Pushgateway
go get github.com/bborbe/metricsimport "github.com/bborbe/metrics"
// Create a valid Prometheus metric name
name := metrics.BuildName("my-service", "request_count")
// Result: "my_service_request_count"
// Add to existing name
name = name.Add("total")
// Result: "my_service_request_count_total"The BuildName function:
- Converts to lowercase
- Replaces illegal characters with underscores
- Handles leading numbers
- Collapses multiple underscores
import (
"context"
"github.com/bborbe/metrics"
"github.com/prometheus/client_golang/prometheus"
)
func pushMetrics(ctx context.Context) error {
// Create a registry and register your metrics
prometheusRegistry := prometheus.NewRegistry()
// Create and configure a pusher using the fluent API
pusher := metrics.NewPusher(
"http://monitoring-pushgateway:9091",
metrics.BuildName("kafka", "backup", "my_topic"),
).Gatherer(prometheusRegistry)
// Push metrics to the gateway (using context from caller)
if err := pusher.Push(ctx); err != nil {
return err
}
return nil
}The pusher supports a fluent API for configuration:
pusher := metrics.NewPusher(url, jobName).
Gatherer(registry). // Use a custom registry
Collector(myCollector). // Add a specific collector
Client(customHTTPClient) // Use a custom HTTP client