@@ -37,7 +37,6 @@ import (
37
37
"bytes"
38
38
"fmt"
39
39
"io"
40
- "reflect"
41
40
42
41
"github.com/prometheus/common/expfmt"
43
42
@@ -125,49 +124,57 @@ func CollectAndCompare(c prometheus.Collector, expected io.Reader, metricNames .
125
124
// exposition format. If any metricNames are provided, only metrics with those
126
125
// names are compared.
127
126
func GatherAndCompare (g prometheus.Gatherer , expected io.Reader , metricNames ... string ) error {
128
- metrics , err := g .Gather ()
127
+ got , err := g .Gather ()
129
128
if err != nil {
130
129
return fmt .Errorf ("gathering metrics failed: %s" , err )
131
130
}
132
131
if metricNames != nil {
133
- metrics = filterMetrics (metrics , metricNames )
132
+ got = filterMetrics (got , metricNames )
134
133
}
135
134
var tp expfmt.TextParser
136
- expectedMetrics , err := tp .TextToMetricFamilies (expected )
135
+ wantRaw , err := tp .TextToMetricFamilies (expected )
137
136
if err != nil {
138
137
return fmt .Errorf ("parsing expected metrics failed: %s" , err )
139
138
}
139
+ want := internal .NormalizeMetricFamilies (wantRaw )
140
140
141
- if ! reflect .DeepEqual (metrics , internal .NormalizeMetricFamilies (expectedMetrics )) {
142
- // Encode the gathered output to the readable text format for comparison.
143
- var buf1 bytes.Buffer
144
- enc := expfmt .NewEncoder (& buf1 , expfmt .FmtText )
145
- for _ , mf := range metrics {
146
- if err := enc .Encode (mf ); err != nil {
147
- return fmt .Errorf ("encoding result failed: %s" , err )
148
- }
141
+ if len (got ) != len (want ) {
142
+ return notMatchingError (got , want )
143
+ }
144
+ for i := range got {
145
+ if got [i ].String () != want [i ].String () {
146
+ return notMatchingError (got , want )
149
147
}
150
- // Encode normalized expected metrics again to generate them in the same ordering
151
- // the registry does to spot differences more easily.
152
- var buf2 bytes.Buffer
153
- enc = expfmt .NewEncoder (& buf2 , expfmt .FmtText )
154
- for _ , mf := range internal .NormalizeMetricFamilies (expectedMetrics ) {
155
- if err := enc .Encode (mf ); err != nil {
156
- return fmt .Errorf ("encoding result failed: %s" , err )
157
- }
148
+ }
149
+ return nil
150
+ }
151
+
152
+ // notMatchingError encodes both provided slices of metric families into the
153
+ // text format and creates a readable error message from the result.
154
+ func notMatchingError (got , want []* dto.MetricFamily ) error {
155
+ var gotBuf , wantBuf bytes.Buffer
156
+ enc := expfmt .NewEncoder (& gotBuf , expfmt .FmtText )
157
+ for _ , mf := range got {
158
+ if err := enc .Encode (mf ); err != nil {
159
+ return fmt .Errorf ("encoding gathered metrics failed: %s" , err )
160
+ }
161
+ }
162
+ enc = expfmt .NewEncoder (& wantBuf , expfmt .FmtText )
163
+ for _ , mf := range want {
164
+ if err := enc .Encode (mf ); err != nil {
165
+ return fmt .Errorf ("encoding expected metrics failed: %s" , err )
158
166
}
167
+ }
159
168
160
- return fmt .Errorf (`
169
+ return fmt .Errorf (`
161
170
metric output does not match expectation; want:
162
171
163
172
%s
164
173
165
174
got:
166
175
167
176
%s
168
- ` , buf2 .String (), buf1 .String ())
169
- }
170
- return nil
177
+ ` , wantBuf .String (), gotBuf .String ())
171
178
}
172
179
173
180
func filterMetrics (metrics []* dto.MetricFamily , names []string ) []* dto.MetricFamily {
0 commit comments