Movatterモバイル変換


[0]ホーム

URL:


promauto

package
v1.23.2Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2025 License:Apache-2.0Imports:1Imported by:8,243

Details

Repository

github.com/prometheus/client_golang

Links

Documentation

Overview

Package promauto provides alternative constructors for the fundamentalPrometheus metric types and their …Vec and …Func variants. The difference totheir counterparts in the prometheus package is that the promautoconstructors register the Collectors with a registry before returning them.There are two sets of constructors. The constructors in the first set aretop-level functions, while the constructors in the other set are methods ofthe Factory type. The top-level functions return Collectors registered withthe global registry (prometheus.DefaultRegisterer), while the methods returnCollectors registered with the registry the Factory was constructed with. Allconstructors panic if the registration fails.

The following example is a complete program to create a histogram of normallydistributed random numbers from the math/rand package:

package mainimport ("math/rand""net/http""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/promauto""github.com/prometheus/client_golang/prometheus/promhttp")var histogram = promauto.NewHistogram(prometheus.HistogramOpts{Name:    "random_numbers",Help:    "A histogram of normally distributed random numbers.",Buckets: prometheus.LinearBuckets(-3, .1, 61),})func Random() {for {histogram.Observe(rand.NormFloat64())}}func main() {go Random()http.Handle("/metrics", promhttp.Handler())http.ListenAndServe(":1971", nil)}

Prometheus's version of a minimal hello-world program:

package mainimport ("fmt""net/http""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/promauto""github.com/prometheus/client_golang/prometheus/promhttp")func main() {http.Handle("/", promhttp.InstrumentHandlerCounter(promauto.NewCounterVec(prometheus.CounterOpts{Name: "hello_requests_total",Help: "Total number of hello-world requests by HTTP code.",},[]string{"code"},),http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w, "Hello, world!")}),))http.Handle("/metrics", promhttp.Handler())http.ListenAndServe(":1971", nil)}

A Factory is created with the With(prometheus.Registerer) function, whichenables two usage patterns. With(prometheus.Registerer) can be called once perline:

var (reg           = prometheus.NewRegistry()randomNumbers = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{Name:    "random_numbers",Help:    "A histogram of normally distributed random numbers.",Buckets: prometheus.LinearBuckets(-3, .1, 61),})requestCount = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{Name: "http_requests_total",Help: "Total number of HTTP requests by status code and method.",},[]string{"code", "method"},))

Or it can be used to create a Factory once to be used multiple times:

var (reg           = prometheus.NewRegistry()factory       = promauto.With(reg)randomNumbers = factory.NewHistogram(prometheus.HistogramOpts{Name:    "random_numbers",Help:    "A histogram of normally distributed random numbers.",Buckets: prometheus.LinearBuckets(-3, .1, 61),})requestCount = factory.NewCounterVec(prometheus.CounterOpts{Name: "http_requests_total",Help: "Total number of HTTP requests by status code and method.",},[]string{"code", "method"},))

This appears very handy. So why are these constructors locked away in aseparate package?

The main problem is that registration may fail, e.g. if a metric inconsistentwith or equal to the newly to be registered one is already registered.Therefore, the Register method in the prometheus.Registerer interface returnsan error, and the same is the case for the top-level prometheus.Registerfunction that registers with the global registry. The prometheus package alsoprovides MustRegister versions for both. They panic if the registrationfails, and they clearly call this out by using the Must… idiom. Panicking isproblematic in this case because it doesn't just happen on input provided bythe caller that is invalid on its own. Things are a bit more subtle here:Metric creation and registration tend to be spread widely over thecodebase. It can easily happen that an incompatible metric is added to anunrelated part of the code, and suddenly code that used to work perfectlyfine starts to panic (provided that the registration of the newly addedmetric happens before the registration of the previously existingmetric). This may come as an even bigger surprise with the global registry,where simply importing another package can trigger a panic (if the newlyimported package registers metrics in its init function). At least, in theprometheus package, creation of metrics and other collectors is separate fromregistration. You first create the metric, and then you decide explicitly ifyou want to register it with a local or the global registry, and if you wantto handle the error or risk a panic. With the constructors in the promautopackage, registration is automatic, and if it fails, it will alwayspanic. Furthermore, the constructors will often be called in the var sectionof a file, which means that panicking will happen as a side effect of merelyimporting a package.

A separate package allows conservative users to entirely ignore it. Andwhoever wants to use it will do so explicitly, with an opportunity to readthis warning.

Enjoy promauto responsibly!

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcNewCounter

NewCounter works like the function of the same name in the prometheus packagebut it automatically registers the Counter with theprometheus.DefaultRegisterer. If the registration fails, NewCounter panics.

funcNewCounterFunc

func NewCounterFunc(optsprometheus.CounterOpts, function func()float64)prometheus.CounterFunc

NewCounterFunc works like the function of the same name in the prometheuspackage but it automatically registers the CounterFunc with theprometheus.DefaultRegisterer. If the registration fails, NewCounterFuncpanics.

funcNewCounterVec

func NewCounterVec(optsprometheus.CounterOpts, labelNames []string) *prometheus.CounterVec

NewCounterVec works like the function of the same name in the prometheuspackage but it automatically registers the CounterVec with theprometheus.DefaultRegisterer. If the registration fails, NewCounterVecpanics.

