mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
NHCB scrape: refactor state handling and speed up scrape test (#15193)
* NHCB: scrape use state field and not booleans From comment https://github.com/prometheus/prometheus/pull/14978#discussion_r1800898724 Also make compareLabels read only and move storeLabels to the first processed classic histogram series. Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com> * Speed up TestConvertClassicHistogramsToNHCB 3x Reduce the startup time and timeouts Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com> * lint fix Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com> --------- Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
This commit is contained in:
parent
3bb5e28c6b
commit
aa81210c8b
|
@ -28,6 +28,14 @@ import (
|
||||||
"github.com/prometheus/prometheus/util/convertnhcb"
|
"github.com/prometheus/prometheus/util/convertnhcb"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type collectionState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
stateStart collectionState = iota
|
||||||
|
stateCollecting
|
||||||
|
stateEmitting
|
||||||
|
)
|
||||||
|
|
||||||
// The NHCBParser wraps a Parser and converts classic histograms to native
|
// The NHCBParser wraps a Parser and converts classic histograms to native
|
||||||
// histograms with custom buckets.
|
// histograms with custom buckets.
|
||||||
//
|
//
|
||||||
|
@ -48,6 +56,9 @@ type NHCBParser struct {
|
||||||
// Labels builder.
|
// Labels builder.
|
||||||
builder labels.ScratchBuilder
|
builder labels.ScratchBuilder
|
||||||
|
|
||||||
|
// State of the parser.
|
||||||
|
state collectionState
|
||||||
|
|
||||||
// Caches the values from the underlying parser.
|
// Caches the values from the underlying parser.
|
||||||
// For Series and Histogram.
|
// For Series and Histogram.
|
||||||
bytes []byte
|
bytes []byte
|
||||||
|
@ -64,9 +75,9 @@ type NHCBParser struct {
|
||||||
|
|
||||||
// Caches the entry itself if we are inserting a converted NHCB
|
// Caches the entry itself if we are inserting a converted NHCB
|
||||||
// halfway through.
|
// halfway through.
|
||||||
entry Entry
|
entry Entry
|
||||||
err error
|
err error
|
||||||
justInsertedNHCB bool
|
|
||||||
// Caches the values and metric for the inserted converted NHCB.
|
// Caches the values and metric for the inserted converted NHCB.
|
||||||
bytesNHCB []byte
|
bytesNHCB []byte
|
||||||
hNHCB *histogram.Histogram
|
hNHCB *histogram.Histogram
|
||||||
|
@ -77,11 +88,10 @@ type NHCBParser struct {
|
||||||
|
|
||||||
// Collates values from the classic histogram series to build
|
// Collates values from the classic histogram series to build
|
||||||
// the converted histogram later.
|
// the converted histogram later.
|
||||||
tempLsetNHCB labels.Labels
|
tempLsetNHCB labels.Labels
|
||||||
tempNHCB convertnhcb.TempHistogram
|
tempNHCB convertnhcb.TempHistogram
|
||||||
tempExemplars []exemplar.Exemplar
|
tempExemplars []exemplar.Exemplar
|
||||||
tempExemplarCount int
|
tempExemplarCount int
|
||||||
isCollationInProgress bool
|
|
||||||
|
|
||||||
// Remembers the last base histogram metric name (assuming it's
|
// Remembers the last base histogram metric name (assuming it's
|
||||||
// a classic histogram) so we can tell if the next float series
|
// a classic histogram) so we can tell if the next float series
|
||||||
|
@ -105,7 +115,7 @@ func (p *NHCBParser) Series() ([]byte, *int64, float64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NHCBParser) Histogram() ([]byte, *int64, *histogram.Histogram, *histogram.FloatHistogram) {
|
func (p *NHCBParser) Histogram() ([]byte, *int64, *histogram.Histogram, *histogram.FloatHistogram) {
|
||||||
if p.justInsertedNHCB {
|
if p.state == stateEmitting {
|
||||||
return p.bytesNHCB, p.ts, p.hNHCB, p.fhNHCB
|
return p.bytesNHCB, p.ts, p.hNHCB, p.fhNHCB
|
||||||
}
|
}
|
||||||
return p.bytes, p.ts, p.h, p.fh
|
return p.bytes, p.ts, p.h, p.fh
|
||||||
|
@ -128,7 +138,7 @@ func (p *NHCBParser) Comment() []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NHCBParser) Metric(l *labels.Labels) string {
|
func (p *NHCBParser) Metric(l *labels.Labels) string {
|
||||||
if p.justInsertedNHCB {
|
if p.state == stateEmitting {
|
||||||
*l = p.lsetNHCB
|
*l = p.lsetNHCB
|
||||||
return p.metricStringNHCB
|
return p.metricStringNHCB
|
||||||
}
|
}
|
||||||
|
@ -137,7 +147,7 @@ func (p *NHCBParser) Metric(l *labels.Labels) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NHCBParser) Exemplar(ex *exemplar.Exemplar) bool {
|
func (p *NHCBParser) Exemplar(ex *exemplar.Exemplar) bool {
|
||||||
if p.justInsertedNHCB {
|
if p.state == stateEmitting {
|
||||||
if len(p.exemplars) == 0 {
|
if len(p.exemplars) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -153,8 +163,8 @@ func (p *NHCBParser) CreatedTimestamp() *int64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NHCBParser) Next() (Entry, error) {
|
func (p *NHCBParser) Next() (Entry, error) {
|
||||||
if p.justInsertedNHCB {
|
if p.state == stateEmitting {
|
||||||
p.justInsertedNHCB = false
|
p.state = stateStart
|
||||||
if p.entry == EntrySeries {
|
if p.entry == EntrySeries {
|
||||||
isNHCB := p.handleClassicHistogramSeries(p.lset)
|
isNHCB := p.handleClassicHistogramSeries(p.lset)
|
||||||
if isNHCB && !p.keepClassicHistograms {
|
if isNHCB && !p.keepClassicHistograms {
|
||||||
|
@ -202,34 +212,21 @@ func (p *NHCBParser) Next() (Entry, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return true if labels have changed and we should emit the NHCB.
|
// Return true if labels have changed and we should emit the NHCB.
|
||||||
// Update the stored labels if the labels have changed.
|
|
||||||
func (p *NHCBParser) compareLabels() bool {
|
func (p *NHCBParser) compareLabels() bool {
|
||||||
// Collection not in progress.
|
if p.state != stateCollecting {
|
||||||
if p.lastHistogramName == "" {
|
|
||||||
if p.typ == model.MetricTypeHistogram {
|
|
||||||
p.storeBaseLabels()
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if p.typ != model.MetricTypeHistogram {
|
if p.typ != model.MetricTypeHistogram {
|
||||||
// Different metric type, emit the NHCB.
|
// Different metric type.
|
||||||
p.lastHistogramName = ""
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.lastHistogramName != convertnhcb.GetHistogramMetricBaseName(p.lset.Get(labels.MetricName)) {
|
if p.lastHistogramName != convertnhcb.GetHistogramMetricBaseName(p.lset.Get(labels.MetricName)) {
|
||||||
// Different metric name.
|
// Different metric name.
|
||||||
p.storeBaseLabels()
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
nextHash, _ := p.lset.HashWithoutLabels(p.hBuffer, labels.BucketLabel)
|
nextHash, _ := p.lset.HashWithoutLabels(p.hBuffer, labels.BucketLabel)
|
||||||
if p.lastHistogramLabelsHash != nextHash {
|
// Different label values.
|
||||||
// Different label values.
|
return p.lastHistogramLabelsHash != nextHash
|
||||||
p.storeBaseLabels()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the label set of the classic histogram without suffix and bucket `le` label.
|
// Save the label set of the classic histogram without suffix and bucket `le` label.
|
||||||
|
@ -275,7 +272,10 @@ func (p *NHCBParser) handleClassicHistogramSeries(lset labels.Labels) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NHCBParser) processClassicHistogramSeries(lset labels.Labels, suffix string, updateHist func(*convertnhcb.TempHistogram)) {
|
func (p *NHCBParser) processClassicHistogramSeries(lset labels.Labels, suffix string, updateHist func(*convertnhcb.TempHistogram)) {
|
||||||
p.isCollationInProgress = true
|
if p.state != stateCollecting {
|
||||||
|
p.storeBaseLabels()
|
||||||
|
}
|
||||||
|
p.state = stateCollecting
|
||||||
p.tempLsetNHCB = convertnhcb.GetHistogramMetricBase(lset, suffix)
|
p.tempLsetNHCB = convertnhcb.GetHistogramMetricBase(lset, suffix)
|
||||||
p.storeExemplars()
|
p.storeExemplars()
|
||||||
updateHist(&p.tempNHCB)
|
updateHist(&p.tempNHCB)
|
||||||
|
@ -308,9 +308,9 @@ func (p *NHCBParser) swapExemplars() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// processNHCB converts the collated classic histogram series to NHCB and caches the info
|
// processNHCB converts the collated classic histogram series to NHCB and caches the info
|
||||||
// to be returned to callers.
|
// to be returned to callers. Retruns true if the conversion was successful.
|
||||||
func (p *NHCBParser) processNHCB() bool {
|
func (p *NHCBParser) processNHCB() bool {
|
||||||
if !p.isCollationInProgress {
|
if p.state != stateCollecting {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
ub := make([]float64, 0, len(p.tempNHCB.BucketCounts))
|
ub := make([]float64, 0, len(p.tempNHCB.BucketCounts))
|
||||||
|
@ -338,7 +338,6 @@ func (p *NHCBParser) processNHCB() bool {
|
||||||
p.lsetNHCB = p.tempLsetNHCB
|
p.lsetNHCB = p.tempLsetNHCB
|
||||||
p.swapExemplars()
|
p.swapExemplars()
|
||||||
p.tempNHCB = convertnhcb.NewTempHistogram()
|
p.tempNHCB = convertnhcb.NewTempHistogram()
|
||||||
p.isCollationInProgress = false
|
p.state = stateEmitting
|
||||||
p.justInsertedNHCB = true
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -3891,8 +3891,8 @@ metric: <
|
||||||
JobName: "test",
|
JobName: "test",
|
||||||
SampleLimit: 100,
|
SampleLimit: 100,
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
ScrapeInterval: model.Duration(100 * time.Millisecond),
|
ScrapeInterval: model.Duration(50 * time.Millisecond),
|
||||||
ScrapeTimeout: model.Duration(100 * time.Millisecond),
|
ScrapeTimeout: model.Duration(25 * time.Millisecond),
|
||||||
AlwaysScrapeClassicHistograms: tc.alwaysScrapeClassicHistograms,
|
AlwaysScrapeClassicHistograms: tc.alwaysScrapeClassicHistograms,
|
||||||
ConvertClassicHistogramsToNHCB: tc.convertClassicHistToNHCB,
|
ConvertClassicHistogramsToNHCB: tc.convertClassicHistToNHCB,
|
||||||
}
|
}
|
||||||
|
@ -3931,7 +3931,7 @@ metric: <
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
sp, err := newScrapePool(config, simpleStorage, 0, nil, nil, &Options{EnableNativeHistogramsIngestion: true}, newTestScrapeMetrics(t))
|
sp, err := newScrapePool(config, simpleStorage, 0, nil, nil, &Options{DiscoveryReloadInterval: model.Duration(10 * time.Millisecond), EnableNativeHistogramsIngestion: true}, newTestScrapeMetrics(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer sp.stop()
|
defer sp.stop()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue