Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable matching on multiple labels #61

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ tenant:
# env: CT_TENANT_LABEL
label: tenant

# List of labels examined for tenant information. If set takes precedent over `label`
label_list:
- tenant
- other_tenant

# Whether to remove the tenant label from the request
# env: CT_TENANT_LABEL_REMOVE
label_remove: true
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"`
LabelList []string `yaml:"label_list"`
matthewjstanford marked this conversation as resolved.
Show resolved Hide resolved
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"`
}

pipeIn *fhu.InmemoryListener
Expand Down
3 changes: 3 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ log_response_errors: true

tenant:
label: tenant
label_list:
- tenat
matthewjstanford marked this conversation as resolved.
Show resolved Hide resolved
- other_tenant
prefix: ""
label_remove: true
header: X-Scope-OrgID
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
4 changes: 4 additions & 0 deletions deploy/k8s/chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ data:
{{- end }}
tenant:
label: {{ .Values.config.tenant.label }}
{{- with .Values.config.tenant.label_list }}
label_list:
{{- . | toYaml | nindent 8 }}
{{- end }}
prefix: {{ .Values.config.tenant.prefix }}
label_remove: {{ .Values.config.tenant.label_remove }}
header: {{ .Values.config.tenant.header }}
Expand Down
10 changes: 10 additions & 0 deletions deploy/k8s/chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@
"description": "Which label to look for the tenant information",
"default": "tenant"
},
"label_list": {
"type": "array",
"title": "LabelList",
"description": "List of labels examined for tenant information. If set takes precedent over `label`",
"items": {
"type": "string",
"title": "LabelListItems",
"default": ""
}
},
"prefix": {
"type": "string",
"title": "Prefix",
Expand Down
2 changes: 2 additions & 0 deletions deploy/k8s/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ config:
# -- Which label to look for the tenant information
# (env: `CT_TENANT_LABEL`)
label: tenant
# -- List of labels examined for tenant information. If set takes precedent over `label`
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/
Expand Down
18 changes: 14 additions & 4 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -331,16 +332,25 @@ func removeOrdered(slice []prompb.Label, s int) []prompb.Label {

func (p *processor) processTimeseries(ts *prompb.TimeSeries) (tenant string, err error) {
idx := 0

configuredLabels := p.cfg.Tenant.LabelList

if len(configuredLabels) == 0 {
matthewjstanford marked this conversation as resolved.
Show resolved Hide resolved
configuredLabels = append(configuredLabels, p.cfg.Tenant.Label)
}

for i, l := range ts.Labels {
if l.Name == p.cfg.Tenant.Label {
tenant, idx = l.Value, i
break
for _, configuredLabel := range configuredLabels {
blind-oracle marked this conversation as resolved.
Show resolved Hide resolved
if l.Name == configuredLabel {
tenant, idx = l.Value, i
break
}
}
}

if tenant == "" {
if p.cfg.Tenant.Default == "" {
return "", fmt.Errorf("label '%s' not found", p.cfg.Tenant.Label)
return "", fmt.Errorf("label(s): {'%s'} not found", strings.Join(configuredLabels, "','"))
}

return p.cfg.Tenant.Default, nil
Expand Down
7 changes: 7 additions & 0 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ max_conns_per_host: 64
tenant:
label_remove: false
default: default
label_list:
- "__tenant__"
- "__foo__"
- "__bar__"
`
testConfigWithValues = `listen: 0.0.0.0:8080
listen_pprof: 0.0.0.0:7008
Expand All @@ -45,6 +49,9 @@ tenant:
prefix: foobar-
label_remove: false
default: default
label_list:
- "__foo__"
- "__bar__"
`
)

Expand Down
Loading