promauto
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
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¶
- func NewCounter(opts prometheus.CounterOpts) prometheus.Counter
- func NewCounterFunc(opts prometheus.CounterOpts, function func() float64) prometheus.CounterFunc
- func NewCounterVec(opts prometheus.CounterOpts, labelNames []string) *prometheus.CounterVec
- func NewGauge(opts prometheus.GaugeOpts) prometheus.Gauge
- func NewGaugeFunc(opts prometheus.GaugeOpts, function func() float64) prometheus.GaugeFunc
- func NewGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec
- func NewHistogram(opts prometheus.HistogramOpts) prometheus.Histogram
- func NewHistogramVec(opts prometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec
- func NewSummary(opts prometheus.SummaryOpts) prometheus.Summary
- func NewSummaryVec(opts prometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec
- func NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc
- type Factory
- func (f Factory) NewCounter(opts prometheus.CounterOpts) prometheus.Counter
- func (f Factory) NewCounterFunc(opts prometheus.CounterOpts, function func() float64) prometheus.CounterFunc
- func (f Factory) NewCounterVec(opts prometheus.CounterOpts, labelNames []string) *prometheus.CounterVec
- func (f Factory) NewGauge(opts prometheus.GaugeOpts) prometheus.Gauge
- func (f Factory) NewGaugeFunc(opts prometheus.GaugeOpts, function func() float64) prometheus.GaugeFunc
- func (f Factory) NewGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec
- func (f Factory) NewHistogram(opts prometheus.HistogramOpts) prometheus.Histogram
- func (f Factory) NewHistogramVec(opts prometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec
- func (f Factory) NewSummary(opts prometheus.SummaryOpts) prometheus.Summary
- func (f Factory) NewSummaryVec(opts prometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec
- func (f Factory) NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcNewCounter¶
func NewCounter(optsprometheus.CounterOpts)prometheus.Counter
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¶
func NewGauge(optsprometheus.GaugeOpts)prometheus.Gauge
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¶
func NewHistogram(optsprometheus.HistogramOpts)prometheus.Histogram
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¶
func NewSummary(optsprometheus.SummaryOpts)prometheus.Summary
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.
funcNewUntypedFunc¶added 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¶
typeFactory¶added 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.
funcWith¶added inv0.12.1
func With(rprometheus.Registerer)Factory
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)NewCounter¶added inv0.12.1
func (fFactory) NewCounter(optsprometheus.CounterOpts)prometheus.Counter
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)NewCounterFunc¶added 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)NewCounterVec¶added 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)NewGauge¶added inv0.12.1
func (fFactory) NewGauge(optsprometheus.GaugeOpts)prometheus.Gauge
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)NewGaugeFunc¶added 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)NewGaugeVec¶added 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)NewHistogram¶added inv0.12.1
func (fFactory) NewHistogram(optsprometheus.HistogramOpts)prometheus.Histogram
NewHistogram works like the function of the same name in the prometheuspackage but it automatically registers the Histogram with the Factory'sRegisterer.
func (Factory)NewHistogramVec¶added 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)NewSummary¶added inv0.12.1
func (fFactory) NewSummary(optsprometheus.SummaryOpts)prometheus.Summary
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)NewSummaryVec¶added 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)NewUntypedFunc¶added 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.