2019-04-11 12:59:53 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-12-20 15:06:44 +03:00
|
|
|
"math"
|
|
|
|
"sync/atomic"
|
2019-04-11 12:59:53 +03:00
|
|
|
)
|
|
|
|
|
2023-12-20 15:06:44 +03:00
|
|
|
// NewGauge registers and returns gauge with the given name, which calls f to obtain gauge value.
|
2019-04-11 12:59:53 +03:00
|
|
|
//
|
|
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
|
|
|
// For instance,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-04-11 12:59:53 +03:00
|
|
|
//
|
|
|
|
// f must be safe for concurrent calls.
|
2023-12-20 15:06:44 +03:00
|
|
|
// if f is nil, then it is expected that the gauge value is changed via Gauge.Set() call.
|
2019-04-11 12:59:53 +03:00
|
|
|
//
|
|
|
|
// The returned gauge is safe to use from concurrent goroutines.
|
2020-07-05 17:49:49 +03:00
|
|
|
//
|
|
|
|
// See also FloatCounter for working with floating-point values.
|
2019-04-11 12:59:53 +03:00
|
|
|
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.
|
|
|
|
type Gauge struct {
|
2023-12-20 15:06:44 +03:00
|
|
|
// valueBits contains uint64 representation of float64 passed to Gauge.Set.
|
|
|
|
valueBits uint64
|
2023-12-22 17:54:57 +03:00
|
|
|
|
|
|
|
// f is a callback, which is called for returning the gauge value.
|
|
|
|
f func() float64
|
2019-04-11 12:59:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the current value for g.
|
|
|
|
func (g *Gauge) Get() float64 {
|
2023-12-20 15:06:44 +03:00
|
|
|
if f := g.f; f != nil {
|
|
|
|
return f()
|
|
|
|
}
|
|
|
|
n := atomic.LoadUint64(&g.valueBits)
|
|
|
|
return math.Float64frombits(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets g value to v.
|
|
|
|
//
|
|
|
|
// The g must be created with nil callback in order to be able to call this function.
|
|
|
|
func (g *Gauge) Set(v float64) {
|
|
|
|
if g.f != nil {
|
|
|
|
panic(fmt.Errorf("cannot call Set on gauge created with non-nil callback"))
|
|
|
|
}
|
|
|
|
n := math.Float64bits(v)
|
|
|
|
atomic.StoreUint64(&g.valueBits, n)
|
2019-04-11 12:59:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Gauge) marshalTo(prefix string, w io.Writer) {
|
2023-12-20 15:06:44 +03:00
|
|
|
v := g.Get()
|
2019-04-11 12:59:53 +03:00
|
|
|
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
|
|
|
|
2023-12-19 03:36:54 +03:00
|
|
|
func (g *Gauge) metricType() string {
|
|
|
|
return "gauge"
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
//
|
2022-08-08 17:15:06 +03:00
|
|
|
// - foo
|
|
|
|
// - foo{bar="baz"}
|
|
|
|
// - foo{bar="baz",aaa="b"}
|
2019-04-15 15:34:02 +03:00
|
|
|
//
|
|
|
|
// The returned gauge is safe to use from concurrent goroutines.
|
|
|
|
//
|
|
|
|
// Performance tip: prefer NewGauge instead of GetOrCreateGauge.
|
2020-07-05 17:49:49 +03:00
|
|
|
//
|
|
|
|
// See also FloatCounter for working with floating-point values.
|
2019-04-15 15:34:02 +03:00
|
|
|
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
|
|
|
}
|