funcNewGauge

NewGauge works like the function of the same name in the prometheus packagebut it automatically registers the Gauge with theprometheus.DefaultRegisterer. If the registration fails, NewGauge panics.

funcNewGaugeFunc

func NewGaugeFunc(optsprometheus.GaugeOpts, function func()float64)prometheus.GaugeFunc

NewGaugeFunc works like the function of the same name in the prometheuspackage but it automatically registers the GaugeFunc with theprometheus.DefaultRegisterer. If the registration fails, NewGaugeFunc panics.

funcNewGaugeVec

func NewGaugeVec(optsprometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec

NewGaugeVec works like the function of the same name in the prometheuspackage but it automatically registers the GaugeVec with theprometheus.DefaultRegisterer. If the registration fails, NewGaugeVec panics.

funcNewHistogram

NewHistogram works like the function of the same name in the prometheuspackage but it automatically registers the Histogram with theprometheus.DefaultRegisterer. If the registration fails, NewHistogram panics.

funcNewHistogramVec

func NewHistogramVec(optsprometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec

NewHistogramVec works like the function of the same name in the prometheuspackage but it automatically registers the HistogramVec with theprometheus.DefaultRegisterer. If the registration fails, NewHistogramVecpanics.

funcNewSummary

NewSummary works like the function of the same name in the prometheus packagebut it automatically registers the Summary with theprometheus.DefaultRegisterer. If the registration fails, NewSummary panics.

funcNewSummaryVec

func NewSummaryVec(optsprometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec

NewSummaryVec works like the function of the same name in the prometheuspackage but it automatically registers the SummaryVec with theprometheus.DefaultRegisterer. If the registration fails, NewSummaryVecpanics.

funcNewUntypedFuncadded inv0.12.1

func NewUntypedFunc(optsprometheus.UntypedOpts, function func()float64)prometheus.UntypedFunc

NewUntypedFunc works like the function of the same name in the prometheuspackage but it automatically registers the UntypedFunc with theprometheus.DefaultRegisterer. If the registration fails, NewUntypedFuncpanics.

Types

typeFactoryadded inv0.12.1

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

Factory provides factory methods to create Collectors that are automaticallyregistered with a Registerer. Create a Factory with the With function,providing a Registerer to auto-register created Collectors with. The zerovalue of a Factory creates Collectors that are not registered with anyRegisterer. All methods of the Factory panic if the registration fails.

funcWithadded inv0.12.1

With creates a Factory using the provided Registerer for registration of thecreated Collectors. If the provided Registerer is nil, the returned Factorycreates Collectors that are not registered with any Registerer.

func (Factory)NewCounteradded inv0.12.1

NewCounter works like the function of the same name in the prometheus packagebut it automatically registers the Counter with the Factory's Registerer.

func (Factory)NewCounterFuncadded inv0.12.1

func (fFactory) NewCounterFunc(optsprometheus.CounterOpts, function func()float64)prometheus.CounterFunc

NewCounterFunc works like the function of the same name in the prometheuspackage but it automatically registers the CounterFunc with the Factory'sRegisterer.

func (Factory)NewCounterVecadded inv0.12.1

func (fFactory) NewCounterVec(optsprometheus.CounterOpts, labelNames []string) *prometheus.CounterVec

NewCounterVec works like the function of the same name in the prometheuspackage but it automatically registers the CounterVec with the Factory'sRegisterer.

func (Factory)NewGaugeadded inv0.12.1

NewGauge works like the function of the same name in the prometheus packagebut it automatically registers the Gauge with the Factory's Registerer.

func (Factory)NewGaugeFuncadded inv0.12.1

func (fFactory) NewGaugeFunc(optsprometheus.GaugeOpts, function func()float64)prometheus.GaugeFunc

NewGaugeFunc works like the function of the same name in the prometheuspackage but it automatically registers the GaugeFunc with the Factory'sRegisterer.

func (Factory)NewGaugeVecadded inv0.12.1

func (fFactory) NewGaugeVec(optsprometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec

NewGaugeVec works like the function of the same name in the prometheuspackage but it automatically registers the GaugeVec with the Factory'sRegisterer.

func (Factory)NewHistogramadded inv0.12.1

NewHistogram works like the function of the same name in the prometheuspackage but it automatically registers the Histogram with the Factory'sRegisterer.

func (Factory)NewHistogramVecadded inv0.12.1

func (fFactory) NewHistogramVec(optsprometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec

NewHistogramVec works like the function of the same name in the prometheuspackage but it automatically registers the HistogramVec with the Factory'sRegisterer.

func (Factory)NewSummaryadded inv0.12.1

NewSummary works like the function of the same name in the prometheus packagebut it automatically registers the Summary with the Factory's Registerer.

func (Factory)NewSummaryVecadded inv0.12.1

func (fFactory) NewSummaryVec(optsprometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec

NewSummaryVec works like the function of the same name in the prometheuspackage but it automatically registers the SummaryVec with the Factory'sRegisterer.

func (Factory)NewUntypedFuncadded inv0.12.1

func (fFactory) NewUntypedFunc(optsprometheus.UntypedOpts, function func()float64)prometheus.UntypedFunc

NewUntypedFunc works like the function of the same name in the prometheuspackage but it automatically registers the UntypedFunc with the Factory'sRegisterer.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp