metrics

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2025 License: BSD-2-Clause Imports: 5 Imported by: 0

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

go get github.com/bborbe/metrics

Usage

Building Metric Names
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
import (
    "context"
    "github.com/bborbe/metrics"
    "github.com/prometheus/client_golang/prometheus"
)

// 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
if err := pusher.Push(context.Background()); err != nil {
    // handle error
}

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

Documentation

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

func BuildName(names ...string) Name

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.

func (Name) Add

func (n Name) Add(name string) Name

Add appends the given name to the current Name, creating a new valid Prometheus metric name.

func (Name) String

func (n Name) String() string

String returns the string representation of the Name.

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.
	Gatherer(gatherer prometheus.Gatherer) Pusher
	// Collector adds a Collector to the Pusher.
	Collector(collector prometheus.Collector) Pusher
	// Client sets a custom HTTP client for the Pusher.
	Client(httpClient push.HTTPDoer) Pusher
}

Pusher pushes metrics to a Prometheus Pushgateway.

func NewPusher

func NewPusher(
	url string,
	name Name,
) Pusher

NewPusher creates a new Pusher that sends metrics to the specified Pushgateway URL with the given job name. Use the fluent API methods to configure collectors, gatherers, or a custom HTTP client before calling Push.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL