compare encodes both provided slices of metric families into the text format, compares their string message, and returns an error if they do not match. The error contains the encoded text of both the desired and the actual result.
(got, want []*dto.MetricFamily)
| 301 | // The error contains the encoded text of both the desired and the actual |
| 302 | // result. |
| 303 | func compare(got, want []*dto.MetricFamily) error { |
| 304 | var gotBuf, wantBuf bytes.Buffer |
| 305 | enc := expfmt.NewEncoder(&gotBuf, expfmt.NewFormat(expfmt.TypeTextPlain).WithEscapingScheme(model.NoEscaping)) |
| 306 | for _, mf := range got { |
| 307 | if err := enc.Encode(mf); err != nil { |
| 308 | return fmt.Errorf("encoding gathered metrics failed: %w", err) |
| 309 | } |
| 310 | } |
| 311 | enc = expfmt.NewEncoder(&wantBuf, expfmt.NewFormat(expfmt.TypeTextPlain).WithEscapingScheme(model.NoEscaping)) |
| 312 | for _, mf := range want { |
| 313 | if err := enc.Encode(mf); err != nil { |
| 314 | return fmt.Errorf("encoding expected metrics failed: %w", err) |
| 315 | } |
| 316 | } |
| 317 | if diffErr := diff.Diff(gotBuf.String(), wantBuf.String()); diffErr != "" { |
| 318 | return errors.New(diffErr) |
| 319 | } |
| 320 | return nil |
| 321 | } |
| 322 | |
| 323 | func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily { |
| 324 | var filtered []*dto.MetricFamily |
no test coverage detected