Skip to content

Commit 7df9e2d

Browse files
committed
testutil: include MetricFamilies without timeseries
Signed-off-by: Damien Grisonnet <[email protected]>
1 parent a5a828e commit 7df9e2d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

prometheus/testutil/testutil.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"fmt"
4444
"io"
4545
"net/http"
46+
"sort"
4647

4748
"github.com/kylelemons/godebug/diff"
4849
dto "github.com/prometheus/client_model/go"
@@ -251,6 +252,7 @@ func CollectAndFormat(c prometheus.Collector, format expfmt.FormatType, metricNa
251252
// dto.MetricFamily.
252253
func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error) {
253254
var tp expfmt.TextParser
255+
tp.IncludeEmptyMetricFamilies(true)
254256
notNormalized, err := tp.TextToMetricFamilies(reader)
255257
if err != nil {
256258
return nil, fmt.Errorf("converting reader to metric families failed: %w", err)
@@ -270,7 +272,27 @@ func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error)
270272
}
271273
}
272274

273-
return internal.NormalizeMetricFamilies(notNormalized), nil
275+
a := normalizeMetricFamilies(notNormalized)
276+
return a, nil
277+
}
278+
279+
// normalizeMetricFamilies is a copy of Prometheus
280+
// internal.NormalizeMetricFamilies, but it keeps MetricFamily without
281+
// timeseries.
282+
func normalizeMetricFamilies(metricFamiliesByName map[string]*dto.MetricFamily) []*dto.MetricFamily {
283+
for _, mf := range metricFamiliesByName {
284+
sort.Sort(internal.MetricSorter(mf.Metric))
285+
}
286+
names := make([]string, 0, len(metricFamiliesByName))
287+
for name := range metricFamiliesByName {
288+
names = append(names, name)
289+
}
290+
sort.Strings(names)
291+
result := make([]*dto.MetricFamily, 0, len(names))
292+
for _, name := range names {
293+
result = append(result, metricFamiliesByName[name])
294+
}
295+
return result
274296
}
275297

276298
// compareMetricFamilies would compare 2 slices of metric families, and optionally filters both of

0 commit comments

Comments
 (0)