4 | 4 |
"fmt"
|
5 | 5 |
"testing"
|
6 | 6 |
|
|
7 |
"github.com/go-kit/kit/metrics"
|
7 | 8 |
"github.com/go-kit/kit/metrics/expvar"
|
8 | 9 |
"github.com/go-kit/kit/metrics/teststat"
|
9 | 10 |
)
|
10 | 11 |
|
11 | 12 |
func TestHistogramQuantiles(t *testing.T) {
|
12 | |
metricName := "test_histogram"
|
13 | |
quantiles := []int{50, 90, 95, 99}
|
14 | |
h := expvar.NewHistogram(metricName, 0, 100, 3, quantiles...)
|
15 | |
|
|
13 |
var (
|
|
14 |
name = "test_histogram"
|
|
15 |
quantiles = []int{50, 90, 95, 99}
|
|
16 |
h = expvar.NewHistogram(name, 0, 100, 3, quantiles...).With(metrics.Field{Key: "ignored", Value: "field"})
|
|
17 |
)
|
16 | 18 |
const seed, mean, stdev int64 = 424242, 50, 10
|
17 | 19 |
teststat.PopulateNormalHistogram(t, h, seed, mean, stdev)
|
18 | |
teststat.AssertExpvarNormalHistogram(t, metricName, mean, stdev, quantiles)
|
|
20 |
teststat.AssertExpvarNormalHistogram(t, name, mean, stdev, quantiles)
|
19 | 21 |
}
|
20 | 22 |
|
21 | 23 |
func TestCallbackGauge(t *testing.T) {
|
22 | |
value := 42.43
|
23 | |
metricName := "foo"
|
24 | |
expvar.PublishCallbackGauge(metricName, func() float64 { return value })
|
25 | |
if want, have := fmt.Sprint(value), stdexpvar.Get(metricName).String(); want != have {
|
|
24 |
var (
|
|
25 |
name = "foo"
|
|
26 |
value = 42.43
|
|
27 |
)
|
|
28 |
expvar.PublishCallbackGauge(name, func() float64 { return value })
|
|
29 |
if want, have := fmt.Sprint(value), stdexpvar.Get(name).String(); want != have {
|
26 | 30 |
t.Errorf("want %q, have %q", want, have)
|
27 | 31 |
}
|
28 | 32 |
}
|
|
33 |
|
|
34 |
func TestCounter(t *testing.T) {
|
|
35 |
var (
|
|
36 |
name = "m"
|
|
37 |
value = 123
|
|
38 |
)
|
|
39 |
expvar.NewCounter(name).With(metrics.Field{Key: "ignored", Value: "field"}).Add(uint64(value))
|
|
40 |
if want, have := fmt.Sprint(value), stdexpvar.Get(name).String(); want != have {
|
|
41 |
t.Errorf("want %q, have %q", want, have)
|
|
42 |
}
|
|
43 |
}
|
|
44 |
|
|
45 |
func TestGauge(t *testing.T) {
|
|
46 |
var (
|
|
47 |
name = "xyz"
|
|
48 |
value = 54321
|
|
49 |
delta = 12345
|
|
50 |
g = expvar.NewGauge(name).With(metrics.Field{Key: "ignored", Value: "field"})
|
|
51 |
)
|
|
52 |
g.Set(float64(value))
|
|
53 |
g.Add(float64(delta))
|
|
54 |
if want, have := fmt.Sprint(value+delta), stdexpvar.Get(name).String(); want != have {
|
|
55 |
t.Errorf("want %q, have %q", want, have)
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
func TestInvalidQuantile(t *testing.T) {
|
|
60 |
defer func() {
|
|
61 |
if err := recover(); err == nil {
|
|
62 |
t.Errorf("expected panic, got none")
|
|
63 |
} else {
|
|
64 |
t.Logf("got expected panic: %v", err)
|
|
65 |
}
|
|
66 |
}()
|
|
67 |
expvar.NewHistogram("foo", 0.0, 100.0, 3, 50, 90, 95, 99, 101)
|
|
68 |
}
|