Skip to content

Commit

Permalink
feat: adding prefix_prefer_source option
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Stanford committed Jan 5, 2024
1 parent 0e3c836 commit e1bf9ec
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 20 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ tenant:
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
# env: CT_TENANT_PREFIX
prefix: foobar-

# 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.
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
# env: CT_TENANT_PREFIX
prefix_prefer_source: false
```
### Prometheus configuration example
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.12.3
1.12.4
13 changes: 7 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ type config struct {
}

Tenant struct {
Label string `env:"CT_TENANT_LABEL"`
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"`
Label string `env:"CT_TENANT_LABEL"`
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
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ log_response_errors: true
tenant:
label: tenant
prefix: ""
prefix_prefer_source: false
label_remove: true
header: X-Scope-OrgID
default: ""
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.0 # This is the chart version
appVersion: 1.11.0 # version number of the application being deployed.
version: 0.4.1 # This is the chart version
appVersion: 1.12.4 # version number of the application being deployed.
type: application
sources:
- https://github.com/blind-oracle/cortex-tenant
1 change: 1 addition & 0 deletions deploy/k8s/chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data:
tenant:
label: {{ .Values.config.tenant.label }}
prefix: {{ .Values.config.tenant.prefix }}
prefix_prefer_source: {{ .Values.config.tenant.prefix_prefer_source }}
label_remove: {{ .Values.config.tenant.label_remove }}
header: {{ .Values.config.tenant.header }}
default: {{ .Values.config.tenant.default }}
Expand Down
6 changes: 6 additions & 0 deletions deploy/k8s/chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@
"title": "Prefix",
"description": "Optional hard-coded prefix with delimeter for all tenant values"
},
"prefix_prefer_source": {
"type": "boolean",
"title": "Prefix Prefer Source",
"description": "If true will use the tenant ID of the inbound request as the prefix of the new tenant id.",
"default": false
},
"label_remove": {
"type": "boolean",
"title": "Label Remove",
Expand Down
4 changes: 4 additions & 0 deletions deploy/k8s/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ config:
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
# (env: `CT_TENANT_PREFIX`)
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.
# (env: `CT_TENANT_PREFIX_PREFER_SOURCE`)
prefix_prefer_source: false
# -- Whether to remove the tenant label from the request
# (env: `CT_TENANT_LABEL_REMOVE`)
label_remove: false
Expand Down
4 changes: 4 additions & 0 deletions deploy/k8s/manifests/config-file-configmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ data:
# Delimeters allowed for use:
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
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.
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
prefix_prefer_source: false
# Whether to remove the tenant label from the request
label_remove: false
# To which header to add the tenant ID
Expand Down
26 changes: 17 additions & 9 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,22 @@ 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")) + "-"
}
}

clientIP := ctx.RemoteAddr()
reqID, _ := uuid.NewRandom()

if len(wrReqIn.Timeseries) == 0 {
// 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, p.cfg.Tenant.Default, wrReqIn)
r := p.send(clientIP, reqID, tenantPrefix, 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 @@ -201,7 +209,7 @@ func (p *processor) handle(ctx *fh.RequestCtx) {

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

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

Expand Down Expand Up @@ -303,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, m map[string]*prompb.WriteRequest) (res []result) {
func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, tenantPrefix string, m map[string]*prompb.WriteRequest) (res []result) {
var wg sync.WaitGroup
res = make([]result, len(m))

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

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

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

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

Expand All @@ -374,7 +382,7 @@ func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenant string, wr *
return
}

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

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

func (p *processor) fillRequestHeaders(
clientIP net.Addr, reqID uuid.UUID, tenant string, req *fh.Request) {
clientIP net.Addr, reqID uuid.UUID, tenantPrefix string, 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 p.cfg.Tenant.Prefix != "" {
tenant = p.cfg.Tenant.Prefix + tenant
if tenantPrefix != "" {
tenant = tenantPrefix + 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 @@ -219,7 +219,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 @@ -234,7 +234,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, "my-tenant", req)
p.fillRequestHeaders(clientIP, reqID, "foobar-", "my-tenant", req)

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

0 comments on commit e1bf9ec

Please sign in to comment.