2019-04-11 12:59:53 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewGauge registers and returns gauge with the given name, which calls f
|
|
|
|
// to obtain gauge value.
|
|
|
|
//
|
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
|
|
|
// For instance,
|
|
|
|
//
|
|
|
|
// * foo
|
|
|
|
// * foo{bar="baz"}
|
|
|
|
// * foo{bar="baz",aaa="b"}
|
|
|
|
//
|
|
|
|
// f must be safe for concurrent calls.
|
|
|
|
//
|
|
|
|
// The returned gauge is safe to use from concurrent goroutines.
|
|
|
|
func NewGauge(name string, f func() float64) *Gauge {
|
2019-06-01 23:18:41 +03:00
|
|
|
return defaultSet.NewGauge(name, f)
|
2019-04-11 12:59:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gauge is a float64 gauge.
|
2019-04-15 15:22:58 +03:00
|
|
|
//
|
|
|
|
// See also Counter, which could be used as a gauge with Set and Dec calls.
|
2019-04-11 12:59:53 +03:00
|
|
|
type Gauge struct {
|
|
|
|
f func() float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the current value for g.
|
|
|
|
func (g *Gauge) Get() float64 {
|
|
|
|
return g.f()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gauge) marshalTo(prefix string, w io.Writer) {
|
|
|
|
v := g.f()
|
|
|
|
if float64(int64(v)) == v {
|
2019-08-13 13:24:46 +03:00
|
|
|
// Marshal integer values without scientific notation
|
2019-04-11 12:59:53 +03:00
|
|
|
fmt.Fprintf(w, "%s %d\n", prefix, int64(v))
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "%s %g\n", prefix, v)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 15:34:02 +03:00
|
|
|
|
|
|
|
// GetOrCreateGauge returns registered gauge with the given name
|
|
|
|
// or creates new gauge if the registry doesn't contain gauge with
|
|
|
|
// the given name.
|
|
|
|
//
|
2019-09-30 00:06:17 +03:00
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
2019-04-15 15:34:02 +03:00
|
|
|
// For instance,
|
|
|
|
//
|
|
|
|
// * foo
|
|
|
|
// * foo{bar="baz"}
|
|
|
|
// * foo{bar="baz",aaa="b"}
|
|
|
|
//
|
|
|
|
// The returned gauge is safe to use from concurrent goroutines.
|
|
|
|
//
|
|
|
|
// Performance tip: prefer NewGauge instead of GetOrCreateGauge.
|
|
|
|
func GetOrCreateGauge(name string, f func() float64) *Gauge {
|
2019-06-01 23:18:41 +03:00
|
|
|
return defaultSet.GetOrCreateGauge(name, f)
|
2019-04-15 15:34:02 +03:00
|
|
|
}
|