-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
metric_test.go
263 lines (231 loc) · 6.68 KB
/
metric_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package metric
import (
"bytes"
"encoding/json"
"math"
"reflect"
"sync"
"testing"
"time"
_ "github.com/cockroachdb/cockroach/pkg/util/log" // for flags
"github.com/kr/pretty"
"github.com/prometheus/client_golang/prometheus"
prometheusgo "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)
func testMarshal(t *testing.T, m json.Marshaler, exp string) {
if b, err := m.MarshalJSON(); err != nil || !bytes.Equal(b, []byte(exp)) {
t.Fatalf("unexpected: err=%v\nbytes=%s\nwanted=%s\nfor:\n%+v", err, b, exp, m)
}
}
var emptyMetadata = Metadata{Name: ""}
func TestGauge(t *testing.T) {
g := NewGauge(emptyMetadata)
g.Update(10)
if v := g.Value(); v != 10 {
t.Fatalf("unexpected value: %d", v)
}
var wg sync.WaitGroup
for i := int64(0); i < 10; i++ {
wg.Add(2)
go func(i int64) { g.Inc(i); wg.Done() }(i)
go func(i int64) { g.Dec(i); wg.Done() }(i)
}
wg.Wait()
if v := g.Value(); v != 10 {
t.Fatalf("unexpected value: %d", v)
}
testMarshal(t, g, "10")
}
func TestFunctionalGauge(t *testing.T) {
valToReturn := int64(10)
g := NewFunctionalGauge(emptyMetadata, func() int64 { return valToReturn })
if v := g.Value(); v != 10 {
t.Fatalf("unexpected value: %d", v)
}
valToReturn = 15
if v := g.Value(); v != 15 {
t.Fatalf("unexpected value: %d", v)
}
}
func TestGaugeFloat64(t *testing.T) {
g := NewGaugeFloat64(emptyMetadata)
g.Update(10.4)
if v := g.Value(); v != 10.4 {
t.Fatalf("unexpected value: %f", v)
}
testMarshal(t, g, "10.4")
var wg sync.WaitGroup
for i := int64(0); i < 10; i++ {
wg.Add(2)
go func(i int64) { g.Inc(float64(i)); wg.Done() }(i)
go func(i int64) { g.Dec(float64(i)); wg.Done() }(i)
}
wg.Wait()
if v := g.Value(); math.Abs(v-10.4) > 0.001 {
t.Fatalf("unexpected value: %g", v)
}
}
func TestCounter(t *testing.T) {
c := NewCounter(emptyMetadata)
c.Inc(90)
if v := c.Count(); v != 90 {
t.Fatalf("unexpected value: %d", v)
}
testMarshal(t, c, "90")
}
func setNow(d time.Duration) {
now = func() time.Time {
return time.Time{}.Add(d)
}
}
func TestHistogram(t *testing.T) {
u := func(v int) *uint64 {
n := uint64(v)
return &n
}
f := func(v int) *float64 {
n := float64(v)
return &n
}
h := NewHistogram(HistogramOptions{
ForceUsePrometheus: true,
Metadata: Metadata{},
Duration: time.Hour,
Buckets: []float64{
1.0,
5.0,
10.0,
25.0,
100.0,
},
})
// should return 0 if no observations are made
require.Equal(t, 0.0, h.ValueAtQuantileWindowed(0))
// 200 is intentionally set us the first value to verify that the function
// does not return NaN or Inf.
measurements := []int64{200, 0, 4, 5, 10, 20, 25, 30, 40, 90}
var expSum float64
for i, m := range measurements {
h.RecordValue(m)
if i == 0 {
require.Equal(t, 0.0, h.ValueAtQuantileWindowed(0))
require.Equal(t, 100.0, h.ValueAtQuantileWindowed(99))
}
expSum += float64(m)
}
act := *h.ToPrometheusMetric().Histogram
exp := prometheusgo.Histogram{
SampleCount: u(len(measurements)),
SampleSum: &expSum,
Bucket: []*prometheusgo.Bucket{
{CumulativeCount: u(1), UpperBound: f(1)},
{CumulativeCount: u(3), UpperBound: f(5)},
{CumulativeCount: u(4), UpperBound: f(10)},
{CumulativeCount: u(6), UpperBound: f(25)},
{CumulativeCount: u(9), UpperBound: f(100)},
// NB: 200 is greater than the largest defined bucket so prometheus
// puts it in an implicit bucket with +Inf as the upper bound.
},
}
if !reflect.DeepEqual(act, exp) {
t.Fatalf("expected differs from actual: %s", pretty.Diff(exp, act))
}
require.Equal(t, 0.0, h.ValueAtQuantileWindowed(0))
require.Equal(t, 1.0, h.ValueAtQuantileWindowed(10))
require.Equal(t, 17.5, h.ValueAtQuantileWindowed(50))
require.Equal(t, 75.0, h.ValueAtQuantileWindowed(80))
require.Equal(t, 100.0, h.ValueAtQuantileWindowed(99.99))
}
func TestManualWindowHistogram(t *testing.T) {
u := func(v int) *uint64 {
n := uint64(v)
return &n
}
f := func(v int) *float64 {
n := float64(v)
return &n
}
buckets := []float64{
1.0,
5.0,
10.0,
25.0,
100.0,
}
h := NewManualWindowHistogram(
Metadata{},
buckets,
)
// should return 0 if no observations are made
require.Equal(t, 0.0, h.ValueAtQuantileWindowed(0))
histogram := prometheus.NewHistogram(prometheus.HistogramOpts{Buckets: buckets})
pMetric := &prometheusgo.Metric{}
// 200 is intentionally set us the first value to verify that the function
// does not return NaN or Inf.
measurements := []float64{200, 0, 4, 5, 10, 20, 25, 30, 40, 90}
var expSum float64
for _, m := range measurements {
histogram.Observe(m)
expSum += m
}
require.NoError(t, histogram.Write(pMetric))
h.Update(histogram, pMetric.Histogram)
act := *h.ToPrometheusMetric().Histogram
exp := prometheusgo.Histogram{
SampleCount: u(len(measurements)),
SampleSum: &expSum,
Bucket: []*prometheusgo.Bucket{
{CumulativeCount: u(1), UpperBound: f(1)},
{CumulativeCount: u(3), UpperBound: f(5)},
{CumulativeCount: u(4), UpperBound: f(10)},
{CumulativeCount: u(6), UpperBound: f(25)},
{CumulativeCount: u(9), UpperBound: f(100)},
// NB: 200 is greater than the largest defined bucket so prometheus
// puts it in an implicit bucket with +Inf as the upper bound.
},
}
if !reflect.DeepEqual(act, exp) {
t.Fatalf("expected differs from actual: %s", pretty.Diff(exp, act))
}
require.Equal(t, 0.0, h.ValueAtQuantileWindowed(0))
require.Equal(t, 1.0, h.ValueAtQuantileWindowed(10))
require.Equal(t, 17.5, h.ValueAtQuantileWindowed(50))
require.Equal(t, 75.0, h.ValueAtQuantileWindowed(80))
require.Equal(t, 100.0, h.ValueAtQuantileWindowed(99.99))
}
func TestNewHistogramRotate(t *testing.T) {
defer TestingSetNow(nil)()
setNow(0)
h := NewHistogram(HistogramOptions{
ForceUsePrometheus: true,
Metadata: emptyMetadata,
Duration: 10 * time.Second,
Buckets: nil,
})
for i := 0; i < 4; i++ {
// Windowed histogram is initially empty.
h.Inspect(func(interface{}) {}) // triggers ticking
require.Zero(t, h.TotalSumWindowed())
// But cumulative histogram has history (if i > 0).
require.EqualValues(t, i, h.TotalCount())
// Add a measurement and verify it's there.
{
h.RecordValue(12345)
f := float64(12345)
require.Equal(t, h.TotalSumWindowed(), f)
}
// Tick. This rotates the histogram.
setNow(time.Duration(i+1) * 10 * time.Second)
// Go to beginning.
}
}