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

Function convertReaderToMetricFamily

prometheus/testutil/testutil.go:264–286  ·  view source on GitHub ↗

convertReaderToMetricFamily would read from a io.Reader object and convert it to a slice of dto.MetricFamily.

(reader io.Reader)

Source from the content-addressed store, hash-verified

262// convertReaderToMetricFamily would read from a io.Reader object and convert it to a slice of
263// dto.MetricFamily.
264func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error) {
265 tp := expfmt.NewTextParser(model.UTF8Validation)
266 notNormalized, err := tp.TextToMetricFamilies(reader)
267 if err != nil {
268 return nil, fmt.Errorf("converting reader to metric families failed: %w", err)
269 }
270
271 // The text protocol handles empty help fields inconsistently. When
272 // encoding, any non-nil value, include the empty string, produces a
273 // "# HELP" line. But when decoding, the help field is only set to a
274 // non-nil value if the "# HELP" line contains a non-empty value.
275 //
276 // Because metrics in a registry always have non-nil help fields, populate
277 // any nil help fields in the parsed metrics with the empty string so that
278 // when we compare text encodings, the results are consistent.
279 for _, metric := range notNormalized {
280 if metric.Help == nil {
281 metric.Help = proto.String("")
282 }
283 }
284
285 return internal.NormalizeMetricFamilies(notNormalized), nil
286}
287
288// compareMetricFamilies would compare 2 slices of metric families, and optionally filters both of
289// them to the `metricNames` provided.

Callers 2

ScrapeAndCompareFunction · 0.85

Calls 2

NormalizeMetricFamiliesFunction · 0.92
StringMethod · 0.45

Tested by

no test coverage detected