mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-14 09:34:05 -08:00
Use the proper way to iterate labels
Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
This commit is contained in:
parent
7fccf1e6be
commit
fbbf10baad
|
@ -28,6 +28,8 @@ import (
|
||||||
"github.com/prometheus/prometheus/util/convertnhcb"
|
"github.com/prometheus/prometheus/util/convertnhcb"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var labelsMismatchError = errors.New("labels mismatch")
|
||||||
|
|
||||||
type NHCBParser struct {
|
type NHCBParser struct {
|
||||||
// The parser we're wrapping.
|
// The parser we're wrapping.
|
||||||
parser Parser
|
parser Parser
|
||||||
|
@ -198,50 +200,46 @@ func (p *NHCBParser) compareLabels() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare the labels.
|
// Compare the labels.
|
||||||
for _, l := range p.lset {
|
err := p.lset.Validate(func(l labels.Label) error {
|
||||||
if l.Name == labels.MetricName {
|
switch {
|
||||||
|
case l.Name == labels.BucketLabel:
|
||||||
|
case l.Name == labels.MetricName:
|
||||||
baseName := convertnhcb.GetHistogramMetricBaseName(l.Value)
|
baseName := convertnhcb.GetHistogramMetricBaseName(l.Value)
|
||||||
if baseName != p.lastBaseHistLabels.Get(labels.MetricName) {
|
if baseName != p.lastBaseHistLabels.Get(labels.MetricName) {
|
||||||
|
// Different metric name.
|
||||||
if p.typ == model.MetricTypeHistogram {
|
if p.typ == model.MetricTypeHistogram {
|
||||||
p.storeBaseLabels()
|
p.storeBaseLabels()
|
||||||
} else {
|
} else {
|
||||||
p.lastBaseHistLabels = labels.EmptyLabels()
|
p.lastBaseHistLabels = labels.EmptyLabels()
|
||||||
}
|
}
|
||||||
return true
|
return labelsMismatchError
|
||||||
}
|
}
|
||||||
continue
|
case l.Value != p.lastBaseHistLabels.Get(l.Name):
|
||||||
}
|
|
||||||
if l.Name == labels.BucketLabel {
|
|
||||||
// Ignore.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if l.Value != p.lastBaseHistLabels.Get(l.Name) {
|
|
||||||
// Different label value.
|
// Different label value.
|
||||||
if p.typ == model.MetricTypeHistogram {
|
if p.typ == model.MetricTypeHistogram {
|
||||||
p.storeBaseLabels()
|
p.storeBaseLabels()
|
||||||
} else {
|
} else {
|
||||||
p.lastBaseHistLabels = labels.EmptyLabels()
|
p.lastBaseHistLabels = labels.EmptyLabels()
|
||||||
}
|
}
|
||||||
return true
|
return labelsMismatchError
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
return false
|
})
|
||||||
|
return err == labelsMismatchError
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
func (p *NHCBParser) storeBaseLabels() {
|
func (p *NHCBParser) storeBaseLabels() {
|
||||||
builder := labels.Builder{}
|
builder := labels.Builder{}
|
||||||
for _, l := range p.lset {
|
p.lset.Range(func(l labels.Label) {
|
||||||
if l.Name == labels.MetricName {
|
switch {
|
||||||
|
case l.Name == labels.BucketLabel:
|
||||||
|
case l.Name == labels.MetricName:
|
||||||
builder.Set(l.Name, convertnhcb.GetHistogramMetricBaseName(l.Value))
|
builder.Set(l.Name, convertnhcb.GetHistogramMetricBaseName(l.Value))
|
||||||
continue
|
default:
|
||||||
|
builder.Set(l.Name, l.Value)
|
||||||
}
|
}
|
||||||
if l.Name == labels.BucketLabel {
|
})
|
||||||
// Ignore.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
builder.Set(l.Name, l.Value)
|
|
||||||
}
|
|
||||||
p.lastBaseHistLabels = builder.Labels()
|
p.lastBaseHistLabels = builder.Labels()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue