Avoid creating string for suffix, consider counters without _total suffix

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
This commit is contained in:
Arianna Vespri 2023-12-28 15:41:38 +01:00
parent 9a664b515a
commit 8f07f9dd90
1 changed files with 5 additions and 5 deletions

View File

@ -425,11 +425,11 @@ func (p *ProtobufParser) Next() (Entry, error) {
} }
unit := p.mf.GetUnit() unit := p.mf.GetUnit()
if len(unit) > 0 { if len(unit) > 0 {
sfx := fmt.Sprintf("_%s", unit) if p.mf.GetType() == dto.MetricType_COUNTER && strings.HasSuffix(name, "_total") {
if p.mf.GetType() == dto.MetricType_COUNTER { if !strings.HasSuffix(name[:len(name)-6], unit) || len(name)-6 < len(unit)+1 || name[len(name)-6-len(unit)-1] != '_' {
sfx = fmt.Sprintf("_%s_total", unit) return EntryInvalid, fmt.Errorf("unit %q not a suffix of counter %q", unit, name)
} }
if !strings.HasSuffix(name, sfx) { } 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) return EntryInvalid, fmt.Errorf("unit %q not a suffix of metric %q", unit, name)
} }
} }