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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metric
import (
"testing"
"google.golang.org/protobuf/proto"
"gvisor.dev/gvisor/pkg/eventchannel"
pb "gvisor.dev/gvisor/pkg/metric/metric_go_proto"
)
// sliceEmitter implements eventchannel.Emitter by appending all messages to a
// slice.
type sliceEmitter []proto.Message
// Emit implements eventchannel.Emitter.Emit.
func (s *sliceEmitter) Emit(msg proto.Message) (bool, error) {
*s = append(*s, msg)
return false, nil
}
// Emit implements eventchannel.Emitter.Close.
func (s *sliceEmitter) Close() error {
return nil
}
// Reset clears all events in s.
func (s *sliceEmitter) Reset() {
*s = nil
}
// emitter is the eventchannel.Emitter used for all tests. Package eventchannel
// doesn't allow removing Emitters, so we must use one global emitter for all
// test cases.
var emitter sliceEmitter
func init() {
eventchannel.AddEmitter(&emitter)
}
// reset clears all global state in the metric package.
func reset() {
initialized = false
allMetrics = makeMetricSet()
emitter.Reset()
}
const (
fooDescription = "Foo!"
barDescription = "Bar Baz"
counterDescription = "Counter"
)
func TestInitialize(t *testing.T) {
defer reset()
_, err := NewUint64Metric("/foo", false, pb.MetricMetadata_UNITS_NONE, fooDescription)
if err != nil {
t.Fatalf("NewUint64Metric got err %v want nil", err)
}
_, err = NewUint64Metric("/bar", true, pb.MetricMetadata_UNITS_NANOSECONDS, barDescription)
if err != nil {
t.Fatalf("NewUint64Metric got err %v want nil", err)
}
Initialize()
if len(emitter) != 1 {
t.Fatalf("Initialize emitted %d events want 1", len(emitter))
}
mr, ok := emitter[0].(*pb.MetricRegistration)
if !ok {
t.Fatalf("emitter %v got %T want pb.MetricRegistration", emitter[0], emitter[0])
}
if len(mr.Metrics) != 2 {
t.Errorf("MetricRegistration got %d metrics want 2", len(mr.Metrics))
}
foundFoo := false
foundBar := false
for _, m := range mr.Metrics {
if m.Type != pb.MetricMetadata_TYPE_UINT64 {
t.Errorf("Metadata %+v Type got %v want pb.MetricMetadata_TYPE_UINT64", m, m.Type)
}
if !m.Cumulative {
t.Errorf("Metadata %+v Cumulative got false want true", m)
}
switch m.Name {
case "/foo":
foundFoo = true
if m.Description != fooDescription {
t.Errorf("/foo %+v Description got %q want %q", m, m.Description, fooDescription)
}
if m.Sync {
t.Errorf("/foo %+v Sync got true want false", m)
}
if m.Units != pb.MetricMetadata_UNITS_NONE {
t.Errorf("/foo %+v Units got %v want %v", m, m.Units, pb.MetricMetadata_UNITS_NONE)
}
case "/bar":
foundBar = true
if m.Description != barDescription {
t.Errorf("/bar %+v Description got %q want %q", m, m.Description, barDescription)
}
if !m.Sync {
t.Errorf("/bar %+v Sync got true want false", m)
}
if m.Units != pb.MetricMetadata_UNITS_NANOSECONDS {
t.Errorf("/bar %+v Units got %v want %v", m, m.Units, pb.MetricMetadata_UNITS_NANOSECONDS)
}
}
}
if !foundFoo {
t.Errorf("/foo not found: %+v", emitter)
}
if !foundBar {
t.Errorf("/bar not found: %+v", emitter)
}
}
func TestDisable(t *testing.T) {
defer reset()
_, err := NewUint64Metric("/foo", false, pb.MetricMetadata_UNITS_NONE, fooDescription)
if err != nil {
t.Fatalf("NewUint64Metric got err %v want nil", err)
}
_, err = NewUint64Metric("/bar", true, pb.MetricMetadata_UNITS_NONE, barDescription)
if err != nil {
t.Fatalf("NewUint64Metric got err %v want nil", err)
}
Disable()
if len(emitter) != 1 {
t.Fatalf("Initialize emitted %d events want 1", len(emitter))
}
mr, ok := emitter[0].(*pb.MetricRegistration)
if !ok {
t.Fatalf("emitter %v got %T want pb.MetricRegistration", emitter[0], emitter[0])
}
if len(mr.Metrics) != 0 {
t.Errorf("MetricRegistration got %d metrics want 0", len(mr.Metrics))
}
}
func TestEmitMetricUpdate(t *testing.T) {
defer reset()
foo, err := NewUint64Metric("/foo", false, pb.MetricMetadata_UNITS_NONE, fooDescription)
if err != nil {
t.Fatalf("NewUint64Metric got err %v want nil", err)
}
_, err = NewUint64Metric("/bar", true, pb.MetricMetadata_UNITS_NONE, barDescription)
if err != nil {
t.Fatalf("NewUint64Metric got err %v want nil", err)
}
Initialize()
// Don't care about the registration metrics.
emitter.Reset()
EmitMetricUpdate()
if len(emitter) != 1 {
t.Fatalf("EmitMetricUpdate emitted %d events want 1", len(emitter))
}
update, ok := emitter[0].(*pb.MetricUpdate)
if !ok {
t.Fatalf("emitter %v got %T want pb.MetricUpdate", emitter[0], emitter[0])
}
if len(update.Metrics) != 2 {
t.Errorf("MetricUpdate got %d metrics want 2", len(update.Metrics))
}
// Both are included for their initial values.
foundFoo := false
foundBar := false
for _, m := range update.Metrics {
switch m.Name {
case "/foo":
foundFoo = true
case "/bar":
foundBar = true
}
uv, ok := m.Value.(*pb.MetricValue_Uint64Value)
if !ok {
t.Errorf("%+v: value %v got %T want pb.MetricValue_Uint64Value", m, m.Value, m.Value)
continue
}
if uv.Uint64Value != 0 {
t.Errorf("%v: Value got %v want 0", m, uv.Uint64Value)
}
}
if !foundFoo {
t.Errorf("/foo not found: %+v", emitter)
}
if !foundBar {
t.Errorf("/bar not found: %+v", emitter)
}
// Increment foo. Only it is included in the next update.
foo.Increment()
emitter.Reset()
EmitMetricUpdate()
if len(emitter) != 1 {
t.Fatalf("EmitMetricUpdate emitted %d events want 1", len(emitter))
}
update, ok = emitter[0].(*pb.MetricUpdate)
if !ok {
t.Fatalf("emitter %v got %T want pb.MetricUpdate", emitter[0], emitter[0])
}
if len(update.Metrics) != 1 {
t.Errorf("MetricUpdate got %d metrics want 1", len(update.Metrics))
}
m := update.Metrics[0]
if m.Name != "/foo" {
t.Errorf("Metric %+v name got %q want '/foo'", m, m.Name)
}
uv, ok := m.Value.(*pb.MetricValue_Uint64Value)
if !ok {
t.Errorf("%+v: value %v got %T want pb.MetricValue_Uint64Value", m, m.Value, m.Value)
}
if uv.Uint64Value != 1 {
t.Errorf("%v: Value got %v want 1", m, uv.Uint64Value)
}
}
func TestEmitMetricUpdateWithFields(t *testing.T) {
defer reset()
field := Field{
name: "weirdness_type",
allowedValues: []string{"weird1", "weird2"}}
counter, err := NewUint64Metric("/weirdness", false, pb.MetricMetadata_UNITS_NONE, counterDescription, field)
if err != nil {
t.Fatalf("NewUint64Metric got err %v want nil", err)
}
Initialize()
// Don't care about the registration metrics.
emitter.Reset()
EmitMetricUpdate()
// For metrics with fields, we do not emit data unless the value is
// incremented.
if len(emitter) != 0 {
t.Fatalf("EmitMetricUpdate emitted %d events want 0", len(emitter))
}
counter.IncrementBy(4, "weird1")
counter.Increment("weird2")
emitter.Reset()
EmitMetricUpdate()
if len(emitter) != 1 {
t.Fatalf("EmitMetricUpdate emitted %d events want 1", len(emitter))
}
update, ok := emitter[0].(*pb.MetricUpdate)
if !ok {
t.Fatalf("emitter %v got %T want pb.MetricUpdate", emitter[0], emitter[0])
}
if len(update.Metrics) != 2 {
t.Errorf("MetricUpdate got %d metrics want 2", len(update.Metrics))
}
foundWeird1 := false
foundWeird2 := false
for i := 0; i < len(update.Metrics); i++ {
m := update.Metrics[i]
if m.Name != "/weirdness" {
t.Errorf("Metric %+v name got %q want '/weirdness'", m, m.Name)
}
if len(m.FieldValues) != 1 {
t.Errorf("MetricUpdate got %d fields want 1", len(m.FieldValues))
}
switch m.FieldValues[0] {
case "weird1":
uv, ok := m.Value.(*pb.MetricValue_Uint64Value)
if !ok {
t.Errorf("%+v: value %v got %T want pb.MetricValue_Uint64Value", m, m.Value, m.Value)
}
if uv.Uint64Value != 4 {
t.Errorf("%v: Value got %v want 4", m, uv.Uint64Value)
}
foundWeird1 = true
case "weird2":
uv, ok := m.Value.(*pb.MetricValue_Uint64Value)
if !ok {
t.Errorf("%+v: value %v got %T want pb.MetricValue_Uint64Value", m, m.Value, m.Value)
}
if uv.Uint64Value != 1 {
t.Errorf("%v: Value got %v want 1", m, uv.Uint64Value)
}
foundWeird2 = true
}
}
if !foundWeird1 {
t.Errorf("Field value weird1 not found: %+v", emitter)
}
if !foundWeird2 {
t.Errorf("Field value weird2 not found: %+v", emitter)
}
}
|