Skip to content

Commit

Permalink
Merge pull request #62 from matthewjstanford/prefix_prefer_source
Browse files Browse the repository at this point in the history
feat: adding prefix_prefer_source option
  • Loading branch information
blind-oracle authored Jan 25, 2024
2 parents 763b622 + 002fca9 commit 4867ff5
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 31 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ 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.
# 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_PREFER_SOURCE
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.4
1.12.5
15 changes: 8 additions & 7 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"`
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"`
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"`
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 @@ -25,6 +25,7 @@ tenant:
- tenant
- other_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.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
1 change: 1 addition & 0 deletions deploy/k8s/chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ data:
{{- . | toYaml | nindent 8 }}
{{- end }}
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 @@ -275,6 +275,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
8 changes: 8 additions & 0 deletions deploy/k8s/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ 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:
# 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.
# 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
# (env: `CT_TENANT_LABEL_REMOVE`)
label_remove: false
Expand Down
7 changes: 7 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,13 @@ 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.
# 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
label_remove: false
# To which header to add the tenant ID
Expand Down
20 changes: 13 additions & 7 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,23 @@ func (p *processor) handle(ctx *fh.RequestCtx) {
return
}

tenantPrefix := p.cfg.Tenant.Prefix

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

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 @@ -202,7 +211,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 @@ -304,7 +313,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 @@ -317,7 +326,7 @@ func (p *processor) dispatch(clientIP net.Addr, reqID uuid.UUID, m map[string]*p

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

i++
}
Expand Down Expand Up @@ -408,9 +417,6 @@ func (p *processor) fillRequestHeaders(
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
}
req.Header.Set(p.cfg.Tenant.Header, tenant)
}

Expand Down
14 changes: 0 additions & 14 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,6 @@ func Test_request_headers(t *testing.T) {
assert.Equal(t, "my-tenant", string(req.Header.Peek("X-Scope-OrgID")))
}

func Test_request_headers_with_prefix(t *testing.T) {
cfg, err := getConfig(testConfigWithValues)
assert.Nil(t, err)

p := newProcessor(*cfg)

req := fh.AcquireRequest()
clientIP, _ := net.ResolveIPAddr("ip", "1.1.1.1")
reqID, _ := uuid.NewRandom()
p.fillRequestHeaders(clientIP, reqID, "my-tenant", req)

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

func Test_handle(t *testing.T) {
cfg, err := getConfig(testConfig)
assert.Nil(t, err)
Expand Down

0 comments on commit 4867ff5

Please sign in to comment.