Skip to content

Commit 4867ff5

Browse files
authored
Merge pull request #62 from matthewjstanford/prefix_prefer_source
feat: adding prefix_prefer_source option
2 parents 763b622 + 002fca9 commit 4867ff5

File tree

11 files changed

+56
-31
lines changed

11 files changed

+56
-31
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ tenant:
155155
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
156156
# env: CT_TENANT_PREFIX
157157
prefix: foobar-
158+
159+
# If true will use the tenant ID of the inbound request as the prefix of the new tenant id.
160+
# Will be automatically suffixed with a `-` character.
161+
# Example:
162+
# Prometheus forwards metrics with `X-Scope-OrgID: Prom-A` set in the inbound request.
163+
# This would result in the tenant prefix being set to `Prom-A-`.
164+
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
165+
# env: CT_TENANT_PREFIX_PREFER_SOURCE
166+
prefix_prefer_source: false
158167
```
159168
160169
### Prometheus configuration example

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.12.4
1+
1.12.5

config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ type config struct {
3737
}
3838

3939
Tenant struct {
40-
Label string `env:"CT_TENANT_LABEL"`
41-
LabelList []string `yaml:"label_list" env:"CT_TENANT_LABEL_LIST" envSeparator:","`
42-
Prefix string `yaml:"prefix" env:"CT_TENANT_PREFIX"`
43-
LabelRemove bool `yaml:"label_remove" env:"CT_TENANT_LABEL_REMOVE"`
44-
Header string `env:"CT_TENANT_HEADER"`
45-
Default string `env:"CT_TENANT_DEFAULT"`
46-
AcceptAll bool `yaml:"accept_all" env:"CT_TENANT_ACCEPT_ALL"`
40+
Label string `env:"CT_TENANT_LABEL"`
41+
LabelList []string `yaml:"label_list" env:"CT_TENANT_LABEL_LIST" envSeparator:","`
42+
Prefix string `yaml:"prefix" env:"CT_TENANT_PREFIX"`
43+
PrefixPreferSource bool `yaml:"prefix_prefer_source" env:"CT_TENANT_PREFIX_PREFER_SOURCE`
44+
LabelRemove bool `yaml:"label_remove" env:"CT_TENANT_LABEL_REMOVE"`
45+
Header string `env:"CT_TENANT_HEADER"`
46+
Default string `env:"CT_TENANT_DEFAULT"`
47+
AcceptAll bool `yaml:"accept_all" env:"CT_TENANT_ACCEPT_ALL"`
4748
}
4849

4950
pipeIn *fhu.InmemoryListener

config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tenant:
2525
- tenant
2626
- other_tenant
2727
prefix: ""
28+
prefix_prefer_source: false
2829
label_remove: true
2930
header: X-Scope-OrgID
3031
default: ""

deploy/k8s/chart/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apiVersion: v2
22
description: A Helm Chart for cortex-tenant
33
name: cortex-tenant
4-
version: 0.4.1 # This is the chart version
5-
appVersion: 1.12.4 # version number of the application being deployed.
4+
version: 0.4.2 # This is the chart version
5+
appVersion: 1.12.5 # version number of the application being deployed.
66
type: application
77
sources:
88
- https://github.com/blind-oracle/cortex-tenant

deploy/k8s/chart/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ data:
3030
{{- . | toYaml | nindent 8 }}
3131
{{- end }}
3232
prefix: {{ .Values.config.tenant.prefix }}
33+
prefix_prefer_source: {{ .Values.config.tenant.prefix_prefer_source }}
3334
label_remove: {{ .Values.config.tenant.label_remove }}
3435
header: {{ .Values.config.tenant.header }}
3536
default: {{ .Values.config.tenant.default }}

