model/textparse: Change parser interface Metric(...) string to Labels(...) (#16012)

* model/textparse: Change parser interface Metric(...) string to Labels(...)

Simplified the interface given no one is using the return argument.
Renamed for clarity too.

Found and discussed https://github.com/prometheus/prometheus/pull/15731#discussion_r1950916842

Signed-off-by: bwplotka <bwplotka@gmail.com>

* Fixed comments; optimized not needed copy for om and text.

Signed-off-by: bwplotka <bwplotka@gmail.com>

---------

Signed-off-by: bwplotka <bwplotka@gmail.com>
This commit is contained in:
Bartlomiej Plotka 2025-02-12 16:47:56 +01:00 committed by GitHub
parent a323c23332
commit 00b69efabb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 22 additions and 35 deletions

View file

@ -149,7 +149,7 @@ func createBlocks(input []byte, mint, maxt, maxBlockDuration int64, maxSamplesIn
_, ts, v := p.Series()
if ts == nil {
l := labels.Labels{}
p.Metric(&l)
p.Labels(&l)
return fmt.Errorf("expected timestamp for series %v, got none", l)
}
if *ts < t {
@ -163,7 +163,7 @@ func createBlocks(input []byte, mint, maxt, maxBlockDuration int64, maxSamplesIn
}
l := labels.Labels{}
p.Metric(&l)
p.Labels(&l)
lb.Reset(l)
for name, value := range customLabels {

View file

@ -203,7 +203,7 @@ func benchParse(b *testing.B, data []byte, parser string) {
b.Fatal("not implemented entry", t)
}
_ = p.Metric(&res)
p.Labels(&res)
_ = p.CreatedTimestamp()
for hasExemplar := p.Exemplar(&e); hasExemplar; hasExemplar = p.Exemplar(&e) {
}

View file

@ -57,11 +57,10 @@ type Parser interface {
// The returned byte slice becomes invalid after the next call to Next.
Comment() []byte
// Metric writes the labels of the current sample into the passed labels.
// It returns the string from which the metric was parsed.
// Labels writes the labels of the current sample into the passed labels.
// The values of the "le" labels of classic histograms and "quantile" labels
// of summaries should follow the OpenMetrics formatting rules.
Metric(l *labels.Labels) string
Labels(l *labels.Labels)
// Exemplar writes the exemplar of the current sample into the passed
// exemplar. It can be called repeatedly to retrieve multiple exemplars

View file

@ -238,7 +238,7 @@ func testParse(t *testing.T, p Parser) (ret []parsedEntry) {
got.m = string(m)
}
p.Metric(&got.lset)
p.Labels(&got.lset)
// Parser reuses int pointer.
if ct := p.CreatedTimestamp(); ct != nil {
got.ct = int64p(*ct)

View file

@ -67,8 +67,7 @@ type NHCBParser struct {
h *histogram.Histogram
fh *histogram.FloatHistogram
// For Metric.
lset labels.Labels
metricString string
lset labels.Labels
// For Type.
bName []byte
typ model.MetricType
@ -141,13 +140,12 @@ func (p *NHCBParser) Comment() []byte {
return p.parser.Comment()
}
func (p *NHCBParser) Metric(l *labels.Labels) string {
func (p *NHCBParser) Labels(l *labels.Labels) {
if p.state == stateEmitting {
*l = p.lsetNHCB
return p.metricStringNHCB
return
}
*l = p.lset
return p.metricString
}
func (p *NHCBParser) Exemplar(ex *exemplar.Exemplar) bool {
@ -200,7 +198,7 @@ func (p *NHCBParser) Next() (Entry, error) {
switch p.entry {
case EntrySeries:
p.bytes, p.ts, p.value = p.parser.Series()
p.metricString = p.parser.Metric(&p.lset)
p.parser.Labels(&p.lset)
// Check the label set to see if we can continue or need to emit the NHCB.
var isNHCB bool
if p.compareLabels() {
@ -224,7 +222,7 @@ func (p *NHCBParser) Next() (Entry, error) {
return p.entry, p.err
case EntryHistogram:
p.bytes, p.ts, p.h, p.fh = p.parser.Histogram()
p.metricString = p.parser.Metric(&p.lset)
p.parser.Labels(&p.lset)
p.storeExponentialLabels()
case EntryType:
p.bName, p.typ = p.parser.Type()

View file

@ -197,11 +197,9 @@ func (p *OpenMetricsParser) Comment() []byte {
return p.text
}
// Metric writes the labels of the current sample into the passed labels.
// It returns the string from which the metric was parsed.
func (p *OpenMetricsParser) Metric(l *labels.Labels) string {
// Copy the buffer to a string: this is only necessary for the return value.
s := string(p.series)
// Labels writes the labels of the current sample into the passed labels.
func (p *OpenMetricsParser) Labels(l *labels.Labels) {
s := yoloString(p.series)
p.builder.Reset()
metricName := unreplace(s[p.offsets[0]-p.start : p.offsets[1]-p.start])
@ -220,8 +218,6 @@ func (p *OpenMetricsParser) Metric(l *labels.Labels) string {
p.builder.Sort()
*l = p.builder.Labels()
return s
}
// Exemplar writes the exemplar of the current sample into the passed exemplar.

View file

@ -223,11 +223,9 @@ func (p *PromParser) Comment() []byte {
return p.text
}
// Metric writes the labels of the current sample into the passed labels.
// It returns the string from which the metric was parsed.
func (p *PromParser) Metric(l *labels.Labels) string {
// Copy the buffer to a string: this is only necessary for the return value.
s := string(p.series)
// Labels writes the labels of the current sample into the passed labels.
func (p *PromParser) Labels(l *labels.Labels) {
s := yoloString(p.series)
p.builder.Reset()
metricName := unreplace(s[p.offsets[0]-p.start : p.offsets[1]-p.start])
@ -246,8 +244,6 @@ func (p *PromParser) Metric(l *labels.Labels) string {
p.builder.Sort()
*l = p.builder.Labels()
return s
}
// Exemplar implements the Parser interface. However, since the classic

View file

@ -296,9 +296,9 @@ func (p *ProtobufParser) Comment() []byte {
return nil
}
// Metric writes the labels of the current sample into the passed labels.
// Labels writes the labels of the current sample into the passed labels.
// It returns the string from which the metric was parsed.
func (p *ProtobufParser) Metric(l *labels.Labels) string {
func (p *ProtobufParser) Labels(l *labels.Labels) {
p.builder.Reset()
p.builder.Add(labels.MetricName, p.getMagicName())
@ -312,8 +312,6 @@ func (p *ProtobufParser) Metric(l *labels.Labels) string {
// Sort labels to maintain the sorted labels invariant.
p.builder.Sort()
*l = p.builder.Labels()
return p.metricBytes.String()
}
// Exemplar writes the exemplar of the current sample into the passed

View file

@ -1714,7 +1714,7 @@ loop:
lset = ce.lset
hash = ce.hash
} else {
p.Metric(&lset)
p.Labels(&lset)
hash = lset.Hash()
// Hash label set as it is seen local to the target. Then add target labels

View file

@ -1988,7 +1988,7 @@ func TestScrapeLoopAppendCacheEntryButErrNotFound(t *testing.T) {
var lset labels.Labels
p.Next()
p.Metric(&lset)
p.Labels(&lset)
hash := lset.Hash()
// Create a fake entry in the cache

View file

@ -403,7 +403,7 @@ func TestFederationWithNativeHistograms(t *testing.T) {
}
require.NoError(t, err)
if et == textparse.EntryHistogram || et == textparse.EntrySeries {
p.Metric(&l)
p.Labels(&l)
}
switch et {
case textparse.EntryHelp: