Improve metricUnits runtime (#1286)

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>
This commit is contained in:
Aviv Litman 2023-06-08 13:35:32 +03:00 committed by GitHub
parent 781ea28024
commit e4ff34d23e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 11 deletions

View File

@ -287,17 +287,15 @@ func lintUnitAbbreviations(mf *dto.MetricFamily) []Problem {
func metricUnits(m string) (unit, base string, ok bool) { func metricUnits(m string) (unit, base string, ok bool) {
ss := strings.Split(m, "_") ss := strings.Split(m, "_")
for unit, base := range units {
// Also check for "no prefix".
for _, p := range append(unitPrefixes, "") {
for _, s := range ss { for _, s := range ss {
// Attempt to explicitly match a known unit with a known prefix, if base, found := units[s]; found {
// as some words may look like "units" when matching suffix. return s, base, true
// }
// As an example, "thermometers" should not match "meters", but
// "kilometers" should. for _, p := range unitPrefixes {
if s == p+unit { if strings.HasPrefix(s, p) {
return p + unit, base, true if base, found := units[s[len(p):]]; found {
return s, base, true
} }
} }
} }