Skip to content

Commit

Permalink
Improve metricUnits runtime (#1286)
Browse files Browse the repository at this point in the history
We tested this function runtime in both cases using "testing",
and the runtime for this pr is much shorter.

Signed-off-by: alitman <alitman@redhat.com>
  • Loading branch information
avlitman authored Jun 8, 2023
1 parent 781ea28 commit e4ff34d
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions prometheus/testutil/promlint/promlint.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,15 @@ func lintUnitAbbreviations(mf *dto.MetricFamily) []Problem {
func metricUnits(m string) (unit, base string, ok bool) {
ss := strings.Split(m, "_")

for unit, base := range units {
// Also check for "no prefix".
for _, p := range append(unitPrefixes, "") {
for _, s := range ss {
// Attempt to explicitly match a known unit with a known prefix,
// as some words may look like "units" when matching suffix.
//
// As an example, "thermometers" should not match "meters", but
// "kilometers" should.
if s == p+unit {
return p + unit, base, true
for _, s := range ss {
if base, found := units[s]; found {
return s, base, true
}

for _, p := range unitPrefixes {
if strings.HasPrefix(s, p) {
if base, found := units[s[len(p):]]; found {
return s, base, true
}
}
}
Expand Down

0 comments on commit e4ff34d

Please sign in to comment.