mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
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:
parent
a323c23332
commit
00b69efabb
|
@ -149,7 +149,7 @@ func createBlocks(input []byte, mint, maxt, maxBlockDuration int64, maxSamplesIn
|
||||||
_, ts, v := p.Series()
|
_, ts, v := p.Series()
|
||||||
if ts == nil {
|
if ts == nil {
|
||||||
l := labels.Labels{}
|
l := labels.Labels{}
|
||||||
p.Metric(&l)
|
p.Labels(&l)
|
||||||
return fmt.Errorf("expected timestamp for series %v, got none", l)
|
return fmt.Errorf("expected timestamp for series %v, got none", l)
|
||||||
}
|
}
|
||||||
if *ts < t {
|
if *ts < t {
|
||||||
|
@ -163,7 +163,7 @@ func createBlocks(input []byte, mint, maxt, maxBlockDuration int64, maxSamplesIn
|
||||||
}
|
}
|
||||||
|
|
||||||
l := labels.Labels{}
|
l := labels.Labels{}
|
||||||
p.Metric(&l)
|
p.Labels(&l)
|
||||||
|
|
||||||
lb.Reset(l)
|
lb.Reset(l)
|
||||||
for name, value := range customLabels {
|
for name, value := range customLabels {
|
||||||
|
|
|
@ -203,7 +203,7 @@ func benchParse(b *testing.B, data []byte, parser string) {
|
||||||
b.Fatal("not implemented entry", t)
|
b.Fatal("not implemented entry", t)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = p.Metric(&res)
|
p.Labels(&res)
|
||||||
_ = p.CreatedTimestamp()
|
_ = p.CreatedTimestamp()
|
||||||
for hasExemplar := p.Exemplar(&e); hasExemplar; hasExemplar = p.Exemplar(&e) {
|
for hasExemplar := p.Exemplar(&e); hasExemplar; hasExemplar = p.Exemplar(&e) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,11 +57,10 @@ type Parser interface {
|
||||||
// The returned byte slice becomes invalid after the next call to Next.
|
// The returned byte slice becomes invalid after the next call to Next.
|
||||||
Comment() []byte
|
Comment() []byte
|
||||||
|
|
||||||
// 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.
|
|
||||||
// The values of the "le" labels of classic histograms and "quantile" labels
|
// The values of the "le" labels of classic histograms and "quantile" labels
|
||||||
// of summaries should follow the OpenMetrics formatting rules.
|
// 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 writes the exemplar of the current sample into the passed
|
||||||
// exemplar. It can be called repeatedly to retrieve multiple exemplars
|
// exemplar. It can be called repeatedly to retrieve multiple exemplars
|
||||||
|
|
|
@ -238,7 +238,7 @@ func testParse(t *testing.T, p Parser) (ret []parsedEntry) {
|
||||||
got.m = string(m)
|
got.m = string(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Metric(&got.lset)
|
p.Labels(&got.lset)
|
||||||
// Parser reuses int pointer.
|
// Parser reuses int pointer.
|
||||||
if ct := p.CreatedTimestamp(); ct != nil {
|
if ct := p.CreatedTimestamp(); ct != nil {
|
||||||
got.ct = int64p(*ct)
|
got.ct = int64p(*ct)
|
||||||
|
|
|
@ -68,7 +68,6 @@ type NHCBParser struct {
|
||||||
fh *histogram.FloatHistogram
|
fh *histogram.FloatHistogram
|
||||||
// For Metric.
|
// For Metric.
|
||||||
lset labels.Labels
|
lset labels.Labels
|
||||||
metricString string
|
|
||||||
// For Type.
|
// For Type.
|
||||||
bName []byte
|
bName []byte
|
||||||
typ model.MetricType
|
typ model.MetricType
|
||||||
|
@ -141,13 +140,12 @@ func (p *NHCBParser) Comment() []byte {
|
||||||
return p.parser.Comment()
|
return p.parser.Comment()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NHCBParser) Metric(l *labels.Labels) string {
|
func (p *NHCBParser) Labels(l *labels.Labels) {
|
||||||
if p.state == stateEmitting {
|
if p.state == stateEmitting {
|
||||||
*l = p.lsetNHCB
|
*l = p.lsetNHCB
|
||||||
return p.metricStringNHCB
|
return
|
||||||
}
|
}
|
||||||
*l = p.lset
|
*l = p.lset
|
||||||
return p.metricString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NHCBParser) Exemplar(ex *exemplar.Exemplar) bool {
|
func (p *NHCBParser) Exemplar(ex *exemplar.Exemplar) bool {
|
||||||
|
@ -200,7 +198,7 @@ func (p *NHCBParser) Next() (Entry, error) {
|
||||||
switch p.entry {
|
switch p.entry {
|
||||||
case EntrySeries:
|
case EntrySeries:
|
||||||
p.bytes, p.ts, p.value = p.parser.Series()
|
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.
|
// Check the label set to see if we can continue or need to emit the NHCB.
|
||||||
var isNHCB bool
|
var isNHCB bool
|
||||||
if p.compareLabels() {
|
if p.compareLabels() {
|
||||||
|
@ -224,7 +222,7 @@ func (p *NHCBParser) Next() (Entry, error) {
|
||||||
return p.entry, p.err
|
return p.entry, p.err
|
||||||
case EntryHistogram:
|
case EntryHistogram:
|
||||||
p.bytes, p.ts, p.h, p.fh = p.parser.Histogram()
|
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()
|
p.storeExponentialLabels()
|
||||||
case EntryType:
|
case EntryType:
|
||||||
p.bName, p.typ = p.parser.Type()
|
p.bName, p.typ = p.parser.Type()
|
||||||
|
|
|
@ -197,11 +197,9 @@ func (p *OpenMetricsParser) Comment() []byte {
|
||||||
return p.text
|
return p.text
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 *OpenMetricsParser) Labels(l *labels.Labels) {
|
||||||
func (p *OpenMetricsParser) Metric(l *labels.Labels) string {
|
s := yoloString(p.series)
|
||||||
// Copy the buffer to a string: this is only necessary for the return value.
|
|
||||||
s := string(p.series)
|
|
||||||
|
|
||||||
p.builder.Reset()
|
p.builder.Reset()
|
||||||
metricName := unreplace(s[p.offsets[0]-p.start : p.offsets[1]-p.start])
|
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()
|
p.builder.Sort()
|
||||||
*l = p.builder.Labels()
|
*l = p.builder.Labels()
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exemplar writes the exemplar of the current sample into the passed exemplar.
|
// Exemplar writes the exemplar of the current sample into the passed exemplar.
|
||||||
|
|
|
@ -223,11 +223,9 @@ func (p *PromParser) Comment() []byte {
|
||||||
return p.text
|
return p.text
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 *PromParser) Labels(l *labels.Labels) {
|
||||||
func (p *PromParser) Metric(l *labels.Labels) string {
|
s := yoloString(p.series)
|
||||||
// Copy the buffer to a string: this is only necessary for the return value.
|
|
||||||
s := string(p.series)
|
|
||||||
|
|
||||||
p.builder.Reset()
|
p.builder.Reset()
|
||||||
metricName := unreplace(s[p.offsets[0]-p.start : p.offsets[1]-p.start])
|
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()
|
p.builder.Sort()
|
||||||
*l = p.builder.Labels()
|
*l = p.builder.Labels()
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exemplar implements the Parser interface. However, since the classic
|
// Exemplar implements the Parser interface. However, since the classic
|
||||||
|
|
|
@ -296,9 +296,9 @@ func (p *ProtobufParser) Comment() []byte {
|
||||||
return nil
|
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.
|
// 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.Reset()
|
||||||
p.builder.Add(labels.MetricName, p.getMagicName())
|
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.
|
// Sort labels to maintain the sorted labels invariant.
|
||||||
p.builder.Sort()
|
p.builder.Sort()
|
||||||
*l = p.builder.Labels()
|
*l = p.builder.Labels()
|
||||||
|
|
||||||
return p.metricBytes.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exemplar writes the exemplar of the current sample into the passed
|
// Exemplar writes the exemplar of the current sample into the passed
|
||||||
|
|
|
@ -1714,7 +1714,7 @@ loop:
|
||||||
lset = ce.lset
|
lset = ce.lset
|
||||||
hash = ce.hash
|
hash = ce.hash
|
||||||
} else {
|
} else {
|
||||||
p.Metric(&lset)
|
p.Labels(&lset)
|
||||||
hash = lset.Hash()
|
hash = lset.Hash()
|
||||||
|
|
||||||
// Hash label set as it is seen local to the target. Then add target labels
|
// Hash label set as it is seen local to the target. Then add target labels
|
||||||
|
|
|
@ -1988,7 +1988,7 @@ func TestScrapeLoopAppendCacheEntryButErrNotFound(t *testing.T) {
|
||||||
|
|
||||||
var lset labels.Labels
|
var lset labels.Labels
|
||||||
p.Next()
|
p.Next()
|
||||||
p.Metric(&lset)
|
p.Labels(&lset)
|
||||||
hash := lset.Hash()
|
hash := lset.Hash()
|
||||||
|
|
||||||
// Create a fake entry in the cache
|
// Create a fake entry in the cache
|
||||||
|
|
|
@ -403,7 +403,7 @@ func TestFederationWithNativeHistograms(t *testing.T) {
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
if et == textparse.EntryHistogram || et == textparse.EntrySeries {
|
if et == textparse.EntryHistogram || et == textparse.EntrySeries {
|
||||||
p.Metric(&l)
|
p.Labels(&l)
|
||||||
}
|
}
|
||||||
switch et {
|
switch et {
|
||||||
case textparse.EntryHelp:
|
case textparse.EntryHelp:
|
||||||
|
|
Loading…
Reference in a new issue