MCPcopy
hub / github.com/prometheus/client_golang / compare

Function compare

prometheus/testutil/testutil.go:303–321  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

301// The error contains the encoded text of both the desired and the actual
302// result.
303func 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
323func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {
324 var filtered []*dto.MetricFamily

Callers 1

compareMetricFamiliesFunction · 0.85

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected