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
Closed
fix metric sort error #1443
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
package internal | ||
|
||
import ( | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
@@ -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. | ||
// dealing with sorting problems, especially CPU sorting errors | ||
re := regexp.MustCompile(`\d+$`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.