Skip to content

Commit 3c8c530

Browse files
committed
Addressing some comments
Signed-off-by: Alan Protasio <[email protected]>
1 parent ba5191b commit 3c8c530

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

pkg/querier/blocks_store_queryable.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,12 @@ func (q *blocksStoreQuerier) fetchSeriesFromStores(
578578
reqStats = stats.FromContext(ctx)
579579
)
580580
matchers, shardingInfo, err := querysharding.ExtractShardingInfo(matchers)
581-
convertedMatchers := convertMatchersToLabelMatcher(matchers)
582581

583582
if err != nil {
584583
return nil, nil, nil, 0, err
584+
585585
}
586+
convertedMatchers := convertMatchersToLabelMatcher(matchers)
586587

587588
// Concurrently fetch series from all clients.
588589
for c, blockIDs := range clients {

pkg/querier/tripperware/instantquery/InstantQueryMiddlewares.go renamed to pkg/querier/tripperware/instantquery/instant_query_middlewares.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/cortexproject/cortex/pkg/querier/tripperware"
99
)
1010

11-
// Config for query_range middleware chain.
11+
// Config for instant query middleware chain.
1212
type Config struct {
1313
VerticalShardSize int `yaml:"vertical_shard_size"`
1414
}

pkg/querier/tripperware/shard_by_query.go renamed to pkg/querier/tripperware/shard_by.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"context"
55

66
"github.com/go-kit/log"
7+
"github.com/go-kit/log/level"
78
"github.com/thanos-io/thanos/pkg/querysharding"
89
"github.com/thanos-io/thanos/pkg/store/storepb"
910

1011
cquerysharding "github.com/cortexproject/cortex/pkg/querysharding"
12+
util_log "github.com/cortexproject/cortex/pkg/util/log"
1113
)
1214

1315
func ShardByMiddleware(logger log.Logger, limits Limits, merger Merger, numShards int) Middleware {
1416
return MiddlewareFunc(func(next Handler) Handler {
15-
return shardQueryByMiddleware{
17+
return shardBy{
1618
next: next,
1719
limits: limits,
1820
merger: merger,
@@ -22,7 +24,7 @@ func ShardByMiddleware(logger log.Logger, limits Limits, merger Merger, numShard
2224
})
2325
}
2426

25-
type shardQueryByMiddleware struct {
27+
type shardBy struct {
2628
next Handler
2729
limits Limits
2830
logger log.Logger
@@ -31,17 +33,18 @@ type shardQueryByMiddleware struct {
3133
numShards int
3234
}
3335

34-
func (s shardQueryByMiddleware) Do(ctx context.Context, r Request) (Response, error) {
36+
func (s shardBy) Do(ctx context.Context, r Request) (Response, error) {
37+
logger := util_log.WithContext(ctx, s.logger)
3538
analysis, err := s.queryAnalyzer.Analyze(r.GetQuery())
3639
if err != nil {
37-
return nil, err
40+
level.Warn(logger).Log("msg", "error sharding query", "q", r.GetQuery(), "err", err)
3841
}
3942

40-
if !analysis.IsShardable() {
43+
if err != nil || !analysis.IsShardable() {
4144
return s.next.Do(ctx, r)
4245
}
4346

44-
reqs := s.shardQuery(r, analysis)
47+
reqs := s.shardQuery(logger, r, analysis)
4548

4649
reqResps, err := DoRequests(ctx, s.next, reqs, s.limits)
4750
if err != nil {
@@ -60,7 +63,7 @@ func (s shardQueryByMiddleware) Do(ctx context.Context, r Request) (Response, er
6063
return response, nil
6164
}
6265

63-
func (s shardQueryByMiddleware) shardQuery(r Request, analysis querysharding.QueryAnalysis) []Request {
66+
func (s shardBy) shardQuery(l log.Logger, r Request, analysis querysharding.QueryAnalysis) []Request {
6467
reqs := make([]Request, s.numShards)
6568
for i := 0; i < s.numShards; i++ {
6669
q, err := cquerysharding.InjectShardingInfo(r.GetQuery(), &storepb.ShardInfo{
@@ -72,7 +75,7 @@ func (s shardQueryByMiddleware) shardQuery(r Request, analysis querysharding.Que
7275
reqs[i] = r.WithQuery(q)
7376

7477
if err != nil {
75-
s.logger.Log("error sharding query", "q", r.GetQuery())
78+
level.Warn(l).Log("msg", "error sharding query", "q", r.GetQuery(), "err", err)
7679
return []Request{r}
7780
}
7881
}

pkg/querier/tripperware/test_shard_by_query_utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func TestQueryShardQuery(t *testing.T, instantQueryCodec Codec, shardedPrometheu
8080
/ on ()
8181
http_requests_total`,
8282
},
83+
{
84+
name: "problematic query",
85+
expression: `sum(a by(lanel)`,
86+
},
8387
}
8488

8589
shardableByLabels := []queries{

pkg/querysharding/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ func InjectShardingInfo(query string, shardInfo *storepb.ShardInfo) (string, err
2929
if err != nil {
3030
return "", err
3131
}
32+
eShardInfo := base64.StdEncoding.EncodeToString(b)
3233
parser.Inspect(expr, func(n parser.Node, _ []parser.Node) error {
3334
if selector, ok := n.(*parser.VectorSelector); ok {
3435
selector.LabelMatchers = append(selector.LabelMatchers, &labels.Matcher{
3536
Type: labels.MatchEqual,
3637
Name: CortexShardByLabel,
37-
Value: base64.StdEncoding.EncodeToString(b),
38+
Value: eShardInfo,
3839
})
3940
}
4041
return nil

0 commit comments

Comments
 (0)