Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Switch to using signed int64s for gauges.
Browse files Browse the repository at this point in the history
  • Loading branch information
codahale committed Sep 2, 2014
1 parent b05b08d commit b55884e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
26 changes: 13 additions & 13 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//
// Gauges
//
// A gauge returns instantaneous measurements of something using 64-bit floating
// point values.
// A gauge returns instantaneous measurements of something using signed, 64-bit
// integers. This value does not need to be monotonic.
//
// Histograms
//
Expand Down Expand Up @@ -84,18 +84,18 @@ func (c Counter) SetBatchFunc(key interface{}, init func(), f func() uint64) {
type Gauge string

// Set the gauge's value to the given value.
func (g Gauge) Set(value float64) {
func (g Gauge) Set(value int64) {
gm.Lock()
defer gm.Unlock()

gauges[string(g)] = func() float64 {
gauges[string(g)] = func() int64 {
return value
}
}

// SetFunc sets the gauge's value to the lazily-called return value of the given
// function.
func (g Gauge) SetFunc(f func() float64) {
func (g Gauge) SetFunc(f func() int64) {
gm.Lock()
defer gm.Unlock()

Expand All @@ -105,7 +105,7 @@ func (g Gauge) SetFunc(f func() float64) {
// SetBatchFunc sets the gauge's value to the lazily-called return value of the
// given function, with an additional initializer function for a related batch
// of gauges, all of which are keyed by an arbitrary value.
func (g Gauge) SetBatchFunc(key interface{}, init func(), f func() float64) {
func (g Gauge) SetBatchFunc(key interface{}, init func(), f func() int64) {
gm.Lock()
defer gm.Unlock()

Expand All @@ -128,13 +128,13 @@ func Reset() {

counters = make(map[string]uint64)
counterFuncs = make(map[string]func() uint64)
gauges = make(map[string]func() float64)
gauges = make(map[string]func() int64)
histograms = make(map[string]*Histogram)
inits = make(map[interface{}]func())
}

// Snapshot returns a copy of the values of all registered counters and gauges.
func Snapshot() (c map[string]uint64, g map[string]float64) {
func Snapshot() (c map[string]uint64, g map[string]int64) {
cm.Lock()
defer cm.Unlock()

Expand All @@ -157,7 +157,7 @@ func Snapshot() (c map[string]uint64, g map[string]float64) {
c[n] = f()
}

g = make(map[string]float64, len(gauges))
g = make(map[string]int64, len(gauges))
for n, f := range gauges {
g[n] = f()
}
Expand Down Expand Up @@ -225,23 +225,23 @@ func (h *Histogram) merge() {
h.m = h.hist.Merge()
}

func (h *Histogram) valueAt(q float64) func() float64 {
return func() float64 {
func (h *Histogram) valueAt(q float64) func() int64 {
return func() int64 {
h.rw.RLock()
defer h.rw.RUnlock()

if h.m == nil {
return 0
}

return float64(h.m.ValueAtQuantile(q))
return h.m.ValueAtQuantile(q)
}
}

var (
counters = make(map[string]uint64)
counterFuncs = make(map[string]func() uint64)
gauges = make(map[string]func() float64)
gauges = make(map[string]func() int64)
inits = make(map[interface{}]func())
histograms = make(map[string]*Histogram)

Expand Down
22 changes: 11 additions & 11 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ func TestCounterBatchFunc(t *testing.T) {
func TestGaugeValue(t *testing.T) {
metrics.Reset()

metrics.Gauge("whee").Set(100.01)
metrics.Gauge("whee").Set(-100)

_, gauges := metrics.Snapshot()
if v, want := gauges["whee"], 100.01; v != want {
if v, want := gauges["whee"], int64(-100); v != want {
t.Errorf("Gauge was %v, but expected %v", v, want)
}
}

func TestGaugeFunc(t *testing.T) {
metrics.Reset()

metrics.Gauge("whee").SetFunc(func() float64 {
return 100.03
metrics.Gauge("whee").SetFunc(func() int64 {
return -100
})

_, gauges := metrics.Snapshot()
if v, want := gauges["whee"], 100.03; v != want {
if v, want := gauges["whee"], int64(-100); v != want {
t.Errorf("Gauge was %v, but expected %v", v, want)
}
}
Expand All @@ -102,27 +102,27 @@ func TestHistogram(t *testing.T) {

_, gauges := metrics.Snapshot()

if v, want := gauges["heyo.P50"], 71.0; v != want {
if v, want := gauges["heyo.P50"], int64(71); v != want {
t.Errorf("P50 was %v, but expected %v", v, want)
}

if v, want := gauges["heyo.P75"], 87.0; v != want {
if v, want := gauges["heyo.P75"], int64(87); v != want {
t.Errorf("P75 was %v, but expected %v", v, want)
}

if v, want := gauges["heyo.P90"], 95.0; v != want {
if v, want := gauges["heyo.P90"], int64(95); v != want {
t.Errorf("P90 was %v, but expected %v", v, want)
}

if v, want := gauges["heyo.P95"], 98.0; v != want {
if v, want := gauges["heyo.P95"], int64(98); v != want {
t.Errorf("P95 was %v, but expected %v", v, want)
}

if v, want := gauges["heyo.P99"], 100.0; v != want {
if v, want := gauges["heyo.P99"], int64(100); v != want {
t.Errorf("P99 was %v, but expected %v", v, want)
}

if v, want := gauges["heyo.P999"], 100.0; v != want {
if v, want := gauges["heyo.P999"], int64(100); v != want {
t.Errorf("P999 was %v, but expected %v", v, want)
}
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/fds.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ func getFDUsage() (uint64, error) {
}

func init() {
metrics.Gauge("FileDescriptors.Max").SetFunc(func() float64 {
metrics.Gauge("FileDescriptors.Max").SetFunc(func() int64 {
v, err := getFDLimit()
if err != nil {
return 0
}
return float64(v)
return int64(v)
})

metrics.Gauge("FileDescriptors.Used").SetFunc(func() float64 {
metrics.Gauge("FileDescriptors.Used").SetFunc(func() int64 {
v, err := getFDUsage()
if err != nil {
return 0
}
return float64(v)
return int64(v)
})
}
12 changes: 6 additions & 6 deletions runtime/memstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (msg *memStatGauges) totalPause() uint64 {
return msg.stats.PauseTotalNs
}

func (msg *memStatGauges) lastPause() float64 {
return float64(msg.stats.LastGC)
func (msg *memStatGauges) lastPause() int64 {
return int64(msg.stats.LastGC)
}

func (msg *memStatGauges) alloc() float64 {
return float64(msg.stats.Alloc)
func (msg *memStatGauges) alloc() int64 {
return int64(msg.stats.Alloc)
}

func (msg *memStatGauges) objects() float64 {
return float64(msg.stats.HeapObjects)
func (msg *memStatGauges) objects() int64 {
return int64(msg.stats.HeapObjects)
}

0 comments on commit b55884e

Please sign in to comment.