Skip to content

Commit 2186312

Browse files
committed
Added name validation scheme as a config field and flag as well
1 parent 7b2728d commit 2186312

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

docs/configuration/config-file-reference.md

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ Where default_value is the value to use if the environment variable is undefined
6868
# CLI flag: -http.prefix
6969
[http_prefix: <string> | default = "/api/prom"]
7070

71+
# NameValidationScheme for prometheus
72+
# Set to legacy as default
73+
# CLI flag: -name.validation.scheme
74+
[name_validation_scheme: <string> | default = "legacy"]
75+
7176
# Comma-separated list of resources to monitor. Supported values are cpu and
7277
# heap, which tracks metrics from github.com/prometheus/procfs and
7378
# runtime/metrics that are close estimates. Empty string to disable.

pkg/cortex/configinit/init.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package configinit
22

3-
import "github.com/prometheus/common/model"
3+
// import "github.com/prometheus/common/model"
44

5-
func init() {
6-
model.NameValidationScheme = model.LegacyValidation
7-
}
5+
// func init() {
6+
// model.NameValidationScheme = model.LegacyValidation
7+
// }

pkg/cortex/cortex.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/go-kit/log/level"
1515
"github.com/pkg/errors"
1616
"github.com/prometheus/client_golang/prometheus"
17+
"github.com/prometheus/common/model"
1718
"github.com/prometheus/prometheus/promql"
1819
prom_storage "github.com/prometheus/prometheus/storage"
1920
"github.com/weaveworks/common/server"
@@ -90,11 +91,12 @@ var (
9091

9192
// Config is the root config for Cortex.
9293
type Config struct {
93-
Target flagext.StringSliceCSV `yaml:"target"`
94-
AuthEnabled bool `yaml:"auth_enabled"`
95-
PrintConfig bool `yaml:"-"`
96-
HTTPPrefix string `yaml:"http_prefix"`
97-
MonitoredResources flagext.StringSliceCSV `yaml:"monitored_resources"`
94+
Target flagext.StringSliceCSV `yaml:"target"`
95+
AuthEnabled bool `yaml:"auth_enabled"`
96+
PrintConfig bool `yaml:"-"`
97+
HTTPPrefix string `yaml:"http_prefix"`
98+
NameValidationScheme string `yaml:"name_validation_scheme"`
99+
MonitoredResources flagext.StringSliceCSV `yaml:"monitored_resources"`
98100

99101
ExternalQueryable prom_storage.Queryable `yaml:"-"`
100102
ExternalPusher ruler.Pusher `yaml:"-"`
@@ -146,6 +148,7 @@ func (c *Config) RegisterFlags(f *flag.FlagSet) {
146148
f.BoolVar(&c.AuthEnabled, "auth.enabled", true, "Set to false to disable auth.")
147149
f.BoolVar(&c.PrintConfig, "print.config", false, "Print the config and exit.")
148150
f.StringVar(&c.HTTPPrefix, "http.prefix", "/api/prom", "HTTP path prefix for Cortex API.")
151+
f.StringVar(&c.NameValidationScheme, "name.validation.scheme", "strict", "Used to set name validation scheme in prometheus common. legacy by default")
149152

150153
c.MonitoredResources = []string{}
151154
f.Var(&c.MonitoredResources, "monitored.resources", "Comma-separated list of resources to monitor. "+
@@ -193,6 +196,10 @@ func (c *Config) Validate(log log.Logger) error {
193196
return errInvalidHTTPPrefix
194197
}
195198

199+
if c.NameValidationScheme != "" && c.NameValidationScheme != "legacy" && c.NameValidationScheme != "utf-8" {
200+
return fmt.Errorf("invalid name validation scheme")
201+
}
202+
196203
if err := c.API.Validate(); err != nil {
197204
return errors.Wrap(err, "invalid api config")
198205
}
@@ -361,6 +368,13 @@ func New(cfg Config) (*Cortex, error) {
361368
os.Exit(0)
362369
}
363370

371+
// Sets the NameValidationScheme in prometheus/common
372+
if cfg.NameValidationScheme != "legacy" {
373+
model.NameValidationScheme = model.LegacyValidation
374+
} else {
375+
model.NameValidationScheme = model.UTF8Validation
376+
}
377+
364378
// Swap out the default resolver to support multiple tenant IDs separated by a '|'
365379
if cfg.TenantFederation.Enabled {
366380
util_log.WarnExperimentalUse("tenant-federation")

0 commit comments

Comments
 (0)