Skip to content

Commit

Permalink
Rebased.
Browse files Browse the repository at this point in the history
Addressing PR comments.
Simplifying PrefixPreferSource logic
Adding example to readme
  • Loading branch information
matthewjstanford committed Jan 19, 2024
1 parent 3ed018e commit ef23a6e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ tenant:

# If true will use the tenant ID of the inbound request as the prefix of the new tenant id.
# Will be automatically suffixed with a `-` character.
# Example:
# Prometheus forwards metrics with `X-Scope-OrgID: Prom-A` set in the inbound request.
# This would result in the tenant prefix being set to `Prom-A-`.
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
# env: CT_TENANT_PREFIX
# env: CT_TENANT_PREFIX_PREFER_SOURCE
prefix_prefer_source: false
```
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.12.4
1.12.5
13 changes: 7 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ type config struct {
}

Tenant struct {
Label string `env:"CT_TENANT_LABEL"`
Label string `env:"CT_TENANT_LABEL"`
LabelList []string `yaml:"label_list" env:"CT_TENANT_LABEL_LIST" envSeparator:","`
Prefix string `yaml:"prefix" env:"CT_TENANT_PREFIX"`
LabelRemove bool `yaml:"label_remove" env:"CT_TENANT_LABEL_REMOVE"`
Header string `env:"CT_TENANT_HEADER"`
Default string `env:"CT_TENANT_DEFAULT"`
AcceptAll bool `yaml:"accept_all" env:"CT_TENANT_ACCEPT_ALL"`
Prefix string `yaml:"prefix" env:"CT_TENANT_PREFIX"`
PrefixPreferSource bool `yaml:"prefix_prefer_source" env:"CT_TENANT_PREFIX_PREFER_SOURCE`
LabelRemove bool `yaml:"label_remove" env:"CT_TENANT_LABEL_REMOVE"`
Header string `env:"CT_TENANT_HEADER"`
Default string `env:"CT_TENANT_DEFAULT"`
AcceptAll bool `yaml:"accept_all" env:"CT_TENANT_ACCEPT_ALL"`
}

pipeIn *fhu.InmemoryListener
Expand Down
4 changes: 2 additions & 2 deletions deploy/k8s/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v2
description: A Helm Chart for cortex-tenant
name: cortex-tenant
version: 0.4.1 # This is the chart version
appVersion: 1.12.4 # version number of the application being deployed.
version: 0.4.2 # This is the chart version
appVersion: 1.12.5 # version number of the application being deployed.
type: application
sources:
- https://github.com/blind-oracle/cortex-tenant
4 changes: 4 additions & 0 deletions deploy/k8s/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ config:
# (env: `CT_TENANT_LABEL`)
label: tenant
# -- List of labels examined for tenant information. If set takes precedent over `label`
# (env: `CT_TENANT_LABEL_LIST`)
label_list: []
# -- Optional hard-coded prefix with delimeter for all tenant values.
# Delimeters allowed for use:
Expand All @@ -109,6 +110,9 @@ config:
prefix: ""
# -- If true will use the tenant ID of the inbound request as the prefix of the new tenant id.
# Will be automatically suffixed with a `-` character.
# Example:
# Prometheus forwards metrics with `X-Scope-OrgID: Prom-A` set in the inbound request.
# This would result in the tenant prefix being set to `Prom-A-`.
# (env: `CT_TENANT_PREFIX_PREFER_SOURCE`)
prefix_prefer_source: false
# -- Whether to remove the tenant label from the request
Expand Down
3 changes: 3 additions & 0 deletions deploy/k8s/manifests/config-file-configmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ data:
prefix: ""
# If true will use the tenant ID of the inbound request as the prefix of the new tenant id.
# Will be automatically suffixed with a `-` character.
# Example:
# Prometheus forwards metrics with `X-Scope-OrgID: Prom-A` set in the inbound request.
# This would result in the tenant prefix being set to `Prom-A-`.
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
prefix_prefer_source: false
# Whether to remove the tenant label from the request
Expand Down
25 changes: 12 additions & 13 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ func (p *processor) handle(ctx *fh.RequestCtx) {
return
}

tenantPrefix := p.cfg.Tenant.Prefix

