Skip to content

Commit

Permalink
store: fix e2e labelname tests
Browse files Browse the repository at this point in the history
Signed-off-by: Giedrius Statkevičius <[email protected]>
  • Loading branch information
GiedriusS committed Aug 30, 2021
1 parent 668fa1f commit 8f9a57f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/promclient/promclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ func (c *Client) SeriesInGRPC(ctx context.Context, base *url.URL, matchers []*la
return m.Data, c.get2xxResultWithGRPCErrors(ctx, "/prom_series HTTP[client]", &u, &m)
}

// LabelNames returns all known label names. It uses gRPC errors.
// LabelNames returns all known label names constrained by the given matchers. It uses gRPC errors.
// NOTE: This method is tested in pkg/store/prometheus_test.go against Prometheus.
func (c *Client) LabelNamesInGRPC(ctx context.Context, base *url.URL, matchers []storepb.LabelMatcher, startTime, endTime int64) ([]string, error) {
u := *base
Expand Down
3 changes: 2 additions & 1 deletion pkg/query/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ func (q *querier) LabelValues(name string, matchers ...*labels.Matcher) ([]strin
return resp.Values, warns, nil
}

// LabelNames returns all the unique label names present in the block in sorted order.
// LabelNames returns all the unique label names present in the block in sorted order constrained
// by the given matchers.
func (q *querier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
span, ctx := tracing.StartSpan(q.ctx, "querier_label_names")
defer span.Finish()
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/multitsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (s *MultiTSDBStore) Series(r *storepb.SeriesRequest, srv storepb.Store_Seri

}

// LabelNames returns all known label names.
// LabelNames returns all known label names constrained by the matchers.
func (s *MultiTSDBStore) LabelNames(ctx context.Context, req *storepb.LabelNamesRequest) (*storepb.LabelNamesResponse, error) {
span, ctx := tracing.StartSpan(ctx, "multitsdb_label_names")
defer span.Finish()
Expand Down
6 changes: 2 additions & 4 deletions pkg/store/storepb/rpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/store/storepb/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ service Store {
/// This heavily optimizes the resource usage on Querier / Federated Queries.
rpc Series(SeriesRequest) returns (stream SeriesResponse);

/// LabelNames returns all label names that is available.
/// Currently unimplemented in all Thanos implementations, because Query API does not implement this either.
/// LabelNames returns all label names constrained by the given matchers.
rpc LabelNames(LabelNamesRequest) returns (LabelNamesResponse);

/// LabelValues returns all label values for given label name.
Expand Down
11 changes: 10 additions & 1 deletion pkg/store/tsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"io"
"math"
"sort"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"
Expand Down Expand Up @@ -194,7 +195,7 @@ func (s *TSDBStore) Series(r *storepb.SeriesRequest, srv storepb.Store_SeriesSer
return nil
}

// LabelNames returns all known label names.
// LabelNames returns all known label names constrained with the given matchers.
func (s *TSDBStore) LabelNames(ctx context.Context, r *storepb.LabelNamesRequest) (
*storepb.LabelNamesResponse, error,
) {
Expand All @@ -213,6 +214,14 @@ func (s *TSDBStore) LabelNames(ctx context.Context, r *storepb.LabelNamesRequest
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

if len(res) > 0 {
for _, lbl := range s.extLset {
res = append(res, lbl.Name)
}
sort.Strings(res)
}

return &storepb.LabelNamesResponse{Names: res}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/store/tsdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestTSDBStore_LabelNames(t *testing.T) {
{
title: "add one label",
labels: []string{"foo", "foo"},
expectedNames: []string{"foo"},
expectedNames: []string{"foo", "region"},
timestamp: now.Unix(),
start: func() int64 {
return timestamp.FromTime(minTime)
Expand All @@ -246,7 +246,7 @@ func TestTSDBStore_LabelNames(t *testing.T) {
title: "add another label",
labels: []string{"bar", "bar"},
// We will get two labels here.
expectedNames: []string{"bar", "foo"},
expectedNames: []string{"bar", "foo", "region"},
timestamp: now.Unix(),
start: func() int64 {
return timestamp.FromTime(minTime)
Expand All @@ -269,7 +269,7 @@ func TestTSDBStore_LabelNames(t *testing.T) {
{
title: "get all labels",
labels: []string{"buz", "buz"},
expectedNames: []string{"bar", "buz", "foo"},
expectedNames: []string{"bar", "buz", "foo", "region"},
timestamp: now.Unix(),
start: func() int64 {
return timestamp.FromTime(minTime)
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,10 @@ func TestQueryLabelNames(t *testing.T) {

labelNames(t, ctx, q.HTTPEndpoint(), []storepb.LabelMatcher{{Type: storepb.LabelMatcher_EQ, Name: "__name__", Value: "up"}},
timestamp.FromTime(now.Add(-time.Hour)), timestamp.FromTime(now.Add(time.Hour)), func(res []string) bool {
// Expected result: [__name__, instance, job, prometheus, replica]
return len(res) == 5
// Expected result: [__name__, instance, job, prometheus, replica, receive, tenant_id]
// Pre-labelnames pushdown we've done Select() over all series and picked out the label names hence they all had external labels.
// With labelnames pushdown we had to extend the LabelNames() call to enrich the response with the external labelset with there are more than one label.
return len(res) == 7
},
)

Expand Down

0 comments on commit 8f9a57f

Please sign in to comment.