@@ -83,19 +83,16 @@ func (s MetricSorter) Less(i, j int) bool {
83
83
// MetricFamilies pruned and the remaining MetricFamilies sorted by name within
84
84
// the slice, with the contained Metrics sorted within each MetricFamily.
85
85
func NormalizeMetricFamilies (metricFamiliesByName map [string ]* dto.MetricFamily ) []* dto.MetricFamily {
86
+ result := make ([]* dto.MetricFamily , 0 , len (metricFamiliesByName ))
86
87
for _ , mf := range metricFamiliesByName {
87
- sort .Sort (MetricSorter (mf .Metric ))
88
- }
89
- names := make ([]string , 0 , len (metricFamiliesByName ))
90
- for name , mf := range metricFamiliesByName {
91
- if len (mf .Metric ) > 0 {
92
- names = append (names , name )
88
+ if len (mf .Metric ) == 0 {
89
+ continue
93
90
}
91
+ sort .Sort (MetricSorter (mf .Metric ))
92
+ result = append (result , mf )
94
93
}
95
- sort .Strings (names )
96
- result := make ([]* dto.MetricFamily , 0 , len (names ))
97
- for _ , name := range names {
98
- result = append (result , metricFamiliesByName [name ])
99
- }
94
+ sort .Slice (result , func (i , j int ) bool {
95
+ return * result [i ].Name < * result [j ].Name
96
+ })
100
97
return result
101
98
}
0 commit comments