@@ -43,6 +43,7 @@ import (
43
43
"fmt"
44
44
"io"
45
45
"net/http"
46
+ "sort"
46
47
47
48
"github.com/kylelemons/godebug/diff"
48
49
dto "github.com/prometheus/client_model/go"
@@ -251,6 +252,7 @@ func CollectAndFormat(c prometheus.Collector, format expfmt.FormatType, metricNa
251
252
// dto.MetricFamily.
252
253
func convertReaderToMetricFamily (reader io.Reader ) ([]* dto.MetricFamily , error ) {
253
254
var tp expfmt.TextParser
255
+ tp .IncludeEmptyMetricFamilies (true )
254
256
notNormalized , err := tp .TextToMetricFamilies (reader )
255
257
if err != nil {
256
258
return nil , fmt .Errorf ("converting reader to metric families failed: %w" , err )
@@ -270,7 +272,27 @@ func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error)
270
272
}
271
273
}
272
274
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
274
296
}
275
297
276
298
// compareMetricFamilies would compare 2 slices of metric families, and optionally filters both of
0 commit comments