forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetric.go
286 lines (244 loc) · 6.64 KB
/
metric.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
package testutil
import (
"reflect"
"sort"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/influxdata/telegraf"
telegrafMetric "github.com/influxdata/telegraf/metric"
)
type metricDiff struct {
Measurement string
Tags []*telegraf.Tag
Fields []*telegraf.Field
Type telegraf.ValueType
Time time.Time
}
type helper interface {
Helper()
}
func lessFunc(lhs, rhs *metricDiff) bool {
if lhs.Measurement != rhs.Measurement {
return lhs.Measurement < rhs.Measurement
}
for i := 0; ; i++ {
if i >= len(lhs.Tags) && i >= len(rhs.Tags) {
break
} else if i >= len(lhs.Tags) {
return true
} else if i >= len(rhs.Tags) {
return false
}
if lhs.Tags[i].Key != rhs.Tags[i].Key {
return lhs.Tags[i].Key < rhs.Tags[i].Key
}
if lhs.Tags[i].Value != rhs.Tags[i].Value {
return lhs.Tags[i].Value < rhs.Tags[i].Value
}
}
for i := 0; ; i++ {
if i >= len(lhs.Fields) && i >= len(rhs.Fields) {
break
} else if i >= len(lhs.Fields) {
return true
} else if i >= len(rhs.Fields) {
return false
}
if lhs.Fields[i].Key != rhs.Fields[i].Key {
return lhs.Fields[i].Key < rhs.Fields[i].Key
}
if lhs.Fields[i].Value != rhs.Fields[i].Value {
ltype := reflect.TypeOf(lhs.Fields[i].Value)
rtype := reflect.TypeOf(lhs.Fields[i].Value)
if ltype.Kind() != rtype.Kind() {
return ltype.Kind() < rtype.Kind()
}
switch v := lhs.Fields[i].Value.(type) {
case int64:
return v < lhs.Fields[i].Value.(int64)
case uint64:
return v < lhs.Fields[i].Value.(uint64)
case float64:
return v < lhs.Fields[i].Value.(float64)
case string:
return v < lhs.Fields[i].Value.(string)
case bool:
return !v
default:
panic("unknown type")
}
}
}
if lhs.Type != rhs.Type {
return lhs.Type < rhs.Type
}
if lhs.Time.UnixNano() != rhs.Time.UnixNano() {
return lhs.Time.UnixNano() < rhs.Time.UnixNano()
}
return false
}
func newMetricDiff(metric telegraf.Metric) *metricDiff {
if metric == nil {
return nil
}
m := &metricDiff{}
m.Measurement = metric.Name()
m.Tags = append(m.Tags, metric.TagList()...)
sort.Slice(m.Tags, func(i, j int) bool {
return m.Tags[i].Key < m.Tags[j].Key
})
m.Fields = append(m.Fields, metric.FieldList()...)
sort.Slice(m.Fields, func(i, j int) bool {
return m.Fields[i].Key < m.Fields[j].Key
})
m.Type = metric.Type()
m.Time = metric.Time()
return m
}
func newMetricStructureDiff(metric telegraf.Metric) *metricDiff {
if metric == nil {
return nil
}
m := &metricDiff{}
m.Measurement = metric.Name()
m.Tags = append(m.Tags, metric.TagList()...)
sort.Slice(m.Tags, func(i, j int) bool {
return m.Tags[i].Key < m.Tags[j].Key
})
for _, f := range metric.FieldList() {
sf := &telegraf.Field{
Key: f.Key,
Value: reflect.Zero(reflect.TypeOf(f.Value)).Interface(),
}
m.Fields = append(m.Fields, sf)
}
sort.Slice(m.Fields, func(i, j int) bool {
return m.Fields[i].Key < m.Fields[j].Key
})
m.Type = metric.Type()
m.Time = metric.Time()
return m
}
// SortMetrics enables sorting metrics before comparison.
func SortMetrics() cmp.Option {
return cmpopts.SortSlices(lessFunc)
}
// IgnoreTime disables comparison of timestamp.
func IgnoreTime() cmp.Option {
return cmpopts.IgnoreFields(metricDiff{}, "Time")
}
// IgnoreFields disables comparison of the fields with the given names.
// The field-names are case-sensitive!
func IgnoreFields(names ...string) cmp.Option {
return cmpopts.IgnoreSliceElements(
func(f *telegraf.Field) bool {
for _, n := range names {
if f.Key == n {
return true
}
}
return false
},
)
}
// IgnoreTags disables comparison of the tags with the given names.
// The tag-names are case-sensitive!
func IgnoreTags(names ...string) cmp.Option {
return cmpopts.IgnoreSliceElements(
func(f *telegraf.Tag) bool {
for _, n := range names {
if f.Key == n {
return true
}
}
return false
},
)
}
// MetricEqual returns true if the metrics are equal.
func MetricEqual(expected, actual telegraf.Metric, opts ...cmp.Option) bool {
var lhs, rhs *metricDiff
if expected != nil {
lhs = newMetricDiff(expected)
}
if actual != nil {
rhs = newMetricDiff(actual)
}
opts = append(opts, cmpopts.EquateNaNs())
return cmp.Equal(lhs, rhs, opts...)
}
// RequireMetricEqual halts the test with an error if the metrics are not
// equal.
func RequireMetricEqual(t testing.TB, expected, actual telegraf.Metric, opts ...cmp.Option) {
if x, ok := t.(helper); ok {
x.Helper()
}
var lhs, rhs *metricDiff
if expected != nil {
lhs = newMetricDiff(expected)
}
if actual != nil {
rhs = newMetricDiff(actual)
}
opts = append(opts, cmpopts.EquateNaNs())
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
t.Fatalf("telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
}
// RequireMetricsEqual halts the test with an error if the array of metrics
// are not equal.
func RequireMetricsEqual(t testing.TB, expected, actual []telegraf.Metric, opts ...cmp.Option) {
if x, ok := t.(helper); ok {
x.Helper()
}
lhs := make([]*metricDiff, 0, len(expected))
for _, m := range expected {
lhs = append(lhs, newMetricDiff(m))
}
rhs := make([]*metricDiff, 0, len(actual))
for _, m := range actual {
rhs = append(rhs, newMetricDiff(m))
}
opts = append(opts, cmpopts.EquateNaNs())
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
t.Fatalf("[]telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
}
// RequireMetricsStructureEqual halts the test with an error if the array of
// metrics is structural different. Structure means that the metric differs
// in either name, tag key/values, time (if not ignored) or fields. For fields
// ONLY the name and type are compared NOT the value.
func RequireMetricsStructureEqual(t testing.TB, expected, actual []telegraf.Metric, opts ...cmp.Option) {
if x, ok := t.(helper); ok {
x.Helper()
}
lhs := make([]*metricDiff, 0, len(expected))
for _, m := range expected {
lhs = append(lhs, newMetricStructureDiff(m))
}
rhs := make([]*metricDiff, 0, len(actual))
for _, m := range actual {
rhs = append(rhs, newMetricStructureDiff(m))
}
opts = append(opts, cmpopts.EquateNaNs())
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
t.Fatalf("[]telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
}
// MustMetric creates a new metric.
func MustMetric(
name string,
tags map[string]string,
fields map[string]interface{},
tm time.Time,
tp ...telegraf.ValueType,
) telegraf.Metric {
m := telegrafMetric.New(name, tags, fields, tm, tp...)
return m
}
func FromTestMetric(met *Metric) telegraf.Metric {
m := telegrafMetric.New(met.Measurement, met.Tags, met.Fields, met.Time, met.Type)
return m
}