if p.cfg.Tenant.PrefixPreferSource {
if string(ctx.Request.Header.Peek("X-Scope-OrgID")) != "" {
tenantPrefix = string(ctx.Request.Header.Peek("X-Scope-OrgID")) + "-"
sourceTenantPrefix := string(ctx.Request.Header.Peek(p.cfg.Tenant.Header))
if sourceTenantPrefix != "" {
p.cfg.Tenant.Prefix = sourceTenantPrefix + "-"
}
}

Expand All @@ -185,7 +184,7 @@ func (p *processor) handle(ctx *fh.RequestCtx) {
// If there's metadata - just accept the request and drop it
if len(wrReqIn.Metadata) > 0 {
if p.cfg.Metadata && p.cfg.Tenant.Default != "" {
r := p.send(clientIP, reqID, tenantPrefix, p.cfg.Tenant.Default, wrReqIn)
r := p.send(clientIP, reqID, p.cfg.Tenant.Default, wrReqIn)
if r.err != nil {
ctx.Error(err.Error(), fh.StatusInternalServerError)
p.Errorf("src=%s req_id=%s: unable to proxy metadata: %s", clientIP, reqID, r.err)
Expand All @@ -210,7 +209,7 @@ func (p *processor) handle(ctx *fh.RequestCtx) {

metricTenant := ""
var errs *me.Error
results := p.dispatch(clientIP, reqID, tenantPrefix, m)
results := p.dispatch(clientIP, reqID, m)

code, body := 0, []byte("Ok")

Expand Down Expand Up @@ -312,7 +311,7 @@ func (p *processor) marshal(wr *prompb.WriteRequest) (bufOut []byte, err error)
return snappy.Encode(nil, b), nil
}

func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, tenantPrefix string, m map[string]*prompb.WriteRequest) (res []result) {
func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, m map[string]*prompb.WriteRequest) (res []result) {
var wg sync.WaitGroup
res = make([]result, len(m))

Expand All @@ -323,7 +322,7 @@ func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, tenantPrefix st
go func(idx int, tenant string, wrReq *prompb.WriteRequest) {
defer wg.Done()

r := p.send(clientIP, reqID, tenantPrefix, tenant, wrReq)
r := p.send(clientIP, reqID, tenant, wrReq)
res[idx] = r
}(i, tenant, wrReq)

Expand Down Expand Up @@ -368,7 +367,7 @@ func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err
return
}

func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenantPrefix string, tenant string, wr *prompb.WriteRequest) (r result) {
func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenant string, wr *prompb.WriteRequest) (r result) {
start := time.Now()
r.tenant = tenant

Expand All @@ -386,7 +385,7 @@ func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenantPrefix string
return
}

p.fillRequestHeaders(clientIP, reqID, tenantPrefix, tenant, req)
p.fillRequestHeaders(clientIP, reqID, tenant, req)

if p.auth.egressHeader != nil {
req.Header.SetBytesV("Authorization", p.auth.egressHeader)
Expand All @@ -410,14 +409,14 @@ func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenantPrefix string
}

func (p *processor) fillRequestHeaders(
clientIP net.Addr, reqID uuid.UUID, tenantPrefix string, tenant string, req *fh.Request) {
clientIP net.Addr, reqID uuid.UUID, tenant string, req *fh.Request) {
req.Header.Set("Content-Encoding", "snappy")
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
req.Header.Set("X-Cortex-Tenant-Client", clientIP.String())
req.Header.Set("X-Cortex-Tenant-ReqID", reqID.String())
if tenantPrefix != "" {
tenant = tenantPrefix + tenant
if p.cfg.Tenant.Prefix != "" {
tenant = p.cfg.Tenant.Prefix + tenant
}
req.Header.Set(p.cfg.Tenant.Header, tenant)
}
Expand Down
4 changes: 2 additions & 2 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func Test_request_headers(t *testing.T) {
req := fh.AcquireRequest()
clientIP, _ := net.ResolveIPAddr("ip", "1.1.1.1")
reqID, _ := uuid.NewRandom()
p.fillRequestHeaders(clientIP, reqID, "", "my-tenant", req)
p.fillRequestHeaders(clientIP, reqID, "my-tenant", req)

assert.Equal(t, "snappy", string(req.Header.Peek("Content-Encoding")))
assert.Equal(t, "my-tenant", string(req.Header.Peek("X-Scope-OrgID")))
Expand All @@ -241,7 +241,7 @@ func Test_request_headers_with_prefix(t *testing.T) {
req := fh.AcquireRequest()
clientIP, _ := net.ResolveIPAddr("ip", "1.1.1.1")
reqID, _ := uuid.NewRandom()
p.fillRequestHeaders(clientIP, reqID, "foobar-", "my-tenant", req)
p.fillRequestHeaders(clientIP, reqID, "my-tenant", req)

assert.Equal(t, "foobar-my-tenant", string(req.Header.Peek("X-Scope-OrgID")))
}
Expand Down

0 comments on commit ef23a6e

Please sign in to comment.