histogramvec

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

Package histogramvec provides primitives for working with a vector of histograms, particularly towards building Prometheus exporters.

Example
// Create a new descriptor with a label.
desc := prometheus.NewDesc(
	"fridge_temperature_celsius",
	"The temperature of the fridge in each store",
	[]string{"store_name"},
	nil,
)

// Create a HistogramVec.
c := Config{
	BucketLimits: []float64{0, 1, 2, 3, 4, 5},
}
hv, _ := New(c)

// Define a struct to hold sensor data,
// here, the temperature of each store's refrigerator.
type StoreTemperature struct {
	StoreName   string
	Temperature float64
}

// Read data from an external system.
firstSamples := []StoreTemperature{
	{
		StoreName:   "London",
		Temperature: 2.5,
	},
	{
		StoreName:   "Frankfurt",
		Temperature: 3.2,
	},
}

// Add the samples to the HistogramVec.
for _, s := range firstSamples {
	_ = hv.Add(s.StoreName, s.Temperature)
}

// Emit each metric, such as in a Prometheus Collector.
// This will emit Histograms for both the London and Frankfurt stores.
for label, h := range hv.Histograms() {
	metric := prometheus.MustNewConstHistogram(
		desc,
		h.Count(), h.Sum(), h.Buckets(),
		label,
	)

	fmt.Println(metric)
}

// Read data again from an external system.
// For example, this would be a separate invocation of Collect
// in a Prometheus Collector.
secondSamples := []StoreTemperature{
	{
		StoreName:   "London",
		Temperature: 2.6,
	},
	// Note: Frankfurt store has shut down,
	// and does not report fridge temperatures anymore.
}

// Add the second set of sample to the HistogramVec.
for _, s := range secondSamples {
	_ = hv.Add(s.StoreName, s.Temperature)
}

// Ensure that any stores that have shut down are removed from the HistogramVec.
// This should be done on every sampling (ommitted earlier for clarity).
storeNames := []string{}
for _, s := range secondSamples {
	storeNames = append(storeNames, s.StoreName)
}
hv.Ensure(storeNames)

// Again, emit each metric. This will only emit a Histogram for the London store,
// as the Frankfurt store has been removed.
for label, h := range hv.Histograms() {
	metric := prometheus.MustNewConstHistogram(
		desc,
		h.Count(), h.Sum(), h.Buckets(),
		label,
	)

	fmt.Println(metric)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsInvalidConfig

func IsInvalidConfig(err error) bool

IsInvalidConfig asserts invalidConfigError.

Types

type Config

type Config struct {
	// BucketLimits is the upper limit of each bucket used when creating new internal Histograms.
	// See https://godoc.org/github.com/prometheus/client_golang/prometheus#HistogramOpts.
	BucketLimits []float64
}

type HistogramVec

type HistogramVec struct {
	// contains filtered or unexported fields
}

HistogramVec is a data structure suitable for holding multiple Histograms, and then providing the inputs to multiple Prometheus Histograms. See https://godoc.org/github.com/prometheus/client_golang/prometheus#MustNewConstHistogram.

func New

func New(config Config) (*HistogramVec, error)

func (*HistogramVec) Add

func (hv *HistogramVec) Add(label string, x float64) error

Add saves an entry to the Histogram with the given label, creating it internally if required.

func (*HistogramVec) Ensure

func (hv *HistogramVec) Ensure(labels []string)

Ensure removes any internal Histograms that aren't in the given slice of labels. This is useful when a label is no longer being recorded, such as in dynamic systems.

func (*HistogramVec) Histograms

func (hv *HistogramVec) Histograms() map[string]*histogram.Histogram

Histograms returns a copy of the currently managed Histograms.

Jump to

Keyboard shortcuts

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