convertReaderToMetricFamily would read from a io.Reader object and convert it to a slice of dto.MetricFamily.
(reader io.Reader)
| 262 | // convertReaderToMetricFamily would read from a io.Reader object and convert it to a slice of |
| 263 | // dto.MetricFamily. |
| 264 | func 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. |
no test coverage detected