mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
refac: dedeuplicate labeldiff checks
Signed-off-by: Manik Rana <manikrana54@gmail.com>
This commit is contained in:
parent
c4b2a14590
commit
b768e24e26
|
@ -31,25 +31,6 @@ func (ls Labels) Len() int { return len(ls) }
|
|||
func (ls Labels) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] }
|
||||
func (ls Labels) Less(i, j int) bool { return ls[i].Name < ls[j].Name }
|
||||
|
||||
// ExtractNames returns an array of all Name in ls.
|
||||
func (ls Labels) ExtractNames() []string {
|
||||
names := make([]string, len(ls))
|
||||
for i, label := range ls {
|
||||
names[i] = label.Name
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// Contains returns true if the label set contains the provided label name.
|
||||
func (ls Labels) Contains(name string) bool {
|
||||
for _, label := range ls {
|
||||
if label.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Bytes returns ls as a byte slice.
|
||||
// It uses an byte invalid character as a separator and so should not be used for printing.
|
||||
func (ls Labels) Bytes(buf []byte) []byte {
|
||||
|
@ -392,6 +373,29 @@ func (ls Labels) ReleaseStrings(release func(string)) {
|
|||
}
|
||||
}
|
||||
|
||||
// ExtractNames returns an array of all Name in ls.
|
||||
func (ls Labels) ExtractNames() []string {
|
||||
names := make([]string, len(ls))
|
||||
for i, label := range ls {
|
||||
names[i] = label.Name
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// Method to check if the Labels contains any of the provided label Names
|
||||
// returns true if any of the provided label names are present in the Labels
|
||||
// return false if no Names are provided
|
||||
func (ls Labels) Contains(names ...string) bool {
|
||||
for _, name := range names {
|
||||
for _, label := range ls {
|
||||
if label.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Builder allows modifying Labels.
|
||||
type Builder struct {
|
||||
base Labels
|
||||
|
|
|
@ -252,21 +252,10 @@ loop:
|
|||
|
||||
newLbs = newLbs.DropMetricName()
|
||||
switch p.mtype {
|
||||
case model.MetricTypeCounter:
|
||||
if !labels.Equal(lbs, newLbs) {
|
||||
return nil
|
||||
}
|
||||
case model.MetricTypeSummary:
|
||||
case model.MetricTypeCounter, model.MetricTypeHistogram, model.MetricTypeSummary:
|
||||
labelDiffs := lbs.MatchLabels(false, newLbs.ExtractNames()...)
|
||||
if labelDiffs.Len() != 0 {
|
||||
if !labelDiffs.Contains("quantile") || labelDiffs.Len() != 1 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
case model.MetricTypeHistogram:
|
||||
labelDiffs := lbs.MatchLabels(false, newLbs.ExtractNames()...)
|
||||
if labelDiffs.Len() != 0 {
|
||||
if !labelDiffs.Contains("le") || labelDiffs.Len() != 1 {
|
||||
if !labelDiffs.Contains("quantile", "le") || labelDiffs.Len() != 1 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue