promreg

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 14 Imported by: 13

README

promreg

promreg provides MetricFactory and MetricCreator to help creation of metrics in components and allow querying for tests

Why we need promreg

In Prometheus Golang library, there are:

Level Writer Reader Description
1 Registry Gatherer Registry of metrics at top level
2 Collector - Collector may be a metric family or a superset of families and other collectors
3 **Vec dto.MetricFamily
4 **Vec - A **Vec may be a subset of a metric family with some of labels filled, created by CurryWith
5 Metric dto.Metric

The obvious problem is that due to the split, we cannot normally read back a metric value or find a metric family in registry. Instead the Gatherer (builtin Registries are also Gatherers) can export to dto.* types and allow us to get the final result.

A second problem is related to application design, that every components would provide full metric names and fill all labels themselves without hierarchy, which is not very suitable in larger applications.

What promreg provides

MetricFactory|MetricCreator

MetricFactory|MetricCreator: a front of Prometheus metric registry, with fixed metric name prefix and labels. For example:

  • root MetricFactory: myapp_
    • sub-MetricCreator for TCP listener: listener_, port=80, tls=false
      • create metric connection_error_total{reason="auth"} => myapp_listener_connection_error_total{port="80",tls="false",reason="auth"}
    • sub-MetricCreator for TCP listener: listener_, port=8443, tls=true
      • sub-MetricCreator for /report handler 1: handler, path=/report
      • ...

Each of MetricFactory (root MetricCreator) is also a metric registry and a collector, supporting lookup of metrics.

Supported metric types are promext.RWCounter, promext.LazyRWCounter, promext.RWGauge instead of the builtin ones which cannot be read.

To find existing metric in factory, from above example it would be:

factory.LookupMetricFamily("listener_connection_error_total") // omit root prefix
Metric Listener for custom factories
server := promreg.LaunchMetricListener("0.0.0.0:8080", factory, false) // true to enable /debug/pprof
...
server.Shutdown(context.Background())

Or listener for multiple registries:

compositeGatherer := prometheus.Gatherers{
    prometheus.DefaultGatherer,
    factory1,
    factory2,
    ...
}
server := promreg.LaunchMetricListener("0.0.0.0:8080", compositeGatherer, false)
...
server.Shutdown(context.Background())
Support for metric removal/replacement

Unregistering a single metric or metric family is not possible due to possible conflicts, but if the goal is to unload or replace certain sets of metrics, we can move the metrics to different factories, e.g.:

  • MetricFactory 1 for business logic metrics
  • MetricFactory 2 for presentation layer metrics
  • default Prometheus Registry for builtin metrics and 3rd-party libs

And use a function-type Gatherer which always retrieves latest factories:

funGatherer := prometheus.GathererFunc(func() ([]*dto.MetricFamily, error) {
    return prometheus.Gatherers{
        prometheus.DefaultGatherer,
        factory1,
        factory2,
        ...
    }
})
server := promreg.LaunchMetricListener("0.0.0.0:8080", funGatherer, false)

Note when a factory is removed or replaced, all metrics remaining uncollected would simply be lost.

Documentation

Overview

Package promreg provides a front to Prometheus metric registry to help with creation of metrics in components as well as querying.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LaunchMetricListener

func LaunchMetricListener(address string, gatherer prometheus.Gatherer, enablePprof bool) *http.Server

LaunchMetricListener starts a HTTP server for Prometheus metrics and optionally /debug/pprof

If the address contains unspecified port (":0"), a random port is assigned and set to server.Addr

Types

type CompositeMetricQuerier

type CompositeMetricQuerier []MetricQuerier

CompositeMetricQuerier combines multiple MetricQuerier(s)

func (CompositeMetricQuerier) Collect

func (c CompositeMetricQuerier) Collect(output chan<- prometheus.Metric)

Collect implements prometheus.Collector's Collect function, storing metrics in the output channel

func (CompositeMetricQuerier) Describe

func (c CompositeMetricQuerier) Describe(output chan<- *prometheus.Desc)

Describe implements prometheus.Collector's Describe function, storing metric descriptions in the output channel

func (CompositeMetricQuerier) LookupMetricFamily

func (c CompositeMetricQuerier) LookupMetricFamily(name string) prometheus.Collector

LookupMetricFamily implements MetricQuerier's LookupMetricFamily function, finding a metric family (vector) by name

type MetricCreator

type MetricCreator interface {

	// AddOrGetPrefix creates a sub-creator which inherits the parent's prefix and fixed labels,
	// with more prefix and fixed labels added to all metrics created from this new sub-factory
	AddOrGetPrefix(prefix string, labelNames []string, labelValues []string) MetricCreator

	// AddOrGetCounter adds or gets a counter
	AddOrGetCounter(name string, help string, labelNames []string, labelValues []string) promext.RWCounter

	// AddOrGetCounterVec adds or gets a counter-vec with leftmost label values
	AddOrGetCounterVec(name string, help string, labelNames []string, leftmostLabelValues []string) *promext.RWCounterVec

	// AddOrGetGauge adds or gets a gauge
	//
	// Gauges must be updated by Add/Sub not Set, because there could be multiple updaters
	AddOrGetGauge(name string, help string, labelNames []string, labelValues []string) promext.RWGauge

	// AddOrGetGaugeVec adds or gets a gauge-vec with leftmost label values
	//
	// Gauges must be updated by Add/Sub not Set, because there could be multiple updaters
	AddOrGetGaugeVec(name string, help string, labelNames []string, leftmostLabelValues []string) *promext.RWGaugeVec

	// AddOrGetLazyCounter adds or gets a lazy counter
	//
	// Lazy counters are not listed in output if the value is zero
	AddOrGetLazyCounter(name string, help string, labelNames []string, labelValues []string) promext.LazyRWCounter

	// AddOrGetLazyCounterVec adds or gets a lazy counter-vec with leftmost label values
	//
	// Lazy counters are not listed in output if the value is zero
	AddOrGetLazyCounterVec(name string, help string, labelNames []string, leftmostLabelValues []string) *promext.LazyRWCounterVec

	fmt.Stringer
}

