[REFACTOR] Scraping: Remove unnecessary yolostring calls (#16927)

Go will not allocate when reading from a map with a key cast from []byte to string.

Also remove some yoloString calls in package `textparse` - call a more suitable library function.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2025-07-28 14:54:32 +01:00 committed by GitHub
parent 2c04f2d7b1
commit d9e5748a27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 5 deletions

View File

@ -183,7 +183,7 @@ func (p *OpenMetricsParser) Help() ([]byte, []byte) {
m := p.l.b[p.offsets[0]:p.offsets[1]]
// Replacer causes allocations. Replace only when necessary.
if strings.IndexByte(yoloString(p.text), byte('\\')) >= 0 {
if bytes.IndexByte(p.text, byte('\\')) >= 0 {
// OpenMetrics always uses the Prometheus format label value escaping.
return m, []byte(lvalReplacer.Replace(string(p.text)))
}

View File

@ -17,6 +17,7 @@
package textparse
import (
"bytes"
"errors"
"fmt"
"io"
@ -199,7 +200,7 @@ func (p *PromParser) Help() ([]byte, []byte) {
m := p.l.b[p.offsets[0]:p.offsets[1]]
// Replacer causes allocations. Replace only when necessary.
if strings.IndexByte(yoloString(p.text), byte('\\')) >= 0 {
if bytes.IndexByte(p.text, byte('\\')) >= 0 {
return m, []byte(helpReplacer.Replace(string(p.text)))
}
return m, p.text

View File

@ -1116,7 +1116,7 @@ func (c *scrapeCache) setType(mfName []byte, t model.MetricType) ([]byte, *metaE
c.metaMtx.Lock()
defer c.metaMtx.Unlock()
e, ok := c.metadata[yoloString(mfName)]
e, ok := c.metadata[string(mfName)]
if !ok {
e = &metaEntry{Metadata: metadata.Metadata{Type: model.MetricTypeUnknown}}
c.metadata[string(mfName)] = e
@ -1133,7 +1133,7 @@ func (c *scrapeCache) setHelp(mfName, help []byte) ([]byte, *metaEntry) {
c.metaMtx.Lock()
defer c.metaMtx.Unlock()
e, ok := c.metadata[yoloString(mfName)]
e, ok := c.metadata[string(mfName)]
if !ok {
e = &metaEntry{Metadata: metadata.Metadata{Type: model.MetricTypeUnknown}}
c.metadata[string(mfName)] = e
@ -1150,7 +1150,7 @@ func (c *scrapeCache) setUnit(mfName, unit []byte) ([]byte, *metaEntry) {
c.metaMtx.Lock()
defer c.metaMtx.Unlock()
e, ok := c.metadata[yoloString(mfName)]
e, ok := c.metadata[string(mfName)]
if !ok {
e = &metaEntry{Metadata: metadata.Metadata{Type: model.MetricTypeUnknown}}
c.metadata[string(mfName)] = e