Skip to content

Commit c3337d4

Browse files
committed
Improve metricUnits runtime
We tested this function runtime in both cases using "testing", and the runtime for this pr is much shorter. Signed-off-by: alitman <[email protected]>
1 parent a09a1d3 commit c3337d4

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

prometheus/testutil/promlint/promlint.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,15 @@ func lintUnitAbbreviations(mf *dto.MetricFamily) []Problem {
287287
func metricUnits(m string) (unit, base string, ok bool) {
288288
ss := strings.Split(m, "_")
289289

290-
for unit, base := range units {
291-
// Also check for "no prefix".
292-
for _, p := range append(unitPrefixes, "") {
293-
for _, s := range ss {
294-
// Attempt to explicitly match a known unit with a known prefix,
295-
// as some words may look like "units" when matching suffix.
296-
//
297-
// As an example, "thermometers" should not match "meters", but
298-
// "kilometers" should.
299-
if s == p+unit {
300-
return p + unit, base, true
290+
for _, s := range ss {
291+
if base, found := units[s]; found {
292+
return s, base, true
293+
}
294+
295+
for _, p := range unitPrefixes {
296+
if strings.HasPrefix(s, p) {
297+
if base, found := units[s[len(p):]]; found {
298+
return s, base, true
301299
}
302300
}
303301
}

0 commit comments

Comments
 (0)