Skip to content

Commit 1fca96f

Browse files
move distributed query tenant propagation to protobuf to prevent header mismatch
Signed-off-by: Piyush Sharma <piyushsharma04321@gmail.com>
1 parent 15585ff commit 1fca96f

5 files changed

Lines changed: 173 additions & 75 deletions

File tree

pkg/api/query/grpc.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/thanos-io/thanos/pkg/query"
2222
"github.com/thanos-io/thanos/pkg/store/labelpb"
2323
"github.com/thanos-io/thanos/pkg/store/storepb/prompb"
24+
"github.com/thanos-io/thanos/pkg/tenancy"
2425
"github.com/thanos-io/thanos/pkg/tracing"
2526
)
2627

@@ -66,6 +67,10 @@ func RegisterQueryServer(queryServer querypb.QueryServer) func(*grpc.Server) {
6667
func (g *GRPCAPI) Query(request *querypb.QueryRequest, server querypb.Query_QueryServer) error {
6768
ctx := server.Context()
6869

70+
if request.Tenant != "" {
71+
ctx = context.WithValue(ctx, tenancy.TenantKey, request.Tenant)
72+
}
73+
6974
if request.TimeoutSeconds != 0 {
7075
var cancel context.CancelFunc
7176
timeout := time.Duration(request.TimeoutSeconds) * time.Second
@@ -189,6 +194,11 @@ func (g *GRPCAPI) Query(request *querypb.QueryRequest, server querypb.Query_Quer
189194

190195
func (g *GRPCAPI) QueryRange(request *querypb.QueryRangeRequest, srv querypb.Query_QueryRangeServer) error {
191196
ctx := srv.Context()
197+
198+
if request.Tenant != "" {
199+
ctx = context.WithValue(ctx, tenancy.TenantKey, request.Tenant)
200+
}
201+
192202
if request.TimeoutSeconds != 0 {
193203
var cancel context.CancelFunc
194204
timeout := time.Duration(request.TimeoutSeconds) * time.Second

pkg/api/query/querypb/query.pb.go

Lines changed: 153 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/query/querypb/query.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ message QueryRequest {
6262
// If set to 0 or 1, each TimeSeries is sent as a separate response (default behavior).
6363
int64 response_batch_size = 15;
6464

65+
string tenant = 16;
66+
6567
reserved 9;
6668
}
6769

@@ -118,6 +120,8 @@ message QueryRangeRequest {
118120
// If set to 0 or 1, each TimeSeries is sent as a separate response (default behavior).
119121
int64 response_batch_size = 17;
120122

123+
string tenant = 18;
124+
121125
reserved 11;
122126
}
123127

pkg/query/remote_engine.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/prometheus/prometheus/util/annotations"
2323
"github.com/prometheus/prometheus/util/stats"
2424
"github.com/thanos-io/thanos/pkg/tenancy"
25-
"google.golang.org/grpc/metadata"
2625

2726
"github.com/thanos-io/promql-engine/api"
2827
"github.com/thanos-io/thanos/pkg/api/query/querypb"
@@ -340,9 +339,10 @@ func (r *remoteQuery) Exec(ctx context.Context) *promql.Result {
340339

341340
r.samplesStats = stats.NewQuerySamples(false)
342341

342+
var requestTenant string
343343
if tenant := ctx.Value(tenancy.TenantKey); tenant != nil {
344344
if tenantStr, ok := tenant.(string); ok {
345-
qctx = metadata.AppendToOutgoingContext(qctx, tenancy.DefaultTenantHeader, tenantStr)
345+
requestTenant = tenantStr
346346
}
347347
}
348348

@@ -358,6 +358,7 @@ func (r *remoteQuery) Exec(ctx context.Context) *promql.Result {
358358
MaxResolutionSeconds: maxResolution,
359359
EnableDedup: true,
360360
ResponseBatchSize: store.DefaultResponseBatchSize,
361+
Tenant: requestTenant,
361362
}
362363

363364
qry, err := r.client.Query(qctx, request)
@@ -437,6 +438,7 @@ func (r *remoteQuery) Exec(ctx context.Context) *promql.Result {
437438
MaxResolutionSeconds: maxResolution,
438439
EnableDedup: true,
439440
ResponseBatchSize: store.DefaultResponseBatchSize,
441+
Tenant: requestTenant,
440442
}
441443
qry, err := r.client.QueryRange(qctx, request)
442444
if err != nil {

0 commit comments

Comments
 (0)