@@ -138,20 +138,13 @@ func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ...
138
138
}
139
139
want := internal .NormalizeMetricFamilies (wantRaw )
140
140
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 )
147
- }
148
- }
149
- return nil
141
+ return compare (got , want )
150
142
}
151
143
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 {
144
+ // compare encodes both provided slices of metric families into the
145
+ // text format compares their string message and returns an error if they do not match.
146
+ // In case of error, prints a readable error message from the result.
147
+ func compare (got , want []* dto.MetricFamily ) error {
155
148
var gotBuf , wantBuf bytes.Buffer
156
149
enc := expfmt .NewEncoder (& gotBuf , expfmt .FmtText )
157
150
for _ , mf := range got {
@@ -166,7 +159,8 @@ func notMatchingError(got, want []*dto.MetricFamily) error {
166
159
}
167
160
}
168
161
169
- return fmt .Errorf (`
162
+ if wantBuf .String () != gotBuf .String () {
163
+ return fmt .Errorf (`
170
164
metric output does not match expectation; want:
171
165
172
166
%s
175
169
176
170
%s
177
171
` , wantBuf .String (), gotBuf .String ())
172
+
173
+ }
174
+ return nil
178
175
}
179
176
180
177
func filterMetrics (metrics []* dto.MetricFamily , names []string ) []* dto.MetricFamily {
0 commit comments