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:
parent
781ea28024
commit
e4ff34d23e
|
@ -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 {
|
for _, s := range ss {
|
||||||
// Also check for "no prefix".
|
if base, found := units[s]; found {
|
||||||
for _, p := range append(unitPrefixes, "") {
|
return s, base, true
|
||||||
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.
|
for _, p := range unitPrefixes {
|
||||||
//
|
if strings.HasPrefix(s, p) {
|
||||||
// As an example, "thermometers" should not match "meters", but
|
if base, found := units[s[len(p):]]; found {
|
||||||
// "kilometers" should.
|
return s, base, true
|
||||||
if s == p+unit {
|
|
||||||
return p + unit, base, true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue