@@ -30,32 +30,25 @@ func lessFunc(lhs, rhs *metricDiff) bool {
30
30
return lhs .Measurement < rhs .Measurement
31
31
}
32
32
33
- for i := 0 ; ; i ++ {
34
- if i >= len (lhs .Tags ) && i >= len (rhs .Tags ) {
35
- break
36
- } else if i >= len (lhs .Tags ) {
37
- return true
38
- } else if i >= len (rhs .Tags ) {
39
- return false
40
- }
33
+ lhsLen , rhsLen := len (lhs .Tags ), len (rhs .Tags )
34
+ minLen := min (lhsLen , rhsLen )
41
35
36
+ for i := 0 ; i < minLen ; i ++ {
42
37
if lhs .Tags [i ].Key != rhs .Tags [i ].Key {
43
38
return lhs .Tags [i ].Key < rhs .Tags [i ].Key
44
39
}
45
40
if lhs .Tags [i ].Value != rhs .Tags [i ].Value {
46
41
return lhs .Tags [i ].Value < rhs .Tags [i ].Value
47
42
}
48
43
}
44
+ if lhsLen != rhsLen {
45
+ return lhsLen < rhsLen
46
+ }
49
47
50
- for i := 0 ; ; i ++ {
51
- if i >= len (lhs .Fields ) && i >= len (rhs .Fields ) {
52
- break
53
- } else if i >= len (lhs .Fields ) {
54
- return true
55
- } else if i >= len (rhs .Fields ) {
56
- return false
57
- }
48
+ lhsLen , rhsLen = len (lhs .Fields ), len (rhs .Fields )
49
+ minLen = min (lhsLen , rhsLen )
58
50
51
+ for i := 0 ; i < minLen ; i ++ {
59
52
if lhs .Fields [i ].Key != rhs .Fields [i ].Key {
60
53
return lhs .Fields [i ].Key < rhs .Fields [i ].Key
61
54
}
@@ -84,38 +77,43 @@ func lessFunc(lhs, rhs *metricDiff) bool {
84
77
}
85
78
}
86
79
}
80
+ if lhsLen != rhsLen {
81
+ return lhsLen < rhsLen
82
+ }
87
83
88
84
if lhs .Type != rhs .Type {
89
85
return lhs .Type < rhs .Type
90
86
}
91
87
92
- if lhs .Time .UnixNano () != rhs .Time .UnixNano () {
93
- return lhs .Time .UnixNano () < rhs .Time .UnixNano ()
94
- }
95
-
96
- return false
88
+ return lhs .Time .UnixNano () < rhs .Time .UnixNano ()
97
89
}
98
90
99
91
func newMetricDiff (telegrafMetric telegraf.Metric ) * metricDiff {
100
92
if telegrafMetric == nil {
101
93
return nil
102
94
}
103
95
104
- m := & metricDiff {}
105
- m .Measurement = telegrafMetric .Name ()
96
+ tags := telegrafMetric .TagList ()
97
+ fields := telegrafMetric .FieldList ()
98
+
99
+ m := & metricDiff {
100
+ Measurement : telegrafMetric .Name (),
101
+ Tags : make ([]* telegraf.Tag , len (tags )),
102
+ Fields : make ([]* telegraf.Field , len (fields )),
103
+ Type : telegrafMetric .Type (),
104
+ Time : telegrafMetric .Time (),
105
+ }
106
+
107
+ copy (m .Tags , tags )
108
+ copy (m .Fields , fields )
106
109
107
- m .Tags = append (m .Tags , telegrafMetric .TagList ()... )
108
110
sort .Slice (m .Tags , func (i , j int ) bool {
109
111
return m .Tags [i ].Key < m .Tags [j ].Key
110
112
})
111
-
112
- m .Fields = append (m .Fields , telegrafMetric .FieldList ()... )
113
113
sort .Slice (m .Fields , func (i , j int ) bool {
114
114
return m .Fields [i ].Key < m .Fields [j ].Key
115
115
})
116
116
117
- m .Type = telegrafMetric .Type ()
118
- m .Time = telegrafMetric .Time ()
119
117
return m
120
118
}
121
119
@@ -124,27 +122,32 @@ func newMetricStructureDiff(telegrafMetric telegraf.Metric) *metricDiff {
124
122
return nil
125
123
}
126
124
127
- m := & metricDiff {}
128
- m . Measurement = telegrafMetric .Name ()
125
+ tags := telegrafMetric . TagList ()
126
+ fields : = telegrafMetric .FieldList ()
129
127
130
- m .Tags = append (m .Tags , telegrafMetric .TagList ()... )
131
- sort .Slice (m .Tags , func (i , j int ) bool {
132
- return m .Tags [i ].Key < m .Tags [j ].Key
133
- })
128
+ m := & metricDiff {
129
+ Measurement : telegrafMetric .Name (),
130
+ Tags : make ([]* telegraf.Tag , len (tags )),
131
+ Fields : make ([]* telegraf.Field , len (fields )),
132
+ Type : telegrafMetric .Type (),
133
+ Time : telegrafMetric .Time (),
134
+ }
134
135
135
- for _ , f := range telegrafMetric .FieldList () {
136
- sf := & telegraf.Field {
136
+ copy (m .Tags , tags )
137
+ for i , f := range fields {
138
+ m .Fields [i ] = & telegraf.Field {
137
139
Key : f .Key ,
138
140
Value : reflect .Zero (reflect .TypeOf (f .Value )).Interface (),
139
141
}
140
- m .Fields = append (m .Fields , sf )
141
142
}
143
+
144
+ sort .Slice (m .Tags , func (i , j int ) bool {
145
+ return m .Tags [i ].Key < m .Tags [j ].Key
146
+ })
142
147
sort .Slice (m .Fields , func (i , j int ) bool {
143
148
return m .Fields [i ].Key < m .Fields [j ].Key
144
149
})
145
150
146
- m .Type = telegrafMetric .Type ()
147
- m .Time = telegrafMetric .Time ()
148
151
return m
149
152
}
150
153
@@ -369,23 +372,23 @@ func MustMetric(
369
372
tm time.Time ,
370
373
tp ... telegraf.ValueType ,
371
374
) telegraf.Metric {
372
- m := metric .New (name , tags , fields , tm , tp ... )
373
- return m
375
+ return metric .New (name , tags , fields , tm , tp ... )
374
376
}
375
377
376
378
func FromTestMetric (met * Metric ) telegraf.Metric {
377
- m := metric .New (met .Measurement , met .Tags , met .Fields , met .Time , met .Type )
378
- return m
379
+ return metric .New (met .Measurement , met .Tags , met .Fields , met .Time , met .Type )
379
380
}
380
381
381
382
func ToTestMetric (tm telegraf.Metric ) * Metric {
382
- tags := make (map [string ]string , len (tm .TagList ()))
383
- for _ , t := range tm .TagList () {
383
+ tagList := tm .TagList ()
384
+ tags := make (map [string ]string , len (tagList ))
385
+ for _ , t := range tagList {
384
386
tags [t .Key ] = t .Value
385
387
}
386
388
387
- fields := make (map [string ]interface {}, len (tm .FieldList ()))
388
- for _ , f := range tm .FieldList () {
389
+ fieldList := tm .FieldList ()
390
+ fields := make (map [string ]interface {}, len (fieldList ))
391
+ for _ , f := range fieldList {
389
392
fields [f .Key ] = f .Value
390
393
}
391
394
0 commit comments