https://github.com/bborbe/metrics
https://github.com/bborbe/metrics
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bborbe/metrics
- Owner: bborbe
- License: bsd-2-clause
- Created: 2025-10-02T08:34:34.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-12-05T22:34:59.000Z (6 months ago)
- Last Synced: 2025-12-09T12:31:32.993Z (6 months ago)
- Language: Go
- Size: 176 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Metrics
Go library for working with Prometheus metrics.
## Features
- **Name Builder**: Create valid Prometheus metric names from arbitrary strings
- **Pusher**: Push metrics to Prometheus Pushgateway
## Installation
```bash
go get github.com/bborbe/metrics
```
## Usage
### Building Metric Names
```go
import "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
### Pushing Metrics
```go
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:
```go
pusher := metrics.NewPusher(url, jobName).
Gatherer(registry). // Use a custom registry
Collector(myCollector). // Add a specific collector
Client(customHTTPClient) // Use a custom HTTP client
```