From 8f07f9dd90bd2694acb1ca5476e9d596a1449fd9 Mon Sep 17 00:00:00 2001 From: Arianna Vespri Date: Thu, 28 Dec 2023 15:41:38 +0100 Subject: [PATCH] Avoid creating string for suffix, consider counters without _total suffix Signed-off-by: Arianna Vespri --- model/textparse/protobufparse.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/model/textparse/protobufparse.go b/model/textparse/protobufparse.go index 5ecdbf6ef..9ccd00f6e 100644 --- a/model/textparse/protobufparse.go +++ b/model/textparse/protobufparse.go @@ -425,11 +425,11 @@ func (p *ProtobufParser) Next() (Entry, error) { } unit := p.mf.GetUnit() if len(unit) > 0 { - sfx := fmt.Sprintf("_%s", unit) - if p.mf.GetType() == dto.MetricType_COUNTER { - sfx = fmt.Sprintf("_%s_total", unit) - } - if !strings.HasSuffix(name, sfx) { + if p.mf.GetType() == dto.MetricType_COUNTER && strings.HasSuffix(name, "_total") { + if !strings.HasSuffix(name[:len(name)-6], unit) || len(name)-6 < len(unit)+1 || name[len(name)-6-len(unit)-1] != '_' { + return EntryInvalid, fmt.Errorf("unit %q not a suffix of counter %q", unit, name) + } + } else if !strings.HasSuffix(name, unit) || len(name) < len(unit)+1 || name[len(name)-len(unit)-1] != '_' { return EntryInvalid, fmt.Errorf("unit %q not a suffix of metric %q", unit, name) } }