Skip to content

Commit 3ed018e

Browse files
feat: adding prefix_prefer_source option
1 parent 763b622 commit 3ed018e

File tree

9 files changed

+48
-18
lines changed

9 files changed

+48
-18
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ 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+
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
162+
# env: CT_TENANT_PREFIX
163+
prefix_prefer_source: false
158164
```
159165
160166
### Prometheus configuration example

config.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ 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+
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"`
4747
}
4848

4949
pipeIn *fhu.InmemoryListener

config.yml

+1
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/templates/configmap.yaml

+1
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

+6
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

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ config:
107107
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
108108
# (env: `CT_TENANT_PREFIX`)
109109
prefix: ""
110+
# -- If true will use the tenant ID of the inbound request as the prefix of the new tenant id.
111+
# Will be automatically suffixed with a `-` character.
112+
# (env: `CT_TENANT_PREFIX_PREFER_SOURCE`)
113+
prefix_prefer_source: false
110114
# -- Whether to remove the tenant label from the request
111115
# (env: `CT_TENANT_LABEL_REMOVE`)
112116
label_remove: false

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

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ 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+
# https://grafana.com/docs/mimir/latest/configure/about-tenant-ids/
48+
prefix_prefer_source: false
4549
# Whether to remove the tenant label from the request
4650
label_remove: false
4751
# To which header to add the tenant ID

processor.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,22 @@ 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+
if string(ctx.Request.Header.Peek("X-Scope-OrgID")) != "" {
177+
tenantPrefix = string(ctx.Request.Header.Peek("X-Scope-OrgID")) + "-"
178+
}
179+
}
180+
173181
clientIP := ctx.RemoteAddr()
174182
reqID, _ := uuid.NewRandom()
175183

176184
if len(wrReqIn.Timeseries) == 0 {
177185
// If there's metadata - just accept the request and drop it
178186
if len(wrReqIn.Metadata) > 0 {
179187
if p.cfg.Metadata && p.cfg.Tenant.Default != "" {
180-
r := p.send(clientIP, reqID, p.cfg.Tenant.Default, wrReqIn)
188+
r := p.send(clientIP, reqID, tenantPrefix, p.cfg.Tenant.Default, wrReqIn)
181189
if r.err != nil {
182190
ctx.Error(err.Error(), fh.StatusInternalServerError)
183191
p.Errorf("src=%s req_id=%s: unable to proxy metadata: %s", clientIP, reqID, r.err)
@@ -202,7 +210,7 @@ func (p *processor) handle(ctx *fh.RequestCtx) {
202210

203211
metricTenant := ""
204212
var errs *me.Error
205-
results := p.dispatch(clientIP, reqID, m)
213+
results := p.dispatch(clientIP, reqID, tenantPrefix, m)
206214

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

@@ -304,7 +312,7 @@ func (p *processor) marshal(wr *prompb.WriteRequest) (bufOut []byte, err error)
304312
return snappy.Encode(nil, b), nil
305313
}
306314

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

@@ -315,7 +323,7 @@ func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, m map[string]*p
315323
go func(idx int, tenant string, wrReq *prompb.WriteRequest) {
316324
defer wg.Done()
317325

318-
r := p.send(clientIP, reqID, tenant, wrReq)
326+
r := p.send(clientIP, reqID, tenantPrefix, tenant, wrReq)
319327
res[idx] = r
320328
}(i, tenant, wrReq)
321329

@@ -360,7 +368,7 @@ func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err
360368
return
361369
}
362370

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

@@ -378,7 +386,7 @@ func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenant string, wr *
378386
return
379387
}
380388

381-
p.fillRequestHeaders(clientIP, reqID, tenant, req)
389+
p.fillRequestHeaders(clientIP, reqID, tenantPrefix, tenant, req)
382390

383391
if p.auth.egressHeader != nil {
384392
req.Header.SetBytesV("Authorization", p.auth.egressHeader)
@@ -402,14 +410,14 @@ func (p *processor) send(clientIP net.Addr, reqID uuid.UUID, tenant string, wr *
402410
}
403411

404412
func (p *processor) fillRequestHeaders(
405-
clientIP net.Addr, reqID uuid.UUID, tenant string, req *fh.Request) {
413+
clientIP net.Addr, reqID uuid.UUID, tenantPrefix string, tenant string, req *fh.Request) {
406414
req.Header.Set("Content-Encoding", "snappy")
407415
req.Header.Set("Content-Type", "application/x-protobuf")
408416
req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
409417
req.Header.Set("X-Cortex-Tenant-Client", clientIP.String())
410418
req.Header.Set("X-Cortex-Tenant-ReqID", reqID.String())
411-
if p.cfg.Tenant.Prefix != "" {
412-
tenant = p.cfg.Tenant.Prefix + tenant
419+
if tenantPrefix != "" {
420+
tenant = tenantPrefix + tenant
413421
}
414422
req.Header.Set(p.cfg.Tenant.Header, tenant)
415423
}

processor_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func Test_request_headers(t *testing.T) {
226226
req := fh.AcquireRequest()
227227
clientIP, _ := net.ResolveIPAddr("ip", "1.1.1.1")
228228
reqID, _ := uuid.NewRandom()
229-
p.fillRequestHeaders(clientIP, reqID, "my-tenant", req)
229+
p.fillRequestHeaders(clientIP, reqID, "", "my-tenant", req)
230230

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

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

0 commit comments

Comments
 (0)