refac: dedeuplicate labeldiff checks

Signed-off-by: Manik Rana <manikrana54@gmail.com>
This commit is contained in:
Manik Rana 2024-07-10 09:47:22 +05:30
parent c4b2a14590
commit b768e24e26
2 changed files with 25 additions and 32 deletions

View file

@ -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

View file

@ -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
}
}