Skip to content

Commit 00b69ef

Browse files
authored
model/textparse: Change parser interface Metric(...) string to Labels(...) (prometheus#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 prometheus#15731 (comment) 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>
1 parent a323c23 commit 00b69ef

File tree

11 files changed

+22
-35
lines changed

11 files changed

+22
-35
lines changed

cmd/promtool/backfill.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func createBlocks(input []byte, mint, maxt, maxBlockDuration int64, maxSamplesIn
149149
_, ts, v := p.Series()
150150
if ts == nil {
151151
l := labels.Labels{}
152-
p.Metric(&l)
152+
p.Labels(&l)
153153
return fmt.Errorf("expected timestamp for series %v, got none", l)
154154
}
155155
if *ts < t {
@@ -163,7 +163,7 @@ func createBlocks(input []byte, mint, maxt, maxBlockDuration int64, maxSamplesIn
163163
}
164164

165165
l := labels.Labels{}
166-
p.Metric(&l)
166+
p.Labels(&l)
167167

168168
lb.Reset(l)
169169
for name, value := range customLabels {

model/textparse/benchmark_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func benchParse(b *testing.B, data []byte, parser string) {
203203
b.Fatal("not implemented entry", t)
204204
}
205205

206-
_ = p.Metric(&res)
206+
p.Labels(&res)
207207
_ = p.CreatedTimestamp()
208208
for hasExemplar := p.Exemplar(&e); hasExemplar; hasExemplar = p.Exemplar(&e) {
209209
}

model/textparse/interface.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ type Parser interface {
5757
// The returned byte slice becomes invalid after the next call to Next.
5858
Comment() []byte
5959

60-
// Metric writes the labels of the current sample into the passed labels.
61-
// It returns the string from which the metric was parsed.
60+
// Labels writes the labels of the current sample into the passed labels.
6261
// The values of the "le" labels of classic histograms and "quantile" labels
6362
// of summaries should follow the OpenMetrics formatting rules.
64-
Metric(l *labels.Labels) string
63+
Labels(l *labels.Labels)
6564

6665
// Exemplar writes the exemplar of the current sample into the passed
6766
// exemplar. It can be called repeatedly to retrieve multiple exemplars

model/textparse/interface_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func testParse(t *testing.T, p Parser) (ret []parsedEntry) {
238238
got.m = string(m)
239239
}
240240

241-
p.Metric(&got.lset)
241+
p.Labels(&got.lset)
242242
// Parser reuses int pointer.
243243
if ct := p.CreatedTimestamp(); ct != nil {
244244
got.ct = int64p(*ct)

model/textparse/nhcbparse.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ type NHCBParser struct {
6767
h *histogram.Histogram
6868
fh *histogram.FloatHistogram
6969
// For Metric.
70-
lset labels.Labels
71-
metricString string
70+
lset labels.Labels
7271
// For Type.
7372
bName []byte
7473
typ model.MetricType
@@ -141,13 +140,12 @@ func (p *NHCBParser) Comment() []byte {
141140
return p.parser.Comment()
142141
}
143142

144-
func (p *NHCBParser) Metric(l *labels.Labels) string {
143+
func (p *NHCBParser) Labels(l *labels.Labels) {
145144
if p.state == stateEmitting {
146145
*l = p.lsetNHCB
147-
return p.metricStringNHCB
146+
return
148147
}
149148
*l = p.lset
150-
return p.metricString
151149
}
152150

153151
func (p *NHCBParser) Exemplar(ex *exemplar.Exemplar) bool {
@@ -200,7 +198,7 @@ func (p *NHCBParser) Next() (Entry, error) {
200198
switch p.entry {
201199
case EntrySeries:
202200
p.bytes, p.ts, p.value = p.parser.Series()
203-
p.metricString = p.parser.Metric(&p.lset)
201+
p.parser.Labels(&p.lset)
204202
// Check the label set to see if we can continue or need to emit the NHCB.
205203
var isNHCB bool
206204
if p.compareLabels() {
@@ -224,7 +222,7 @@ func (p *NHCBParser) Next() (Entry, error) {
224222
return p.entry, p.err
225223
case EntryHistogram:
226224
p.bytes, p.ts, p.h, p.fh = p.parser.Histogram()
227-
p.metricString = p.parser.Metric(&p.lset)
225+
p.parser.Labels(&p.lset)
228226
p.storeExponentialLabels()
229227
case EntryType:
230228
p.bName, p.typ = p.parser.Type()

model/textparse/openmetricsparse.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,9 @@ func (p *OpenMetricsParser) Comment() []byte {
197197
return p.text
198198
}
199199

200-
// Metric writes the labels of the current sample into the passed labels.
201-
// It returns the string from which the metric was parsed.
202-
func (p *OpenMetricsParser) Metric(l *labels.Labels) string {
203-
// Copy the buffer to a string: this is only necessary for the return value.
204-
s := string(p.series)
200+
// Labels writes the labels of the current sample into the passed labels.
201+
func (p *OpenMetricsParser) Labels(l *labels.Labels) {
202+
s := yoloString(p.series)
205203

206204
p.builder.Reset()
207205
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 {
220218

221219
p.builder.Sort()
222220
*l = p.builder.Labels()
223-
224-
return s
225221
}
226222

227223
// Exemplar writes the exemplar of the current sample into the passed exemplar.

model/textparse/promparse.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,9 @@ func (p *PromParser) Comment() []byte {
223223
return p.text
224224
}
225225

226-
// Metric writes the labels of the current sample into the passed labels.
227-
// It returns the string from which the metric was parsed.
228-
func (p *PromParser) Metric(l *labels.Labels) string {
229-
// Copy the buffer to a string: this is only necessary for the return value.
230-
s := string(p.series)
226+
// Labels writes the labels of the current sample into the passed labels.
227+
func (p *PromParser) Labels(l *labels.Labels) {
228+
s := yoloString(p.series)
231229

232230
p.builder.Reset()
233231
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 {
246244

247245
p.builder.Sort()
248246
*l = p.builder.Labels()
249-
250-
return s
251247
}
252248

253249
// Exemplar implements the Parser interface. However, since the classic

model/textparse/protobufparse.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ func (p *ProtobufParser) Comment() []byte {
296296
return nil
297297
}
298298

299-
// Metric writes the labels of the current sample into the passed labels.
299+
// Labels writes the labels of the current sample into the passed labels.
300300
// It returns the string from which the metric was parsed.
301-
func (p *ProtobufParser) Metric(l *labels.Labels) string {
301+
func (p *ProtobufParser) Labels(l *labels.Labels) {
302302
p.builder.Reset()
303303
p.builder.Add(labels.MetricName, p.getMagicName())
304304

@@ -312,8 +312,6 @@ func (p *ProtobufParser) Metric(l *labels.Labels) string {
312312
// Sort labels to maintain the sorted labels invariant.
313313
p.builder.Sort()
314314
*l = p.builder.Labels()
315-
316-
return p.metricBytes.String()
317315
}
318316

319317
// Exemplar writes the exemplar of the current sample into the passed

scrape/scrape.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ loop:
17141714
lset = ce.lset
17151715
hash = ce.hash
17161716
} else {
1717-
p.Metric(&lset)
1717+
p.Labels(&lset)
17181718
hash = lset.Hash()
17191719

17201720
// Hash label set as it is seen local to the target. Then add target labels

scrape/scrape_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ func TestScrapeLoopAppendCacheEntryButErrNotFound(t *testing.T) {
19881988

19891989
var lset labels.Labels
19901990
p.Next()
1991-
p.Metric(&lset)
1991+
p.Labels(&lset)
19921992
hash := lset.Hash()
19931993

19941994
// Create a fake entry in the cache

0 commit comments

Comments
 (0)