Skip to content

Commit

Permalink
Fix AddLabelClient handling of multiple matching labels
Browse files Browse the repository at this point in the history
As reported in #665 the initial implementation fixed *some* cases but
not all. This fixes the typo and edge-case handling missed in the
previous PR.

Fixes #665
  • Loading branch information
jacksontj committed Sep 19, 2024
1 parent c042777 commit aa9a044
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/promclient/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,31 @@ func (c *AddLabelClient) Key() model.LabelSet {

func (c *AddLabelClient) filterMatchers(matchers []string) ([]string, bool, error) {
ret := make([]string, 0, len(matchers))
for i, matcher := range matchers {
for _, matcher := range matchers {
selectors, err := parser.ParseMetricSelector(matcher)
if err != nil {
return nil, true, err
}

filteredSelectors := make([]*labels.Matcher, 0, len(selectors))

// If the selector matches our value -- remove the selector
// if the selector doesn't match, return empty
for sI, s := range selectors {
for _, s := range selectors {
if v, ok := c.Labels[model.LabelName(s.Name)]; ok {
if s.Matches(string(v)) {
selectors = append(selectors[:sI], selectors[i+1:]...)
} else {
// If the selector doesn't match the labels from our client; we don't match
if !s.Matches(string(v)) {
return nil, false, nil
}
} else { // Otherwise if the selector isn't part of the `Labels` we add; we pass it along
filteredSelectors = append(filteredSelectors, s)
}
}
newMatcher, err := promhttputil.MatcherToString(selectors)
// If the selector is cleared -- then we skip it in the return
if len(filteredSelectors) == 0 {
continue
}
newMatcher, err := promhttputil.MatcherToString(filteredSelectors)
if err != nil {
return nil, false, err
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/promclient/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ func TestAddLabelClient(t *testing.T) {
labelValues: []string{"1"},
matchers: []string{`{b="1"}`},
},
{
labelSet: model.LabelSet{"b": "1", "c": "1"},
labelValues: []string{"1"},
matchers: []string{`{b="1", c="1"}`},
},
{
labelSet: model.LabelSet{"b": "1", "c": "1", "d": "1"},
labelValues: []string{"1"},
matchers: []string{`{b="1", c="1"}`},
},
{
labelSet: model.LabelSet{"b": "1", "c": "1", "d": "1"},
labelValues: []string{"1"},
matchers: []string{`{a="1", b="1", c="1"}`},
},
}

for i, test := range tests {
Expand Down

0 comments on commit aa9a044

Please sign in to comment.