Test and support 1.19 (#1160)

* Add new Go 1.19 metrics

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>

* Format files with the latest formatter

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
This commit is contained in:
Kemal Akkoyun 2022-11-08 00:14:19 +01:00 committed by GitHub
parent b785d0c828
commit 870469ecf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 181 additions and 165 deletions

View File

@ -54,6 +54,10 @@ workflows:
name: go-1-18 name: go-1-18
go_version: "1.18" go_version: "1.18"
run_lint: true run_lint: true
- test:
name: go-1-19
go_version: "1.19"
run_lint: true
# Style and unused/missing packages are only checked against # Style and unused/missing packages are only checked against
# the latest supported Go version. # the latest supported Go version.
run_style_and_unused: true run_style_and_unused: true

View File

@ -335,14 +335,15 @@ type RuleGroup struct {
// that rules are returned in by the API. // that rules are returned in by the API.
// //
// Rule types can be determined using a type switch: // Rule types can be determined using a type switch:
// switch v := rule.(type) { //
// case RecordingRule: // switch v := rule.(type) {
// fmt.Print("got a recording rule") // case RecordingRule:
// case AlertingRule: // fmt.Print("got a recording rule")
// fmt.Print("got a alerting rule") // case AlertingRule:
// default: // fmt.Print("got a alerting rule")
// fmt.Printf("unknown rule type %s", v) // default:
// } // fmt.Printf("unknown rule type %s", v)
// }
type Rules []interface{} type Rules []interface{}
// AlertingRule models a alerting rule. // AlertingRule models a alerting rule.

View File

@ -246,7 +246,8 @@ func (v *CounterVec) GetMetricWith(labels Labels) (Counter, error) {
// WithLabelValues works as GetMetricWithLabelValues, but panics where // WithLabelValues works as GetMetricWithLabelValues, but panics where
// GetMetricWithLabelValues would have returned an error. Not returning an // GetMetricWithLabelValues would have returned an error. Not returning an
// error allows shortcuts like // error allows shortcuts like
// myVec.WithLabelValues("404", "GET").Add(42) //
// myVec.WithLabelValues("404", "GET").Add(42)
func (v *CounterVec) WithLabelValues(lvs ...string) Counter { func (v *CounterVec) WithLabelValues(lvs ...string) Counter {
c, err := v.GetMetricWithLabelValues(lvs...) c, err := v.GetMetricWithLabelValues(lvs...)
if err != nil { if err != nil {
@ -257,7 +258,8 @@ func (v *CounterVec) WithLabelValues(lvs ...string) Counter {
// With works as GetMetricWith, but panics where GetMetricWithLabels would have // With works as GetMetricWith, but panics where GetMetricWithLabels would have
// returned an error. Not returning an error allows shortcuts like // returned an error. Not returning an error allows shortcuts like
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Add(42) //
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Add(42)
func (v *CounterVec) With(labels Labels) Counter { func (v *CounterVec) With(labels Labels) Counter {
c, err := v.GetMetricWith(labels) c, err := v.GetMetricWith(labels)
if err != nil { if err != nil {

View File

@ -21,67 +21,66 @@
// All exported functions and methods are safe to be used concurrently unless // All exported functions and methods are safe to be used concurrently unless
// specified otherwise. // specified otherwise.
// //
// A Basic Example // # A Basic Example
// //
// As a starting point, a very basic usage example: // As a starting point, a very basic usage example:
// //
// package main // package main
// //
// import ( // import (
// "log" // "log"
// "net/http" // "net/http"
// //
// "github.com/prometheus/client_golang/prometheus" // "github.com/prometheus/client_golang/prometheus"
// "github.com/prometheus/client_golang/prometheus/promhttp" // "github.com/prometheus/client_golang/prometheus/promhttp"
// ) // )
// //
// type metrics struct { // type metrics struct {
// cpuTemp prometheus.Gauge // cpuTemp prometheus.Gauge
// hdFailures *prometheus.CounterVec // hdFailures *prometheus.CounterVec
// } // }
// //
// func NewMetrics(reg prometheus.Registerer) *metrics { // func NewMetrics(reg prometheus.Registerer) *metrics {
// m := &metrics{ // m := &metrics{
// cpuTemp: prometheus.NewGauge(prometheus.GaugeOpts{ // cpuTemp: prometheus.NewGauge(prometheus.GaugeOpts{
// Name: "cpu_temperature_celsius", // Name: "cpu_temperature_celsius",
// Help: "Current temperature of the CPU.", // Help: "Current temperature of the CPU.",
// }), // }),
// hdFailures: prometheus.NewCounterVec( // hdFailures: prometheus.NewCounterVec(
// prometheus.CounterOpts{ // prometheus.CounterOpts{
// Name: "hd_errors_total", // Name: "hd_errors_total",
// Help: "Number of hard-disk errors.", // Help: "Number of hard-disk errors.",
// }, // },
// []string{"device"}, // []string{"device"},
// ), // ),
// } // }
// reg.MustRegister(m.cpuTemp) // reg.MustRegister(m.cpuTemp)
// reg.MustRegister(m.hdFailures) // reg.MustRegister(m.hdFailures)
// return m // return m
// } // }
// //
// func main() { // func main() {
// // Create a non-global registry. // // Create a non-global registry.
// reg := prometheus.NewRegistry() // reg := prometheus.NewRegistry()
// //
// // Create new metrics and register them using the custom registry. // // Create new metrics and register them using the custom registry.
// m := NewMetrics(reg) // m := NewMetrics(reg)
// // Set values for the new created metrics. // // Set values for the new created metrics.
// m.cpuTemp.Set(65.3) // m.cpuTemp.Set(65.3)
// m.hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc() // m.hdFailures.With(prometheus.Labels{"device":"/dev/sda"}).Inc()
//
// // Expose metrics and custom registry via an HTTP server
// // using the HandleFor function. "/metrics" is the usual endpoint for that.
// http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
// log.Fatal(http.ListenAndServe(":8080", nil))
// }
// //
// // Expose metrics and custom registry via an HTTP server
// // using the HandleFor function. "/metrics" is the usual endpoint for that.
// http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
// log.Fatal(http.ListenAndServe(":8080", nil))
// }
// //
// This is a complete program that exports two metrics, a Gauge and a Counter, // This is a complete program that exports two metrics, a Gauge and a Counter,
// the latter with a label attached to turn it into a (one-dimensional) vector. // the latter with a label attached to turn it into a (one-dimensional) vector.
// It register the metrics using a custom registry and exposes them via an HTTP server // It register the metrics using a custom registry and exposes them via an HTTP server
// on the /metrics endpoint. // on the /metrics endpoint.
// //
// Metrics // # Metrics
// //
// The number of exported identifiers in this package might appear a bit // The number of exported identifiers in this package might appear a bit
// overwhelming. However, in addition to the basic plumbing shown in the example // overwhelming. However, in addition to the basic plumbing shown in the example
@ -112,7 +111,7 @@
// To create instances of Metrics and their vector versions, you need a suitable // To create instances of Metrics and their vector versions, you need a suitable
// …Opts struct, i.e. GaugeOpts, CounterOpts, SummaryOpts, or HistogramOpts. // …Opts struct, i.e. GaugeOpts, CounterOpts, SummaryOpts, or HistogramOpts.
// //
// Custom Collectors and constant Metrics // # Custom Collectors and constant Metrics
// //
// While you could create your own implementations of Metric, most likely you // While you could create your own implementations of Metric, most likely you
// will only ever implement the Collector interface on your own. At a first // will only ever implement the Collector interface on your own. At a first
@ -153,7 +152,7 @@
// a metric, GaugeFunc, CounterFunc, or UntypedFunc might be interesting // a metric, GaugeFunc, CounterFunc, or UntypedFunc might be interesting
// shortcuts. // shortcuts.
// //
// Advanced Uses of the Registry // # Advanced Uses of the Registry
// //
// While MustRegister is the by far most common way of registering a Collector, // While MustRegister is the by far most common way of registering a Collector,
// sometimes you might want to handle the errors the registration might cause. // sometimes you might want to handle the errors the registration might cause.
@ -188,23 +187,23 @@
// NewProcessCollector). With a custom registry, you are in control and decide // NewProcessCollector). With a custom registry, you are in control and decide
// yourself about the Collectors to register. // yourself about the Collectors to register.
// //
// HTTP Exposition // # HTTP Exposition
// //
// The Registry implements the Gatherer interface. The caller of the Gather // The Registry implements the Gatherer interface. The caller of the Gather
// method can then expose the gathered metrics in some way. Usually, the metrics // method can then expose the gathered metrics in some way. Usually, the metrics
// are served via HTTP on the /metrics endpoint. That's happening in the example // are served via HTTP on the /metrics endpoint. That's happening in the example
// above. The tools to expose metrics via HTTP are in the promhttp sub-package. // above. The tools to expose metrics via HTTP are in the promhttp sub-package.
// //
// Pushing to the Pushgateway // # Pushing to the Pushgateway
// //
// Function for pushing to the Pushgateway can be found in the push sub-package. // Function for pushing to the Pushgateway can be found in the push sub-package.
// //
// Graphite Bridge // # Graphite Bridge
// //
// Functions and examples to push metrics from a Gatherer to Graphite can be // Functions and examples to push metrics from a Gatherer to Graphite can be
// found in the graphite sub-package. // found in the graphite sub-package.
// //
// Other Means of Exposition // # Other Means of Exposition
// //
// More ways of exposing metrics can easily be added by following the approaches // More ways of exposing metrics can easily be added by following the approaches
// of the existing implementations. // of the existing implementations.

View File

@ -210,7 +210,8 @@ func (v *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) {
// WithLabelValues works as GetMetricWithLabelValues, but panics where // WithLabelValues works as GetMetricWithLabelValues, but panics where
// GetMetricWithLabelValues would have returned an error. Not returning an // GetMetricWithLabelValues would have returned an error. Not returning an
// error allows shortcuts like // error allows shortcuts like
// myVec.WithLabelValues("404", "GET").Add(42) //
// myVec.WithLabelValues("404", "GET").Add(42)
func (v *GaugeVec) WithLabelValues(lvs ...string) Gauge { func (v *GaugeVec) WithLabelValues(lvs ...string) Gauge {
g, err := v.GetMetricWithLabelValues(lvs...) g, err := v.GetMetricWithLabelValues(lvs...)
if err != nil { if err != nil {
@ -221,7 +222,8 @@ func (v *GaugeVec) WithLabelValues(lvs ...string) Gauge {
// With works as GetMetricWith, but panics where GetMetricWithLabels would have // With works as GetMetricWith, but panics where GetMetricWithLabels would have
// returned an error. Not returning an error allows shortcuts like // returned an error. Not returning an error allows shortcuts like
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Add(42) //
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Add(42)
func (v *GaugeVec) With(labels Labels) Gauge { func (v *GaugeVec) With(labels Labels) Gauge {
g, err := v.GetMetricWith(labels) g, err := v.GetMetricWith(labels)
if err != nil { if err != nil {

View File

@ -201,12 +201,15 @@ func (m *SequenceMatcher) isBJunk(s string) bool {
// If IsJunk is not defined: // If IsJunk is not defined:
// //
// Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where // Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
// alo <= i <= i+k <= ahi //
// blo <= j <= j+k <= bhi // alo <= i <= i+k <= ahi
// blo <= j <= j+k <= bhi
//
// and for all (i',j',k') meeting those conditions, // and for all (i',j',k') meeting those conditions,
// k >= k' //
// i <= i' // k >= k'
// and if i == i', j <= j' // i <= i'
// and if i == i', j <= j'
// //
// In other words, of all maximal matching blocks, return one that // In other words, of all maximal matching blocks, return one that
// starts earliest in a, and of all those maximal matching blocks that // starts earliest in a, and of all those maximal matching blocks that

View File

@ -25,7 +25,8 @@ import (
// Labels represents a collection of label name -> value mappings. This type is // Labels represents a collection of label name -> value mappings. This type is
// commonly used with the With(Labels) and GetMetricWith(Labels) methods of // commonly used with the With(Labels) and GetMetricWith(Labels) methods of
// metric vector Collectors, e.g.: // metric vector Collectors, e.g.:
// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42) //
// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
// //
// The other use-case is the specification of constant label pairs in Opts or to // The other use-case is the specification of constant label pairs in Opts or to
// create a Desc. // create a Desc.

View File

@ -25,103 +25,103 @@
// The following example is a complete program to create a histogram of normally // The following example is a complete program to create a histogram of normally
// distributed random numbers from the math/rand package: // distributed random numbers from the math/rand package:
// //
// package main // package main
// //
// import ( // import (
// "math/rand" // "math/rand"
// "net/http" // "net/http"
// //
// "github.com/prometheus/client_golang/prometheus" // "github.com/prometheus/client_golang/prometheus"
// "github.com/prometheus/client_golang/prometheus/promauto" // "github.com/prometheus/client_golang/prometheus/promauto"
// "github.com/prometheus/client_golang/prometheus/promhttp" // "github.com/prometheus/client_golang/prometheus/promhttp"
// ) // )
// //
// var histogram = promauto.NewHistogram(prometheus.HistogramOpts{ // var histogram = promauto.NewHistogram(prometheus.HistogramOpts{
// Name: "random_numbers", // Name: "random_numbers",
// Help: "A histogram of normally distributed random numbers.", // Help: "A histogram of normally distributed random numbers.",
// Buckets: prometheus.LinearBuckets(-3, .1, 61), // Buckets: prometheus.LinearBuckets(-3, .1, 61),
// }) // })
// //
// func Random() { // func Random() {
// for { // for {
// histogram.Observe(rand.NormFloat64()) // histogram.Observe(rand.NormFloat64())
// } // }
// } // }
// //
// func main() { // func main() {
// go Random() // go Random()
// http.Handle("/metrics", promhttp.Handler()) // http.Handle("/metrics", promhttp.Handler())
// http.ListenAndServe(":1971", nil) // http.ListenAndServe(":1971", nil)
// } // }
// //
// Prometheus's version of a minimal hello-world program: // Prometheus's version of a minimal hello-world program:
// //
// package main // package main
// //
// import ( // import (
// "fmt" // "fmt"
// "net/http" // "net/http"
// //
// "github.com/prometheus/client_golang/prometheus" // "github.com/prometheus/client_golang/prometheus"
// "github.com/prometheus/client_golang/prometheus/promauto" // "github.com/prometheus/client_golang/prometheus/promauto"
// "github.com/prometheus/client_golang/prometheus/promhttp" // "github.com/prometheus/client_golang/prometheus/promhttp"
// ) // )
// //
// func main() { // func main() {
// http.Handle("/", promhttp.InstrumentHandlerCounter( // http.Handle("/", promhttp.InstrumentHandlerCounter(
// promauto.NewCounterVec( // promauto.NewCounterVec(
// prometheus.CounterOpts{ // prometheus.CounterOpts{
// Name: "hello_requests_total", // Name: "hello_requests_total",
// Help: "Total number of hello-world requests by HTTP code.", // Help: "Total number of hello-world requests by HTTP code.",
// }, // },
// []string{"code"}, // []string{"code"},
// ), // ),
// http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// fmt.Fprint(w, "Hello, world!") // fmt.Fprint(w, "Hello, world!")
// }), // }),
// )) // ))
// http.Handle("/metrics", promhttp.Handler()) // http.Handle("/metrics", promhttp.Handler())
// http.ListenAndServe(":1971", nil) // http.ListenAndServe(":1971", nil)
// } // }
// //
// A Factory is created with the With(prometheus.Registerer) function, which // A Factory is created with the With(prometheus.Registerer) function, which
// enables two usage pattern. With(prometheus.Registerer) can be called once per // enables two usage pattern. With(prometheus.Registerer) can be called once per
// line: // line:
// //
// var ( // var (
// reg = prometheus.NewRegistry() // reg = prometheus.NewRegistry()
// randomNumbers = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ // randomNumbers = promauto.With(reg).NewHistogram(prometheus.HistogramOpts{
// Name: "random_numbers", // Name: "random_numbers",
// Help: "A histogram of normally distributed random numbers.", // Help: "A histogram of normally distributed random numbers.",
// Buckets: prometheus.LinearBuckets(-3, .1, 61), // Buckets: prometheus.LinearBuckets(-3, .1, 61),
// }) // })
// requestCount = promauto.With(reg).NewCounterVec( // requestCount = promauto.With(reg).NewCounterVec(
// prometheus.CounterOpts{ // prometheus.CounterOpts{
// Name: "http_requests_total", // Name: "http_requests_total",
// Help: "Total number of HTTP requests by status code and method.", // Help: "Total number of HTTP requests by status code and method.",
// }, // },
// []string{"code", "method"}, // []string{"code", "method"},
// ) // )
// ) // )
// //
// Or it can be used to create a Factory once to be used multiple times: // Or it can be used to create a Factory once to be used multiple times:
// //
// var ( // var (
// reg = prometheus.NewRegistry() // reg = prometheus.NewRegistry()
// factory = promauto.With(reg) // factory = promauto.With(reg)
// randomNumbers = factory.NewHistogram(prometheus.HistogramOpts{ // randomNumbers = factory.NewHistogram(prometheus.HistogramOpts{
// Name: "random_numbers", // Name: "random_numbers",
// Help: "A histogram of normally distributed random numbers.", // Help: "A histogram of normally distributed random numbers.",
// Buckets: prometheus.LinearBuckets(-3, .1, 61), // Buckets: prometheus.LinearBuckets(-3, .1, 61),
// }) // })
// requestCount = factory.NewCounterVec( // requestCount = factory.NewCounterVec(
// prometheus.CounterOpts{ // prometheus.CounterOpts{
// Name: "http_requests_total", // Name: "http_requests_total",
// Help: "Total number of HTTP requests by status code and method.", // Help: "Total number of HTTP requests by status code and method.",
// }, // },
// []string{"code", "method"}, // []string{"code", "method"},
// ) // )
// ) // )
// //
// This appears very handy. So why are these constructors locked away in a // This appears very handy. So why are these constructors locked away in a
// separate package? // separate package?

View File

@ -15,17 +15,17 @@
// builder approach. Create a Pusher with New and then add the various options // builder approach. Create a Pusher with New and then add the various options
// by using its methods, finally calling Add or Push, like this: // by using its methods, finally calling Add or Push, like this:
// //
// // Easy case: // // Easy case:
// push.New("http://example.org/metrics", "my_job").Gatherer(myRegistry).Push() // push.New("http://example.org/metrics", "my_job").Gatherer(myRegistry).Push()
// //
// // Complex case: // // Complex case:
// push.New("http://example.org/metrics", "my_job"). // push.New("http://example.org/metrics", "my_job").
// Collector(myCollector1). // Collector(myCollector1).
// Collector(myCollector2). // Collector(myCollector2).
// Grouping("zone", "xy"). // Grouping("zone", "xy").
// Client(&myHTTPClient). // Client(&myHTTPClient).
// BasicAuth("top", "secret"). // BasicAuth("top", "secret").
// Add() // Add()
// //
// See the examples section for more detailed examples. // See the examples section for more detailed examples.
// //

View File

@ -603,7 +603,8 @@ func (v *SummaryVec) GetMetricWith(labels Labels) (Observer, error) {
// WithLabelValues works as GetMetricWithLabelValues, but panics where // WithLabelValues works as GetMetricWithLabelValues, but panics where
// GetMetricWithLabelValues would have returned an error. Not returning an // GetMetricWithLabelValues would have returned an error. Not returning an
// error allows shortcuts like // error allows shortcuts like
// myVec.WithLabelValues("404", "GET").Observe(42.21) //
// myVec.WithLabelValues("404", "GET").Observe(42.21)
func (v *SummaryVec) WithLabelValues(lvs ...string) Observer { func (v *SummaryVec) WithLabelValues(lvs ...string) Observer {
s, err := v.GetMetricWithLabelValues(lvs...) s, err := v.GetMetricWithLabelValues(lvs...)
if err != nil { if err != nil {
@ -614,7 +615,8 @@ func (v *SummaryVec) WithLabelValues(lvs ...string) Observer {
// With works as GetMetricWith, but panics where GetMetricWithLabels would have // With works as GetMetricWith, but panics where GetMetricWithLabels would have
// returned an error. Not returning an error allows shortcuts like // returned an error. Not returning an error allows shortcuts like
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Observe(42.21) //
// myVec.With(prometheus.Labels{"code": "404", "method": "GET"}).Observe(42.21)
func (v *SummaryVec) With(labels Labels) Observer { func (v *SummaryVec) With(labels Labels) Observer {
s, err := v.GetMetricWith(labels) s, err := v.GetMetricWith(labels)
if err != nil { if err != nil {
@ -701,7 +703,8 @@ func (s *constSummary) Write(out *dto.Metric) error {
// //
// quantiles maps ranks to quantile values. For example, a median latency of // quantiles maps ranks to quantile values. For example, a median latency of
// 0.23s and a 99th percentile latency of 0.56s would be expressed as: // 0.23s and a 99th percentile latency of 0.56s would be expressed as:
// map[float64]float64{0.5: 0.23, 0.99: 0.56} //
// map[float64]float64{0.5: 0.23, 0.99: 0.56}
// //
// NewConstSummary returns an error if the length of labelValues is not // NewConstSummary returns an error if the length of labelValues is not
// consistent with the variable labels in Desc or if Desc is invalid. // consistent with the variable labels in Desc or if Desc is invalid.

View File

@ -25,11 +25,12 @@ type Timer struct {
// NewTimer creates a new Timer. The provided Observer is used to observe a // NewTimer creates a new Timer. The provided Observer is used to observe a
// duration in seconds. Timer is usually used to time a function call in the // duration in seconds. Timer is usually used to time a function call in the
// following way: // following way:
// func TimeMe() { //
// timer := NewTimer(myHistogram) // func TimeMe() {
// defer timer.ObserveDuration() // timer := NewTimer(myHistogram)
// // Do actual work. // defer timer.ObserveDuration()
// } // // Do actual work.
// }
func NewTimer(o Observer) *Timer { func NewTimer(o Observer) *Timer {
return &Timer{ return &Timer{
begin: time.Now(), begin: time.Now(),