-
Notifications
You must be signed in to change notification settings - Fork 179
/
start_test.go
309 lines (286 loc) · 8.31 KB
/
start_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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MIT
package metrics
import (
"io/ioutil"
"log"
"reflect"
"sync/atomic"
"testing"
"time"
)
func TestDefaultConfig(t *testing.T) {
conf := DefaultConfig("service")
if conf.ServiceName != "service" {
t.Fatalf("Bad name")
}
if conf.HostName == "" {
t.Fatalf("missing hostname")
}
if !conf.EnableHostname || !conf.EnableRuntimeMetrics {
t.Fatalf("expect true")
}
if conf.EnableTypePrefix {
t.Fatalf("expect false")
}
if conf.TimerGranularity != time.Millisecond {
t.Fatalf("bad granularity")
}
if conf.ProfileInterval != time.Second {
t.Fatalf("bad interval")
}
}
func Test_GlobalMetrics(t *testing.T) {
var tests = []struct {
desc string
key []string
val float32
fn func([]string, float32)
}{
{"SetGauge", []string{"test"}, 42, SetGauge},
{"EmitKey", []string{"test"}, 42, EmitKey},
{"IncrCounter", []string{"test"}, 42, IncrCounter},
{"AddSample", []string{"test"}, 42, AddSample},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
s := &MockSink{}
globalMetrics.Store(&Metrics{Config: Config{FilterDefault: true}, sink: s})
tt.fn(tt.key, tt.val)
if got, want := s.keys[0], tt.key; !reflect.DeepEqual(got, want) {
t.Fatalf("got key %s want %s", got, want)
}
if got, want := s.vals[0], tt.val; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %v want %v", got, want)
}
})
}
}
func Test_GlobalMetrics_Labels(t *testing.T) {
labels := []Label{{"a", "b"}}
var tests = []struct {
desc string
key []string
val float32
fn func([]string, float32, []Label)
labels []Label
}{
{"SetGaugeWithLabels", []string{"test"}, 42, SetGaugeWithLabels, labels},
{"IncrCounterWithLabels", []string{"test"}, 42, IncrCounterWithLabels, labels},
{"AddSampleWithLabels", []string{"test"}, 42, AddSampleWithLabels, labels},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
s := &MockSink{}
globalMetrics.Store(&Metrics{Config: Config{FilterDefault: true}, sink: s})
tt.fn(tt.key, tt.val, tt.labels)
if got, want := s.keys[0], tt.key; !reflect.DeepEqual(got, want) {
t.Fatalf("got key %s want %s", got, want)
}
if got, want := s.vals[0], tt.val; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %v want %v", got, want)
}
if got, want := s.labels[0], tt.labels; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %s want %s", got, want)
}
})
}
}
func Test_GlobalPrecisionMetrics_Labels(t *testing.T) {
labels := []Label{{"a", "b"}}
var tests = []struct {
desc string
key []string
val float64
fn func([]string, float64, []Label)
labels []Label
}{
{"SetPrecisionGaugeWithLabels", []string{"test"}, 42, SetPrecisionGaugeWithLabels, labels},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
s := &MockSink{}
globalMetrics.Store(&Metrics{Config: Config{FilterDefault: true}, sink: s})
tt.fn(tt.key, tt.val, tt.labels)
if got, want := s.keys[0], tt.key; !reflect.DeepEqual(got, want) {
t.Fatalf("got key %s want %s", got, want)
}
if got, want := s.precisionVals[0], tt.val; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %v want %v", got, want)
}
if got, want := s.labels[0], tt.labels; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %s want %s", got, want)
}
})
}
}
func Test_GlobalMetrics_DefaultLabels(t *testing.T) {
config := Config{
HostName: "host1",
ServiceName: "redis",
EnableHostnameLabel: true,
EnableServiceLabel: true,
FilterDefault: true,
}
labels := []Label{
{"host", config.HostName},
{"service", config.ServiceName},
}
var tests = []struct {
desc string
key []string
val float32
fn func([]string, float32, []Label)
labels []Label
}{
{"SetGaugeWithLabels", []string{"test"}, 42, SetGaugeWithLabels, labels},
{"IncrCounterWithLabels", []string{"test"}, 42, IncrCounterWithLabels, labels},
{"AddSampleWithLabels", []string{"test"}, 42, AddSampleWithLabels, labels},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
s := &MockSink{}
globalMetrics.Store(&Metrics{Config: config, sink: s})
tt.fn(tt.key, tt.val, nil)
if got, want := s.keys[0], tt.key; !reflect.DeepEqual(got, want) {
t.Fatalf("got key %s want %s", got, want)
}
if got, want := s.vals[0], tt.val; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %v want %v", got, want)
}
if got, want := s.labels[0], tt.labels; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %s want %s", got, want)
}
})
}
}
func Test_GlobalPrecisionMetrics_DefaultLabels(t *testing.T) {
config := Config{
HostName: "host1",
ServiceName: "redis",
EnableHostnameLabel: true,
EnableServiceLabel: true,
FilterDefault: true,
}
labels := []Label{
{"host", config.HostName},
{"service", config.ServiceName},
}
var tests = []struct {
desc string
key []string
val float64
fn func([]string, float64, []Label)
labels []Label
}{
{"SetGaugeWithLabels", []string{"test"}, 42, SetPrecisionGaugeWithLabels, labels},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
s := &MockSink{}
globalMetrics.Store(&Metrics{Config: config, sink: s})
tt.fn(tt.key, tt.val, nil)
if got, want := s.keys[0], tt.key; !reflect.DeepEqual(got, want) {
t.Fatalf("got key %s want %s", got, want)
}
if got, want := s.precisionVals[0], tt.val; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %v want %v", got, want)
}
if got, want := s.labels[0], tt.labels; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %s want %s", got, want)
}
})
}
}
func Test_GlobalMetrics_MeasureSince(t *testing.T) {
s := &MockSink{}
m := &Metrics{sink: s, Config: Config{TimerGranularity: time.Millisecond, FilterDefault: true}}
globalMetrics.Store(m)
k := []string{"test"}
now := time.Now()
MeasureSince(k, now)
if !reflect.DeepEqual(s.keys[0], k) {
t.Fatalf("key not equal")
}
if s.vals[0] > 0.1 {
t.Fatalf("val too large %v", s.vals[0])
}
labels := []Label{{"a", "b"}}
MeasureSinceWithLabels(k, now, labels)
if got, want := s.keys[1], k; !reflect.DeepEqual(got, want) {
t.Fatalf("got key %s want %s", got, want)
}
if s.vals[1] > 0.1 {
t.Fatalf("val too large %v", s.vals[0])
}
if got, want := s.labels[1], labels; !reflect.DeepEqual(got, want) {
t.Fatalf("got val %s want %s", got, want)
}
}
func Test_GlobalMetrics_UpdateFilter(t *testing.T) {
globalMetrics.Store(&Metrics{Config: Config{
AllowedPrefixes: []string{"a"},
BlockedPrefixes: []string{"b"},
AllowedLabels: []string{"1"},
BlockedLabels: []string{"2"},
}})
UpdateFilterAndLabels([]string{"c"}, []string{"d"}, []string{"3"}, []string{"4"})
m := globalMetrics.Load().(*Metrics)
if m.AllowedPrefixes[0] != "c" {
t.Fatalf("bad: %v", m.AllowedPrefixes)
}
if m.BlockedPrefixes[0] != "d" {
t.Fatalf("bad: %v", m.BlockedPrefixes)
}
if m.AllowedLabels[0] != "3" {
t.Fatalf("bad: %v", m.AllowedPrefixes)
}
if m.BlockedLabels[0] != "4" {
t.Fatalf("bad: %v", m.AllowedPrefixes)
}
if _, ok := m.allowedLabels["3"]; !ok {
t.Fatalf("bad: %v", m.allowedLabels)
}
if _, ok := m.blockedLabels["4"]; !ok {
t.Fatalf("bad: %v", m.blockedLabels)
}
}
func Test_GlobalMetrics_Shutdown(t *testing.T) {
s := &MockSink{}
m := &Metrics{sink: s}
globalMetrics.Store(m)
Shutdown()
loaded := globalMetrics.Load()
metrics, ok := loaded.(*Metrics)
if !ok {
t.Fatalf("Expected globalMetrics to contain a Metrics pointer, but found: %v", loaded)
}
if metrics == m {
t.Errorf("Calling shutdown should have replaced the Metrics struct stored in globalMetrics")
}
if !s.shutdown {
t.Errorf("Expected Shutdown to have been called on MockSink")
}
}
// Benchmark_GlobalMetrics_Direct/direct-8 5000000 278 ns/op
// Benchmark_GlobalMetrics_Direct/atomic.Value-8 5000000 235 ns/op
func Benchmark_GlobalMetrics_Direct(b *testing.B) {
log.SetOutput(ioutil.Discard)
s := &MockSink{}
m := &Metrics{sink: s}
var v atomic.Value
v.Store(m)
k := []string{"test"}
b.Run("direct", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m.IncrCounter(k, 1)
}
})
b.Run("atomic.Value", func(b *testing.B) {
for i := 0; i < b.N; i++ {
v.Load().(*Metrics).IncrCounter(k, 1)
}
})
// do something with m so that the compiler does not optimize this away
b.Logf("%d", m.lastNumGC)
}