From e4ff34d23eacb385c1a9d3d67c4d06aed042ebec Mon Sep 17 00:00:00 2001 From: Aviv Litman <64130977+avlitman@users.noreply.github.com> Date: Thu, 8 Jun 2023 13:35:32 +0300 Subject: [PATCH] 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 --- prometheus/testutil/promlint/promlint.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/prometheus/testutil/promlint/promlint.go b/prometheus/testutil/promlint/promlint.go index a20f159b7..c8864b6c3 100644 --- a/prometheus/testutil/promlint/promlint.go +++ b/prometheus/testutil/promlint/promlint.go @@ -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 } } }