forked from mirror/client_golang
Merge pull request #259 from prometheus/beorn7/guideline-compliance
Bring Gauges and Counters in line with client library guidelines
This commit is contained in:
commit
87297c8d5c
|
@ -30,7 +30,8 @@ type Counter interface {
|
||||||
Metric
|
Metric
|
||||||
Collector
|
Collector
|
||||||
|
|
||||||
// Inc increments the counter by 1.
|
// Inc increments the counter by 1. Use Add to increment it by arbitrary
|
||||||
|
// non-negative values.
|
||||||
Inc()
|
Inc()
|
||||||
// Add adds the given value to the counter. It panics if the value is <
|
// Add adds the given value to the counter. It panics if the value is <
|
||||||
// 0.
|
// 0.
|
||||||
|
|
|
@ -27,16 +27,21 @@ type Gauge interface {
|
||||||
|
|
||||||
// Set sets the Gauge to an arbitrary value.
|
// Set sets the Gauge to an arbitrary value.
|
||||||
Set(float64)
|
Set(float64)
|
||||||
// Inc increments the Gauge by 1.
|
// Inc increments the Gauge by 1. Use Add to increment it by arbitrary
|
||||||
|
// values.
|
||||||
Inc()
|
Inc()
|
||||||
// Dec decrements the Gauge by 1.
|
// Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary
|
||||||
|
// values.
|
||||||
Dec()
|
Dec()
|
||||||
// Add adds the given value to the Gauge. (The value can be
|
// Add adds the given value to the Gauge. (The value can be negative,
|
||||||
// negative, resulting in a decrease of the Gauge.)
|
// resulting in a decrease of the Gauge.)
|
||||||
Add(float64)
|
Add(float64)
|
||||||
// Sub subtracts the given value from the Gauge. (The value can be
|
// Sub subtracts the given value from the Gauge. (The value can be
|
||||||
// negative, resulting in an increase of the Gauge.)
|
// negative, resulting in an increase of the Gauge.)
|
||||||
Sub(float64)
|
Sub(float64)
|
||||||
|
|
||||||
|
// SetToCurrentTime sets the Gauge to the current Unix time in seconds.
|
||||||
|
SetToCurrentTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GaugeOpts is an alias for Opts. See there for doc comments.
|
// GaugeOpts is an alias for Opts. See there for doc comments.
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/quick"
|
"testing/quick"
|
||||||
|
"time"
|
||||||
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
)
|
)
|
||||||
|
@ -180,3 +181,22 @@ func TestGaugeFunc(t *testing.T) {
|
||||||
t.Errorf("expected %q, got %q", expected, got)
|
t.Errorf("expected %q, got %q", expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGaugeSetCurrentTime(t *testing.T) {
|
||||||
|
g := NewGauge(GaugeOpts{
|
||||||
|
Name: "test_name",
|
||||||
|
Help: "test help",
|
||||||
|
})
|
||||||
|
g.SetToCurrentTime()
|
||||||
|
unixTime := float64(time.Now().Unix())
|
||||||
|
|
||||||
|
m := &dto.Metric{}
|
||||||
|
g.Write(m)
|
||||||
|
|
||||||
|
delta := unixTime - m.GetGauge().GetValue()
|
||||||
|
// This is just a smoke test to make sure SetToCurrentTime is not
|
||||||
|
// totally off. Tests with current time involved are hard...
|
||||||
|
if math.Abs(delta) > 5 {
|
||||||
|
t.Errorf("Gauge set to current time deviates from current time by more than 5s, delta is %f seconds", delta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -20,6 +20,11 @@ package prometheus
|
||||||
// no type information is implied.
|
// no type information is implied.
|
||||||
//
|
//
|
||||||
// To create Untyped instances, use NewUntyped.
|
// To create Untyped instances, use NewUntyped.
|
||||||
|
//
|
||||||
|
// Deprecated: The Untyped type is deprecated because it doesn't make sense in
|
||||||
|
// direct instrumentation. If you need to mirror an external metric of unknown
|
||||||
|
// type (usually while writing exporters), Use MustNewConstMetric to create an
|
||||||
|
// untyped metric instance on the fly.
|
||||||
type Untyped interface {
|
type Untyped interface {
|
||||||
Metric
|
Metric
|
||||||
Collector
|
Collector
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
|
|
||||||
|
@ -80,6 +81,10 @@ func (v *value) Set(val float64) {
|
||||||
atomic.StoreUint64(&v.valBits, math.Float64bits(val))
|
atomic.StoreUint64(&v.valBits, math.Float64bits(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *value) SetToCurrentTime() {
|
||||||
|
v.Set(float64(time.Now().UnixNano()) / 1e9)
|
||||||
|
}
|
||||||
|
|
||||||
func (v *value) Inc() {
|
func (v *value) Inc() {
|
||||||
v.Add(1)
|
v.Add(1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue