Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NormalizeMetricFamilies: Optimize memory usage #1394

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 8 additions & 11 deletions prometheus/internal/metric.go
Expand Up @@ -83,19 +83,16 @@ func (s MetricSorter) Less(i, j int) bool {
// MetricFamilies pruned and the remaining MetricFamilies sorted by name within
// the slice, with the contained Metrics sorted within each MetricFamily.
func NormalizeMetricFamilies(metricFamiliesByName map[string]*dto.MetricFamily) []*dto.MetricFamily {
result := make([]*dto.MetricFamily, 0, len(metricFamiliesByName))
for _, mf := range metricFamiliesByName {
sort.Sort(MetricSorter(mf.Metric))
}
names := make([]string, 0, len(metricFamiliesByName))
for name, mf := range metricFamiliesByName {
if len(mf.Metric) > 0 {
names = append(names, name)
if len(mf.Metric) == 0 {
continue
}
sort.Sort(MetricSorter(mf.Metric))
result = append(result, mf)
}
sort.Strings(names)
result := make([]*dto.MetricFamily, 0, len(names))
for _, name := range names {
result = append(result, metricFamiliesByName[name])
}
sort.Slice(result, func(i, j int) bool {
return *result[i].Name < *result[j].Name
ethanvc marked this conversation as resolved.
Show resolved Hide resolved
})
return result
}