@@ -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.
252253func 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