gauge.go: add Set() method, which can be used for changing the gauge value without the need to pass callback to NewGauge()
This commit is contained in:
parent
efd161d607
commit
b23fdf5bd7
31
gauge.go
31
gauge.go
@ -3,10 +3,11 @@ package metrics
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewGauge registers and returns gauge with the given name, which calls f
|
// NewGauge registers and returns gauge with the given name, which calls f to obtain gauge value.
|
||||||
// to obtain gauge value.
|
|
||||||
//
|
//
|
||||||
// name must be valid Prometheus-compatible metric with possible labels.
|
// name must be valid Prometheus-compatible metric with possible labels.
|
||||||
// For instance,
|
// For instance,
|
||||||
@ -16,6 +17,7 @@ import (
|
|||||||
// - foo{bar="baz",aaa="b"}
|
// - foo{bar="baz",aaa="b"}
|
||||||
//
|
//
|
||||||
// f must be safe for concurrent calls.
|
// f must be safe for concurrent calls.
|
||||||
|
// if f is nil, then it is expected that the gauge value is changed via Gauge.Set() call.
|
||||||
//
|
//
|
||||||
// The returned gauge is safe to use from concurrent goroutines.
|
// The returned gauge is safe to use from concurrent goroutines.
|
||||||
//
|
//
|
||||||
@ -25,19 +27,36 @@ func NewGauge(name string, f func() float64) *Gauge {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Gauge is a float64 gauge.
|
// Gauge is a float64 gauge.
|
||||||
//
|
|
||||||
// See also Counter, which could be used as a gauge with Set and Dec calls.
|
|
||||||
type Gauge struct {
|
type Gauge struct {
|
||||||
|
// f is a callback, which is called for returning the gauge value.
|
||||||
f func() float64
|
f func() float64
|
||||||
|
|
||||||
|
// valueBits contains uint64 representation of float64 passed to Gauge.Set.
|
||||||
|
valueBits uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the current value for g.
|
// Get returns the current value for g.
|
||||||
func (g *Gauge) Get() float64 {
|
func (g *Gauge) Get() float64 {
|
||||||
return g.f()
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Gauge) marshalTo(prefix string, w io.Writer) {
|
func (g *Gauge) marshalTo(prefix string, w io.Writer) {
|
||||||
v := g.f()
|
v := g.Get()
|
||||||
if float64(int64(v)) == v {
|
if float64(int64(v)) == v {
|
||||||
// Marshal integer values without scientific notation
|
// Marshal integer values without scientific notation
|
||||||
fmt.Fprintf(w, "%s %d\n", prefix, int64(v))
|
fmt.Fprintf(w, "%s %d\n", prefix, int64(v))
|
||||||
|
@ -7,14 +7,28 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestGaugeError(t *testing.T) {
|
func TestGaugeError(t *testing.T) {
|
||||||
expectPanic(t, "NewGauge_nil_callback", func() {
|
expectPanic(t, "NewGauge_Set_non-nil-callback", func() {
|
||||||
NewGauge("NewGauge_nil_callback", nil)
|
g := NewGauge("NewGauge_non_nil_callback", func() float64 { return 123 })
|
||||||
|
g.Set(12.35)
|
||||||
})
|
})
|
||||||
expectPanic(t, "GetOrCreateGauge_nil_callback", func() {
|
expectPanic(t, "GetOrCreateGauge_Set_non-nil-callback", func() {
|
||||||
GetOrCreateGauge("GetOrCreateGauge_nil_callback", nil)
|
g := GetOrCreateGauge("GetOrCreateGauge_nil_callback", func() float64 { return 123 })
|
||||||
|
g.Set(42)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGaugeSet(t *testing.T) {
|
||||||
|
s := NewSet()
|
||||||
|
g := s.NewGauge("foo", nil)
|
||||||
|
if n := g.Get(); n != 0 {
|
||||||
|
t.Fatalf("unexpected gauge value: %g; expecting 0", n)
|
||||||
|
}
|
||||||
|
g.Set(1.234)
|
||||||
|
if n := g.Get(); n != 1.234 {
|
||||||
|
t.Fatalf("unexpected gauge value %g; expecting 1.234", n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGaugeSerial(t *testing.T) {
|
func TestGaugeSerial(t *testing.T) {
|
||||||
name := "GaugeSerial"
|
name := "GaugeSerial"
|
||||||
n := 1.23
|
n := 1.23
|
||||||
|
6
set.go
6
set.go
@ -251,9 +251,6 @@ func (s *Set) GetOrCreateFloatCounter(name string) *FloatCounter {
|
|||||||
//
|
//
|
||||||
// The returned gauge is safe to use from concurrent goroutines.
|
// The returned gauge is safe to use from concurrent goroutines.
|
||||||
func (s *Set) NewGauge(name string, f func() float64) *Gauge {
|
func (s *Set) NewGauge(name string, f func() float64) *Gauge {
|
||||||
if f == nil {
|
|
||||||
panic(fmt.Errorf("BUG: f cannot be nil"))
|
|
||||||
}
|
|
||||||
g := &Gauge{
|
g := &Gauge{
|
||||||
f: f,
|
f: f,
|
||||||
}
|
}
|
||||||
@ -280,9 +277,6 @@ func (s *Set) GetOrCreateGauge(name string, f func() float64) *Gauge {
|
|||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
if nm == nil {
|
if nm == nil {
|
||||||
// Slow path - create and register missing gauge.
|
// Slow path - create and register missing gauge.
|
||||||
if f == nil {
|
|
||||||
panic(fmt.Errorf("BUG: f cannot be nil"))
|
|
||||||
}
|
|
||||||
if err := validateMetric(name); err != nil {
|
if err := validateMetric(name); err != nil {
|
||||||
panic(fmt.Errorf("BUG: invalid metric name %q: %s", name, err))
|
panic(fmt.Errorf("BUG: invalid metric name %q: %s", name, err))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user