Skip to content

Added name validation scheme as a config field and flag as well #6733

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just remove this file and its parent folder

// 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value should be legacy instead of strict.

Let's call the flag name.validation_scheme.
For flag description, let's make it easier for users to understand. How about Validation scheme for metric and label names. Set to utf8 to allow UTF-8 characters.


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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to also handle the case where c.NameValidationScheme is empty and we should use the legacy scheme

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