MetricCreator is a front to facilitate creation of Prometheus metrics

Each MetricCreator carries a predefined prefix, labels and label values that are added to every metrics created from it.

A MetricCreator can create child creators with additional prefix, labels and label values. The parent's prefix, labels or label values can never be removed or overridden.

The MetricCreator doesn't provide any lookup functionality even though it's certainly doable, because metric families created from a sub-creator could contain labels and values from siblings, e.g.:

- sub-creator TCP: metric connection_total[prot=tcp]

- sub-creator UDP: metric connection_total[prot=udp]

The AddOrGet* in both sub-facrories and any lookup function if implemented would report the same metric family "connection_total", which contains both metrics regardless from which sub-creator the call is made. This would cause duplications and inconsistency in resulting data if they're combined.

type MetricFactory

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

MetricFactory is the root implementation of MetricCreator, a front to facilitate creation of Prometheus metrics

MetricFactory is also a promethues.Collector and a prometheus.Gatherer itself

Different MetricFactory(s) MUST NOT contain the same metric families.

func NewMetricFactory

func NewMetricFactory(prefix string, labelNames []string, labelValues []string) *MetricFactory

NewMetricFactory creates a factory with prefix for metrics names and fixed labels for all metrics created from this new factory

func (*MetricFactory) AddOrGetCounter

func (creator *MetricFactory) AddOrGetCounter(name string, help string, labelNames []string, labelValues []string) promext.RWCounter

AddOrGetCounter adds or gets a counter

func (*MetricFactory) AddOrGetCounterVec

func (creator *MetricFactory) AddOrGetCounterVec(name string, help string, labelNames []string, leftmostLabelValues []string) *promext.RWCounterVec

AddOrGetCounterVec adds or gets a counter-vec with leftmost label values

func (*MetricFactory) AddOrGetGauge

func (creator *MetricFactory) AddOrGetGauge(name string, help string, labelNames []string, labelValues []string) promext.RWGauge

AddOrGetGauge adds or gets a gauge

Gauges must be updated by Add/Sub not Set, because there could be multiple updaters

func (*MetricFactory) AddOrGetGaugeVec

func (creator *MetricFactory) AddOrGetGaugeVec(name string, help string, labelNames []string, leftmostLabelValues []string) *promext.RWGaugeVec

AddOrGetGaugeVec adds or gets a gauge-vec with leftmost label values

Gauges must be updated by Add/Sub not Set, because there could be multiple updaters

func (*MetricFactory) AddOrGetLazyCounter

func (creator *MetricFactory) AddOrGetLazyCounter(name string, help string, labelNames []string, labelValues []string) promext.LazyRWCounter

AddOrGetCounter adds or gets a counter

func (*MetricFactory) AddOrGetLazyCounterVec

func (creator *MetricFactory) AddOrGetLazyCounterVec(name string, help string, labelNames []string, leftmostLabelValues []string) *promext.LazyRWCounterVec

AddOrGetCounterVec adds or gets a counter-vec with leftmost label values

func (*MetricFactory) AddOrGetPrefix

func (creator *MetricFactory) AddOrGetPrefix(prefix string, labelNames []string, labelValues []string) MetricCreator

AddOrGetPrefix creates a sub-creator which inherits the parent's prefix and fixed labels, with more prefix and fixed labels added to all metrics created from this new sub-creator

func (*MetricFactory) Collect

func (factory *MetricFactory) Collect(output chan<- prometheus.Metric)

Collect implements prometheus.Collector's Collect function, storing metrics in the output channel

func (*MetricFactory) Describe

func (factory *MetricFactory) Describe(output chan<- *prometheus.Desc)

Describe implements prometheus.Collector's Describe function, storing metric descriptions in the output channel

func (*MetricFactory) Gather

func (factory *MetricFactory) Gather() ([]*dto.MetricFamily, error)

Gather implements prometheus.Gatherer's Gather function, collecting all metric families

func (*MetricFactory) LookupMetricFamily

func (factory *MetricFactory) LookupMetricFamily(name string) prometheus.Collector

LookupMetricFamily implements MetricQuerier's LookupMetricFamily function, finding a metric family (vector) by its name without this factory's prefix

func (*MetricFactory) String

func (creator *MetricFactory) String() string

String implements fmt.Stringer's String function

type MetricQuerier

type MetricQuerier interface {
	prometheus.Collector

	// LookupMetricFamily looks up a metric family (vector) by its name
	//
	// The name is not a full name but a local name without prefix of this querier itself
	LookupMetricFamily(name string) prometheus.Collector
}

MetricQuerier is prometheus metric Collector plus lookup functions

Jump to

Keyboard shortcuts

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