Skip to content
5 changes: 5 additions & 0 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Where default_value is the value to use if the environment variable is undefined
# CLI flag: -http.prefix
[http_prefix: <string> | default = "/api/prom"]

# NameValidationScheme for prometheus
# Set to legacy as default
# CLI flag: -name.validation.scheme
[name_validation_scheme: <string> | default = "legacy"]

# Comma-separated list of resources to monitor. Supported values are cpu and
# heap, which tracks metrics from github.com/prometheus/procfs and
# runtime/metrics that are close estimates. Empty string to disable.
Expand Down
8 changes: 4 additions & 4 deletions pkg/cortex/configinit/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package configinit

import "github.com/prometheus/common/model"
// import "github.com/prometheus/common/model"

func init() {
model.NameValidationScheme = model.LegacyValidation
}
// func init() {
// model.NameValidationScheme = model.LegacyValidation
// }
24 changes: 19 additions & 5 deletions pkg/cortex/cortex.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql"
prom_storage "github.com/prometheus/prometheus/storage"
"github.com/weaveworks/common/server"
Expand Down Expand Up @@ -90,11 +91,12 @@ var (

// Config is the root config for Cortex.
type Config struct {
Target flagext.StringSliceCSV `yaml:"target"`
AuthEnabled bool `yaml:"auth_enabled"`
PrintConfig bool `yaml:"-"`
HTTPPrefix string `yaml:"http_prefix"`
MonitoredResources flagext.StringSliceCSV `yaml:"monitored_resources"`
Target flagext.StringSliceCSV `yaml:"target"`
AuthEnabled bool `yaml:"auth_enabled"`
PrintConfig bool `yaml:"-"`
HTTPPrefix string `yaml:"http_prefix"`
NameValidationScheme string `yaml:"name_validation_scheme"`
MonitoredResources flagext.StringSliceCSV `yaml:"monitored_resources"`

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

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

if c.NameValidationScheme != "" && c.NameValidationScheme != "legacy" && c.NameValidationScheme != "utf-8" {
return fmt.Errorf("invalid name validation scheme")
}

if err := c.API.Validate(); err != nil {
return errors.Wrap(err, "invalid api config")
}
Expand Down Expand Up @@ -361,6 +368,13 @@ func New(cfg Config) (*Cortex, error) {
os.Exit(0)
}

// Sets the NameValidationScheme in prometheus/common
if cfg.NameValidationScheme != "legacy" {
model.NameValidationScheme = model.LegacyValidation
} else {
model.NameValidationScheme = model.UTF8Validation
}

// Swap out the default resolver to support multiple tenant IDs separated by a '|'
if cfg.TenantFederation.Enabled {
util_log.WarnExperimentalUse("tenant-federation")
Expand Down
36 changes: 36 additions & 0 deletions pkg/cortex/cortex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,42 @@ func TestConfigValidation(t *testing.T) {
},
expectedError: nil,
},
// NameValidationScheme tests
{
name: "should not fail validation for empty name validation scheme",
getTestConfig: func() *Config {
configuration := newDefaultConfig()
return configuration
},
expectedError: nil,
},
{
name: "should not fail validation for legacy name validation scheme",
getTestConfig: func() *Config {
configuration := newDefaultConfig()
configuration.NameValidationScheme = "legacy"
return configuration
},
expectedError: nil,
},
{
name: "should not fail validation for utf-8 name validation scheme",
getTestConfig: func() *Config {
configuration := newDefaultConfig()
configuration.NameValidationScheme = "utf-8"
return configuration
},
expectedError: nil,
},
{
name: "should fail validation for invalid(anything other than legacy and utf-8) name validation scheme",
getTestConfig: func() *Config {
configuration := newDefaultConfig()
configuration.NameValidationScheme = "invalid"
return configuration
},
expectedError: fmt.Errorf("invalid name validation scheme"),
},
} {
t.Run(tc.name, func(t *testing.T) {
err := tc.getTestConfig().Validate(nil)
Expand Down
Loading