forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
205 lines (174 loc) · 4.59 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
package testutil
import (
"reflect"
"sort"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
)
type metricDiff struct {
Measurement string
Tags []*telegraf.Tag
Fields []*telegraf.Field
Type telegraf.ValueType
Time time.Time
}
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()
for _, tag := range metric.TagList() {
m.Tags = append(m.Tags, tag)
}
sort.Slice(m.Tags, func(i, j int) bool {
return m.Tags[i].Key < m.Tags[j].Key
})
for _, field := range metric.FieldList() {
m.Fields = append(m.Fields, field)
}
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")
}
// 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.T, expected, actual telegraf.Metric, opts ...cmp.Option) {
t.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.T, expected, actual []telegraf.Metric, opts ...cmp.Option) {
t.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)
}
}
// Metric creates a new metric or panics on error.
func MustMetric(
name string,
tags map[string]string,
fields map[string]interface{},
tm time.Time,
tp ...telegraf.ValueType,
) telegraf.Metric {
m, err := metric.New(name, tags, fields, tm, tp...)
if err != nil {
panic("MustMetric")
}
return m
}
func FromTestMetric(met *Metric) telegraf.Metric {
m, err := metric.New(met.Measurement, met.Tags, met.Fields, met.Time, met.Type)
if err != nil {
panic("MustMetric")
}
return m
}