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

fix metric sort error #1443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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: 18 additions & 1 deletion prometheus/internal/metric.go
Expand Up @@ -14,7 +14,9 @@
package internal

import (
"regexp"
"sort"
"strconv"

dto "github.com/prometheus/client_model/go"
)
Expand Down Expand Up @@ -46,6 +48,21 @@ func (s MetricSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s MetricSorter) compare(si, sj string) bool {
// When the number of devices exceeds two digits, a sorting error occurs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very specific to node exporter, not generic client_golang use, right? client_golang is used in more places than node_exporter, so we have to care about more use cases.

// dealing with sorting problems, especially CPU sorting errors
re := regexp.MustCompile(`\d+$`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I think we can't use regex in such a hot path, we might need to be more smarter here if we ever do this ( let's talk on the issue) - or do it optionally.

if re.Match([]byte(si)) && re.Match([]byte(sj)) {
siCode := re.FindStringSubmatch(si)[0]
sjCode := re.FindStringSubmatch(sj)[0]
siValue, _ := strconv.Atoi(siCode)
sjValue, _ := strconv.Atoi(sjCode)
return siValue < sjValue
}
// Non-numeric identification device, use original sorting
return si < sj
}

func (s MetricSorter) Less(i, j int) bool {
if len(s[i].Label) != len(s[j].Label) {
// This should not happen. The metrics are
Expand All @@ -60,7 +77,7 @@ func (s MetricSorter) Less(i, j int) bool {
vi := lp.GetValue()
vj := s[j].Label[n].GetValue()
if vi != vj {
return vi < vj
return s.compare(vi, vj)
}
}

Expand Down
67 changes: 67 additions & 0 deletions prometheus/internal/metric_sort_test.go
@@ -0,0 +1,67 @@
// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal

import (
"sort"
"strings"
"testing"

"github.com/prometheus/common/expfmt"
)

func BenchmarkSort(b *testing.B) {
var parser expfmt.TextParser
text := `
# HELP node_cpu_guest_seconds_total Seconds the CPUs spent in guests (VMs) for each mode.
# TYPE node_cpu_guest_seconds_total counter
node_cpu_guest_seconds_total{cpu="0",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="0",mode="user"} 0
node_cpu_guest_seconds_total{cpu="1",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="1",mode="user"} 0
node_cpu_guest_seconds_total{cpu="10",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="10",mode="user"} 0
node_cpu_guest_seconds_total{cpu="11",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="11",mode="user"} 0
node_cpu_guest_seconds_total{cpu="12",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="12",mode="user"} 0
node_cpu_guest_seconds_total{cpu="13",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="13",mode="user"} 0
node_cpu_guest_seconds_total{cpu="14",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="14",mode="user"} 0
node_cpu_guest_seconds_total{cpu="15",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="15",mode="user"} 0
node_cpu_guest_seconds_total{cpu="2",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="2",mode="user"} 0
node_cpu_guest_seconds_total{cpu="3",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="3",mode="user"} 0
node_cpu_guest_seconds_total{cpu="4",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="4",mode="user"} 0
node_cpu_guest_seconds_total{cpu="5",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="5",mode="user"} 0
node_cpu_guest_seconds_total{cpu="6",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="6",mode="user"} 0
node_cpu_guest_seconds_total{cpu="7",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="7",mode="user"} 0
node_cpu_guest_seconds_total{cpu="8",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="8",mode="user"} 0
node_cpu_guest_seconds_total{cpu="9",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="9",mode="user"} 0
`
notNormalized, _ := parser.TextToMetricFamilies(strings.NewReader(text))
for i := 0; i < b.N; i++ {
for _, mf := range notNormalized {
sort.Sort(MetricSorter(mf.Metric))
}
}
}