deploy/k8s/chart/values.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@
275275
"title": "Prefix",
276276
"description": "Optional hard-coded prefix with delimeter for all tenant values"
277277
},
278+
"prefix_prefer_source": {
279+
"type": "boolean",
280+
"title": "Prefix Prefer Source",
281+
"description": "If true will use the tenant ID of the inbound request as the prefix of the new tenant id.",
282+
"default": false
283+
},
278284
"label_remove": {
279285
"type": "boolean",
280286
"title": "Label Remove",

deploy/k8s/chart/values.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ config:
101101
# (env: `CT_TENANT_LABEL`)
102102
label: tenant
103103
# -- List of labels examined for tenant information. If set takes precedent over `label`
104+
# (env: `CT_TENANT_LABEL_LIST`)
104105
label_list: []
105106
# -- Optional hard-coded prefix with delimeter for all tenant values.
106107
# Delimeters allowed for use:
107108
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
108109
# (env: `CT_TENANT_PREFIX`)
109110
prefix: ""
111+
# -- If true will use the tenant ID of the inbound request as the prefix of the new tenant id.
112+
# Will be automatically suffixed with a `-` character.
113+
# Example:
114+
# Prometheus forwards metrics with `X-Scope-OrgID: Prom-A` set in the inbound request.
115+
# This would result in the tenant prefix being set to `Prom-A-`.
116+
# (env: `CT_TENANT_PREFIX_PREFER_SOURCE`)
117+
prefix_prefer_source: false
110118
# -- Whether to remove the tenant label from the request
111119
# (env: `CT_TENANT_LABEL_REMOVE`)
112120
label_remove: false

deploy/k8s/manifests/config-file-configmap.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ data:
4242
# Delimeters allowed for use:
4343
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
4444
prefix: ""
45+
# If true will use the tenant ID of the inbound request as the prefix of the new tenant id.
46+
# Will be automatically suffixed with a `-` character.
47+
# Example:
48+
# Prometheus forwards metrics with `X-Scope-OrgID: Prom-A` set in the inbound request.
49+
# This would result in the tenant prefix being set to `Prom-A-`.
50+
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
51+
prefix_prefer_source: false
4552
# Whether to remove the tenant label from the request
4653
label_remove: false
4754
# To which header to add the tenant ID

processor.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,23 @@ func (p *processor) handle(ctx *fh.RequestCtx) {
170170
return
171171
}
172172

173+
tenantPrefix := p.cfg.Tenant.Prefix
174+
175+
if p.cfg.Tenant.PrefixPreferSource {
176+
sourceTenantPrefix := string(ctx.Request.Header.Peek(p.cfg.Tenant.Header))
177+
if sourceTenantPrefix != "" {
178+
tenantPrefix = sourceTenantPrefix + "-"
179+
}
180+
}
181+
173182
clientIP := ctx.RemoteAddr()
174183
reqID, _ := uuid.NewRandom()
175184

176185
if len(wrReqIn.Timeseries) == 0 {
177186
// If there's metadata - just accept the request and drop it
178187
if len(wrReqIn.Metadata) > 0 {
179188
if p.cfg.Metadata && p.cfg.Tenant.Default != "" {
180-
r := p.send(clientIP, reqID, p.cfg.Tenant.Default, wrReqIn)
189+
r := p.send(clientIP, reqID, tenantPrefix+p.cfg.Tenant.Default, wrReqIn)
181190
if r.err != nil {
182191
ctx.Error(err.Error(), fh.StatusInternalServerError)
183192
p.Errorf("src=%s req_id=%s: unable to proxy metadata: %s", clientIP, reqID, r.err)
@@ -202,7 +211,7 @@ func (p *processor) handle(ctx *fh.RequestCtx) {
202211

203212
metricTenant := ""
204213
var errs *me.Error
205-
results := p.dispatch(clientIP, reqID, m)
214+
results := p.dispatch(clientIP, reqID, tenantPrefix, m)
206215

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

@@ -304,7 +313,7 @@ func (p *processor) marshal(wr *prompb.WriteRequest) (bufOut []byte, err error)
304313
return snappy.Encode(nil, b), nil
305314
}
306315

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

@@ -317,7 +326,7 @@ func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, m map[string]*p
317326

318327
r := p.send(clientIP, reqID, tenant, wrReq)
319328
res[idx] = r
320-
}(i, tenant, wrReq)
329+
}(i, tenantPrefix+tenant, wrReq)
321330

322331
i++
323332
}
@@ -408,9 +417,6 @@ func (p *processor) fillRequestHeaders(
408417
req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
409418
req.Header.Set("X-Cortex-Tenant-Client", clientIP.String())
410419
req.Header.Set("X-Cortex-Tenant-ReqID", reqID.String())
411-
if p.cfg.Tenant.Prefix != "" {
412-
tenant = p.cfg.Tenant.Prefix + tenant
413-
}
414420
req.Header.Set(p.cfg.Tenant.Header, tenant)
415421
}
416422

0 commit comments

Comments
 (0)