Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue of wrong results when querying external label values #6339

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pkg/store/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func testLabelAPIs(t *testing.T, startStore func(extLset labels.Labels, append f
{start: timestamp.FromTime(minTime), end: timestamp.FromTime(maxTime), expectErr: errors.New("rpc error: code = InvalidArgument desc = label name parameter cannot be empty")},
{start: timestamp.FromTime(minTime), end: timestamp.FromTime(maxTime), label: "foo"},
{start: timestamp.FromTime(minTime), end: timestamp.FromTime(maxTime), label: "region", expectedValues: []string{"eu-west"}}, // External labels should be visible.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's uncertain whether this test should pass? There are no series, but external label is returned anyway? I didn't touch it because the comment says it should be visible. In case you agree that it's wrong too, I can tweak the PR to account for that (we would need to check len(vals) > 0 returned from storepb.LabelValuesResponse).

{
start: timestamp.FromTime(minTime),
end: timestamp.FromTime(maxTime),
label: "region",
matchers: []storepb.LabelMatcher{{Type: storepb.LabelMatcher_EQ, Name: "unknown", Value: "unknown"}},
},
},
},
{
Expand All @@ -72,6 +78,19 @@ func testLabelAPIs(t *testing.T, startStore func(extLset labels.Labels, append f
},
labelValuesCalls: []labelValuesCallCase{
{start: timestamp.FromTime(minTime), end: timestamp.FromTime(maxTime), label: "foo", expectedValues: []string{"foovalue1"}},
{
start: timestamp.FromTime(minTime),
end: timestamp.FromTime(maxTime),
label: "region",
matchers: []storepb.LabelMatcher{{Type: storepb.LabelMatcher_EQ, Name: "foo", Value: "unknown"}},
},
{
start: timestamp.FromTime(minTime),
end: timestamp.FromTime(maxTime),
label: "region",
matchers: []storepb.LabelMatcher{{Type: storepb.LabelMatcher_EQ, Name: "foo", Value: "foovalue1"}},
expectedValues: []string{"eu-west"},
},
},
},
{
Expand Down
15 changes: 9 additions & 6 deletions pkg/store/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,6 @@ func (p *PrometheusStore) LabelValues(ctx context.Context, r *storepb.LabelValue
}

extLset := p.externalLabelsFn()

// First check for matching external label which has priority.
if l := extLset.Get(r.Label); l != "" {
return &storepb.LabelValuesResponse{Values: []string{l}}, nil
}

match, matchers, err := matchesExternalLabels(r.Matchers, extLset)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
Expand All @@ -713,8 +707,12 @@ func (p *PrometheusStore) LabelValues(ctx context.Context, r *storepb.LabelValue
vals []string
)

l := extLset.Get(r.Label)
version, parseErr := semver.Parse(p.promVersion())
if len(matchers) == 0 || (parseErr == nil && version.GTE(baseVer)) {
if l != "" {
return &storepb.LabelValuesResponse{Values: []string{l}}, nil
}
vals, err = p.client.LabelValuesInGRPC(ctx, p.base, r.Label, matchers, r.Start, r.End)
if err != nil {
return nil, err
Expand All @@ -725,6 +723,11 @@ func (p *PrometheusStore) LabelValues(ctx context.Context, r *storepb.LabelValue
return nil, err
}

// Check for matching external label which has priority.
if l != "" && len(sers) > 0 {
return &storepb.LabelValuesResponse{Values: []string{l}}, nil
}

// Using set to handle duplicate values.
labelValuesSet := make(map[string]struct{})
for _, s := range sers {
Expand Down