From a117a8e27388a3a19fed035d9f75178df747bf89 Mon Sep 17 00:00:00 2001 From: Christian Simon Date: Thu, 26 Nov 2020 13:57:16 +0000 Subject: [PATCH] Add tenant resolver (#3486) * Add tenant resolver package This implements the multi tenant resolver as described by the [proposal] for multi tenant query-federation. By default it behaves like before, but it's implementation can be swapped out. [proposal]: https://github.com/cortexproject/cortex/pull/3364 Signed-off-by: Christian Simon * Replace usages of `ExtractOrgID` Use TenantID or UserID depending on which of the methods are meant to be used. Signed-off-by: Christian Simon * Replace usages of `ExtractOrgIDFromHTTPRequest` This is replaced by ExtractTenantIDFromHTTPRequest, which makes sure that exactly one tenant ID is set. Signed-off-by: Christian Simon * Add methods to `tenant` package to use resolver directly Signed-off-by: Christian Simon * Remove UserID method from Resolver interface We need a better definition for what we are trying to achieve with UserID before we can add it to the interface Signed-off-by: Christian Simon * Update comment on the TenantID/TenantIDs Signed-off-by: Christian Simon * Improve performance of NormalizeTenantIDs - reduce allocations by reusing the input slice during de-duplication Signed-off-by: Christian Simon --- pkg/distributor/distributor.go | 3 ++- pkg/distributor/distributor_test.go | 5 +++-- pkg/distributor/query.go | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/distributor/distributor.go b/pkg/distributor/distributor.go index c575851b2..d1445c801 100644 --- a/pkg/distributor/distributor.go +++ b/pkg/distributor/distributor.go @@ -26,6 +26,7 @@ import ( "github.com/cortexproject/cortex/pkg/prom1/storage/metric" "github.com/cortexproject/cortex/pkg/ring" ring_client "github.com/cortexproject/cortex/pkg/ring/client" + "github.com/cortexproject/cortex/pkg/tenant" "github.com/cortexproject/cortex/pkg/util" "github.com/cortexproject/cortex/pkg/util/extract" "github.com/cortexproject/cortex/pkg/util/limiter" @@ -384,7 +385,7 @@ func (d *Distributor) validateSeries(ts ingester_client.PreallocTimeseries, user // Push implements client.IngesterServer func (d *Distributor) Push(ctx context.Context, req *client.WriteRequest) (*client.WriteResponse, error) { - userID, err := user.ExtractOrgID(ctx) + userID, err := tenant.TenantID(ctx) if err != nil { return nil, err } diff --git a/pkg/distributor/distributor_test.go b/pkg/distributor/distributor_test.go index e2975f0da..b0fa6ab4d 100644 --- a/pkg/distributor/distributor_test.go +++ b/pkg/distributor/distributor_test.go @@ -32,6 +32,7 @@ import ( ring_client "github.com/cortexproject/cortex/pkg/ring/client" "github.com/cortexproject/cortex/pkg/ring/kv" "github.com/cortexproject/cortex/pkg/ring/kv/consul" + "github.com/cortexproject/cortex/pkg/tenant" "github.com/cortexproject/cortex/pkg/util" "github.com/cortexproject/cortex/pkg/util/chunkcompat" "github.com/cortexproject/cortex/pkg/util/flagext" @@ -426,7 +427,7 @@ func TestDistributor_PushHAInstances(t *testing.T) { d.HATracker = r } - userID, err := user.ExtractOrgID(ctx) + userID, err := tenant.TenantID(ctx) assert.NoError(t, err) err = d.HATracker.checkReplica(ctx, userID, tc.cluster, tc.acceptedReplica) assert.NoError(t, err) @@ -1322,7 +1323,7 @@ func (i *mockIngester) Push(ctx context.Context, req *client.WriteRequest, opts i.metadata = map[uint32]map[client.MetricMetadata]struct{}{} } - orgid, err := user.ExtractOrgID(ctx) + orgid, err := tenant.TenantID(ctx) if err != nil { return nil, err } diff --git a/pkg/distributor/query.go b/pkg/distributor/query.go index 8f08268cd..9628fc643 100644 --- a/pkg/distributor/query.go +++ b/pkg/distributor/query.go @@ -9,11 +9,11 @@ import ( "github.com/prometheus/common/model" "github.com/prometheus/prometheus/pkg/labels" "github.com/weaveworks/common/instrument" - "github.com/weaveworks/common/user" "github.com/cortexproject/cortex/pkg/ingester/client" ingester_client "github.com/cortexproject/cortex/pkg/ingester/client" "github.com/cortexproject/cortex/pkg/ring" + "github.com/cortexproject/cortex/pkg/tenant" "github.com/cortexproject/cortex/pkg/util" "github.com/cortexproject/cortex/pkg/util/extract" grpc_util "github.com/cortexproject/cortex/pkg/util/grpc" @@ -76,7 +76,7 @@ func (d *Distributor) QueryStream(ctx context.Context, from, to model.Time, matc // GetIngestersForQuery returns a replication set including all ingesters that should be queried // to fetch series matching input label matchers. func (d *Distributor) GetIngestersForQuery(ctx context.Context, matchers ...*labels.Matcher) (ring.ReplicationSet, error) { - userID, err := user.ExtractOrgID(ctx) + userID, err := tenant.TenantID(ctx) if err != nil { return ring.ReplicationSet{}, err } @@ -107,7 +107,7 @@ func (d *Distributor) GetIngestersForQuery(ctx context.Context, matchers ...*lab // GetIngestersForMetadata returns a replication set including all ingesters that should be queried // to fetch metadata (eg. label names/values or series). func (d *Distributor) GetIngestersForMetadata(ctx context.Context) (ring.ReplicationSet, error) { - userID, err := user.ExtractOrgID(ctx) + userID, err := tenant.TenantID(ctx) if err != nil { return ring.ReplicationSet{}, err }