| 1 |
// Copyright 2018 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package promauto provides alternative constructors for the fundamental |
| 15 |
// Prometheus metric types and their …Vec and …Func variants. The difference to |
| 16 |
// their counterparts in the prometheus package is that the promauto |
| 17 |
// constructors register the Collectors with a registry before returning them. |
| 18 |
// There are two sets of constructors. The constructors in the first set are |
| 19 |
// top-level functions, while the constructors in the other set are methods of |
| 20 |
// the Factory type. The top-level functions return Collectors registered with |
| 21 |
// the global registry (prometheus.DefaultRegisterer), while the methods return |
| 22 |
// Collectors registered with the registry the Factory was constructed with. All |
| 23 |
// constructors panic if the registration fails. |
| 24 |
// |
| 25 |
// The following example is a complete program to create a histogram of normally |
| 26 |
// distributed random numbers from the math/rand package: |
| 27 |
// |
| 28 |
// package main |
| 29 |
// |
| 30 |
// import ( |
| 31 |
// "math/rand" |
| 32 |
// "net/http" |
| 33 |
// |
| 34 |
// "github.com/prometheus/client_golang/prometheus" |
| 35 |
// "github.com/prometheus/client_golang/prometheus/promauto" |
| 36 |
// "github.com/prometheus/client_golang/prometheus/promhttp" |
| 37 |
// ) |
| 38 |
// |
| 39 |
// var histogram = promauto.NewHistogram(prometheus.HistogramOpts{ |
| 40 |
// Name: "random_numbers", |
| 41 |
// Help: "A histogram of normally distributed random numbers.", |
| 42 |
// Buckets: prometheus.LinearBuckets(-3, .1, 61), |
| 43 |
// }) |
| 44 |
// |
| 45 |
// func Random() { |
| 46 |
// for { |
| 47 |
// histogram.Observe(rand.NormFloat64()) |
| 48 |
// } |
| 49 |
// } |
| 50 |
// |
| 51 |
// func main() { |
| 52 |
// go Random() |
| 53 |
// http.Handle("/metrics", promhttp.Handler()) |
| 54 |
// http.ListenAndServe(":1971", nil) |
| 55 |
// } |
| 56 |
// |
| 57 |
// Prometheus's version of a minimal hello-world program: |
| 58 |
// |
| 59 |
// package main |
| 60 |
// |
| 61 |
// import ( |
| 62 |
// "fmt" |
| 63 |
// "net/http" |
| 64 |
// |
| 65 |
// "github.com/prometheus/client_golang/prometheus" |
| 66 |
// "github.com/prometheus/client_golang/prometheus/promauto" |
| 67 |
// "github.com/prometheus/client_golang/prometheus/promhttp" |
| 68 |
// ) |
| 69 |
// |
| 70 |
// func main() { |
| 71 |
// http.Handle("/", promhttp.InstrumentHandlerCounter( |
| 72 |
// promauto.NewCounterVec( |
| 73 |
// prometheus.CounterOpts{ |
| 74 |
// Name: "hello_requests_total", |
| 75 |
// Help: "Total number of hello-world requests by HTTP code.", |
| 76 |
// }, |
| 77 |
// []string{"code"}, |
| 78 |
// ), |
| 79 |
// http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 80 |
// fmt.Fprint(w, "Hello, world!") |
| 81 |
// }), |
| 82 |
// )) |
| 83 |
// http.Handle("/metrics", promhttp.Handler()) |
| 84 |
// http.ListenAndServe(":1971", nil) |
| 85 |
// } |
| 86 |
// |
| 87 |
// A Factory is created with the With(prometheus.Registerer) function, which |
| 88 |
// enables two usage patterns. With(prometheus.Registerer) can be called once per |
| 89 |
// line: |
| 90 |
// |
| 91 |
// var ( |
| 92 |
// reg = prometheus.NewRegistry() |
| 93 |
// randomNumbers = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ |
| 94 |
// Name: "random_numbers", |
| 95 |
// Help: "A histogram of normally distributed random numbers.", |
| 96 |
// Buckets: prometheus.LinearBuckets(-3, .1, 61), |
| 97 |
// }) |
| 98 |
// requestCount = promauto.With(reg).NewCounterVec( |
| 99 |
// prometheus.CounterOpts{ |
| 100 |
// Name: "http_requests_total", |
| 101 |
// Help: "Total number of HTTP requests by status code and method.", |
| 102 |
// }, |
| 103 |
// []string{"code", "method"}, |
| 104 |
// ) |
| 105 |
// ) |
| 106 |
// |
| 107 |
// Or it can be used to create a Factory once to be used multiple times: |
| 108 |
// |
| 109 |
// var ( |
| 110 |
// reg = prometheus.NewRegistry() |
| 111 |
// factory = promauto.With(reg) |
| 112 |
// randomNumbers = factory.NewHistogram(prometheus.HistogramOpts{ |
| 113 |
// Name: "random_numbers", |
| 114 |
// Help: "A histogram of normally distributed random numbers.", |
| 115 |
// Buckets: prometheus.LinearBuckets(-3, .1, 61), |
| 116 |
// }) |
| 117 |
// requestCount = factory.NewCounterVec( |
| 118 |
// prometheus.CounterOpts{ |
| 119 |
// Name: "http_requests_total", |
| 120 |
// Help: "Total number of HTTP requests by status code and method.", |
| 121 |
// }, |
| 122 |
// []string{"code", "method"}, |
| 123 |
// ) |
| 124 |
// ) |
| 125 |
// |
| 126 |
// This appears very handy. So why are these constructors locked away in a |
| 127 |
// separate package? |
| 128 |
// |
| 129 |
// The main problem is that registration may fail, e.g. if a metric inconsistent |
| 130 |
// with or equal to the newly to be registered one is already registered. |
| 131 |
// Therefore, the Register method in the prometheus.Registerer interface returns |
| 132 |
// an error, and the same is the case for the top-level prometheus.Register |
| 133 |
// function that registers with the global registry. The prometheus package also |
| 134 |
// provides MustRegister versions for both. They panic if the registration |
| 135 |
// fails, and they clearly call this out by using the Must… idiom. Panicking is |
| 136 |
// problematic in this case because it doesn't just happen on input provided by |
| 137 |
// the caller that is invalid on its own. Things are a bit more subtle here: |
| 138 |
// Metric creation and registration tend to be spread widely over the |
| 139 |
// codebase. It can easily happen that an incompatible metric is added to an |
| 140 |
// unrelated part of the code, and suddenly code that used to work perfectly |
| 141 |
// fine starts to panic (provided that the registration of the newly added |
| 142 |
// metric happens before the registration of the previously existing |
| 143 |
// metric). This may come as an even bigger surprise with the global registry, |
| 144 |
// where simply importing another package can trigger a panic (if the newly |
| 145 |
// imported package registers metrics in its init function). At least, in the |
| 146 |
// prometheus package, creation of metrics and other collectors is separate from |
| 147 |
// registration. You first create the metric, and then you decide explicitly if |
| 148 |
// you want to register it with a local or the global registry, and if you want |
| 149 |
// to handle the error or risk a panic. With the constructors in the promauto |
| 150 |
// package, registration is automatic, and if it fails, it will always |
| 151 |
// panic. Furthermore, the constructors will often be called in the var section |
| 152 |
// of a file, which means that panicking will happen as a side effect of merely |
| 153 |
// importing a package. |
| 154 |
// |
| 155 |
// A separate package allows conservative users to entirely ignore it. And |
| 156 |
// whoever wants to use it will do so explicitly, with an opportunity to read |
| 157 |
// this warning. |
| 158 |
// |
| 159 |
// Enjoy promauto responsibly! |
| 160 |
package promauto |
| 161 |
|
| 162 |
import "github.com/prometheus/client_golang/prometheus" |
| 163 |
|
| 164 |
// NewCounter works like the function of the same name in the prometheus package |
| 165 |
// but it automatically registers the Counter with the |
| 166 |
// prometheus.DefaultRegisterer. If the registration fails, NewCounter panics. |
| 167 |
func NewCounter(opts prometheus.CounterOpts) prometheus.Counter { |
| 168 |
return With(prometheus.DefaultRegisterer).NewCounter(opts) |
| 169 |
} |
| 170 |
|
| 171 |
// NewCounterVec works like the function of the same name in the prometheus |
| 172 |
// package but it automatically registers the CounterVec with the |
| 173 |
// prometheus.DefaultRegisterer. If the registration fails, NewCounterVec |
| 174 |
// panics. |
| 175 |
func NewCounterVec(opts prometheus.CounterOpts, labelNames []string) *prometheus.CounterVec { |
| 176 |
return With(prometheus.DefaultRegisterer).NewCounterVec(opts, labelNames) |
| 177 |
} |
| 178 |
|
| 179 |
// NewCounterFunc works like the function of the same name in the prometheus |
| 180 |
// package but it automatically registers the CounterFunc with the |
| 181 |
// prometheus.DefaultRegisterer. If the registration fails, NewCounterFunc |
| 182 |
// panics. |
| 183 |
func NewCounterFunc(opts prometheus.CounterOpts, function func() float64) prometheus.CounterFunc { |
| 184 |
return With(prometheus.DefaultRegisterer).NewCounterFunc(opts, function) |
| 185 |
} |
| 186 |
|
| 187 |
// NewGauge works like the function of the same name in the prometheus package |
| 188 |
// but it automatically registers the Gauge with the |
| 189 |
// prometheus.DefaultRegisterer. If the registration fails, NewGauge panics. |
| 190 |
func NewGauge(opts prometheus.GaugeOpts) prometheus.Gauge { |
| 191 |
return With(prometheus.DefaultRegisterer).NewGauge(opts) |
| 192 |
} |
| 193 |
|
| 194 |
// NewGaugeVec works like the function of the same name in the prometheus |
| 195 |
// package but it automatically registers the GaugeVec with the |
| 196 |
// prometheus.DefaultRegisterer. If the registration fails, NewGaugeVec panics. |
| 197 |
func NewGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec { |
| 198 |
return With(prometheus.DefaultRegisterer).NewGaugeVec(opts, labelNames) |
| 199 |
} |
| 200 |
|
| 201 |
// NewGaugeFunc works like the function of the same name in the prometheus |
| 202 |
// package but it automatically registers the GaugeFunc with the |
| 203 |
// prometheus.DefaultRegisterer. If the registration fails, NewGaugeFunc panics. |
| 204 |
func NewGaugeFunc(opts prometheus.GaugeOpts, function func() float64) prometheus.GaugeFunc { |
| 205 |
return With(prometheus.DefaultRegisterer).NewGaugeFunc(opts, function) |
| 206 |
} |
| 207 |
|
| 208 |
// NewSummary works like the function of the same name in the prometheus package |
| 209 |
// but it automatically registers the Summary with the |
| 210 |
// prometheus.DefaultRegisterer. If the registration fails, NewSummary panics. |
| 211 |
func NewSummary(opts prometheus.SummaryOpts) prometheus.Summary { |
| 212 |
return With(prometheus.DefaultRegisterer).NewSummary(opts) |
| 213 |
} |
| 214 |
|
| 215 |
// NewSummaryVec works like the function of the same name in the prometheus |
| 216 |
// package but it automatically registers the SummaryVec with the |
| 217 |
// prometheus.DefaultRegisterer. If the registration fails, NewSummaryVec |
| 218 |
// panics. |
| 219 |
func NewSummaryVec(opts prometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec { |
| 220 |
return With(prometheus.DefaultRegisterer).NewSummaryVec(opts, labelNames) |
| 221 |
} |
| 222 |
|
| 223 |
// NewHistogram works like the function of the same name in the prometheus |
| 224 |
// package but it automatically registers the Histogram with the |
| 225 |
// prometheus.DefaultRegisterer. If the registration fails, NewHistogram panics. |
| 226 |
func NewHistogram(opts prometheus.HistogramOpts) prometheus.Histogram { |
| 227 |
return With(prometheus.DefaultRegisterer).NewHistogram(opts) |
| 228 |
} |
| 229 |
|
| 230 |
// NewHistogramVec works like the function of the same name in the prometheus |
| 231 |
// package but it automatically registers the HistogramVec with the |
| 232 |
// prometheus.DefaultRegisterer. If the registration fails, NewHistogramVec |
| 233 |
// panics. |
| 234 |
func NewHistogramVec(opts prometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec { |
| 235 |
return With(prometheus.DefaultRegisterer).NewHistogramVec(opts, labelNames) |
| 236 |
} |
| 237 |
|
| 238 |
// NewUntypedFunc works like the function of the same name in the prometheus |
| 239 |
// package but it automatically registers the UntypedFunc with the |
| 240 |
// prometheus.DefaultRegisterer. If the registration fails, NewUntypedFunc |
| 241 |
// panics. |
| 242 |
func NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc { |
| 243 |
return With(prometheus.DefaultRegisterer).NewUntypedFunc(opts, function) |
| 244 |
} |
| 245 |
|
| 246 |
// Factory provides factory methods to create Collectors that are automatically |
| 247 |
// registered with a Registerer. Create a Factory with the With function, |
| 248 |
// providing a Registerer to auto-register created Collectors with. The zero |
| 249 |
// value of a Factory creates Collectors that are not registered with any |
| 250 |
// Registerer. All methods of the Factory panic if the registration fails. |
| 251 |
type Factory struct { |
| 252 |
r prometheus.Registerer |
| 253 |
} |
| 254 |
|
| 255 |
// With creates a Factory using the provided Registerer for registration of the |
| 256 |
// created Collectors. If the provided Registerer is nil, the returned Factory |
| 257 |
// creates Collectors that are not registered with any Registerer. |
| 258 |
func With(r prometheus.Registerer) Factory { return Factory{r} } |
| 259 |
|
| 260 |
// NewCounter works like the function of the same name in the prometheus package |
| 261 |
// but it automatically registers the Counter with the Factory's Registerer. |
| 262 |
func (f Factory) NewCounter(opts prometheus.CounterOpts) prometheus.Counter { |
| 263 |
c := prometheus.NewCounter(opts) |
| 264 |
if f.r != nil { |
| 265 |
f.r.MustRegister(c) |
| 266 |
} |
| 267 |
return c |
| 268 |
} |
| 269 |
|
| 270 |
// NewCounterVec works like the function of the same name in the prometheus |
| 271 |
// package but it automatically registers the CounterVec with the Factory's |
| 272 |
// Registerer. |
| 273 |
func (f Factory) NewCounterVec(opts prometheus.CounterOpts, labelNames []string) *prometheus.CounterVec { |
| 274 |
c := prometheus.NewCounterVec(opts, labelNames) |
| 275 |
if f.r != nil { |
| 276 |
f.r.MustRegister(c) |
| 277 |
} |
| 278 |
return c |
| 279 |
} |
| 280 |
|
| 281 |
// NewCounterFunc works like the function of the same name in the prometheus |
| 282 |
// package but it automatically registers the CounterFunc with the Factory's |
| 283 |
// Registerer. |
| 284 |
func (f Factory) NewCounterFunc(opts prometheus.CounterOpts, function func() float64) prometheus.CounterFunc { |
| 285 |
c := prometheus.NewCounterFunc(opts, function) |
| 286 |
if f.r != nil { |
| 287 |
f.r.MustRegister(c) |
| 288 |
} |
| 289 |
return c |
| 290 |
} |
| 291 |
|
| 292 |
// NewGauge works like the function of the same name in the prometheus package |
| 293 |
// but it automatically registers the Gauge with the Factory's Registerer. |
| 294 |
func (f Factory) NewGauge(opts prometheus.GaugeOpts) prometheus.Gauge { |
| 295 |
g := prometheus.NewGauge(opts) |
| 296 |
if f.r != nil { |
| 297 |
f.r.MustRegister(g) |
| 298 |
} |
| 299 |
return g |
| 300 |
} |
| 301 |
|
| 302 |
// NewGaugeVec works like the function of the same name in the prometheus |
| 303 |
// package but it automatically registers the GaugeVec with the Factory's |
| 304 |
// Registerer. |
| 305 |
func (f Factory) NewGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec { |
| 306 |
g := prometheus.NewGaugeVec(opts, labelNames) |
| 307 |
if f.r != nil { |
| 308 |
f.r.MustRegister(g) |
| 309 |
} |
| 310 |
return g |
| 311 |
} |
| 312 |
|
| 313 |
// NewGaugeFunc works like the function of the same name in the prometheus |
| 314 |
// package but it automatically registers the GaugeFunc with the Factory's |
| 315 |
// Registerer. |
| 316 |
func (f Factory) NewGaugeFunc(opts prometheus.GaugeOpts, function func() float64) prometheus.GaugeFunc { |
| 317 |
g := prometheus.NewGaugeFunc(opts, function) |
| 318 |
if f.r != nil { |
| 319 |
f.r.MustRegister(g) |
| 320 |
} |
| 321 |
return g |
| 322 |
} |
| 323 |
|
| 324 |
// NewSummary works like the function of the same name in the prometheus package |
| 325 |
// but it automatically registers the Summary with the Factory's Registerer. |
| 326 |
func (f Factory) NewSummary(opts prometheus.SummaryOpts) prometheus.Summary { |
| 327 |
s := prometheus.NewSummary(opts) |
| 328 |
if f.r != nil { |
| 329 |
f.r.MustRegister(s) |
| 330 |
} |
| 331 |
return s |
| 332 |
} |
| 333 |
|
| 334 |
// NewSummaryVec works like the function of the same name in the prometheus |
| 335 |
// package but it automatically registers the SummaryVec with the Factory's |
| 336 |
// Registerer. |
| 337 |
func (f Factory) NewSummaryVec(opts prometheus.SummaryOpts, labelNames []string) *prometheus.SummaryVec { |
| 338 |
s := prometheus.NewSummaryVec(opts, labelNames) |
| 339 |
if f.r != nil { |
| 340 |
f.r.MustRegister(s) |
| 341 |
} |
| 342 |
return s |
| 343 |
} |
| 344 |
|
| 345 |
// NewHistogram works like the function of the same name in the prometheus |
| 346 |
// package but it automatically registers the Histogram with the Factory's |
| 347 |
// Registerer. |
| 348 |
func (f Factory) NewHistogram(opts prometheus.HistogramOpts) prometheus.Histogram { |
| 349 |
h := prometheus.NewHistogram(opts) |
| 350 |
if f.r != nil { |
| 351 |
f.r.MustRegister(h) |
| 352 |
} |
| 353 |
return h |
| 354 |
} |
| 355 |
|
| 356 |
// NewHistogramVec works like the function of the same name in the prometheus |
| 357 |
// package but it automatically registers the HistogramVec with the Factory's |
| 358 |
// Registerer. |
| 359 |
func (f Factory) NewHistogramVec(opts prometheus.HistogramOpts, labelNames []string) *prometheus.HistogramVec { |
| 360 |
h := prometheus.NewHistogramVec(opts, labelNames) |
| 361 |
if f.r != nil { |
| 362 |
f.r.MustRegister(h) |
| 363 |
} |
| 364 |
return h |
| 365 |
} |
| 366 |
|
| 367 |
// NewUntypedFunc works like the function of the same name in the prometheus |
| 368 |
// package but it automatically registers the UntypedFunc with the Factory's |
| 369 |
// Registerer. |
| 370 |
func (f Factory) NewUntypedFunc(opts prometheus.UntypedOpts, function func() float64) prometheus.UntypedFunc { |
| 371 |
u := prometheus.NewUntypedFunc(opts, function) |
| 372 |
if f.r != nil { |
| 373 |
f.r.MustRegister(u) |
| 374 |
} |
| 375 |
return u |
| 376 |
} |