Skip to content

Commit 30ac251

Browse files
committed
feat: support configurable name validation (legacy or UTF-8 schemes)
Signed-off-by: 7h3-3mp7y-m4n <emailtorash@gmail.com>
1 parent 186c988 commit 30ac251

File tree

9 files changed

+66
-22
lines changed

9 files changed

+66
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [FEATURE] Compactor: Add support for percentage based sharding for compactors. #6738
2121
* [FEATURE] Querier: Allow choosing PromQL engine via header. #6777
2222
* [FEATURE] Querier: Support for configuring query optimizers and enabling XFunctions in the Thanos engine. #6873
23+
*[FEATURE] Config: Name validation scheme for metric and label names can be set using the config file (`name_validation_scheme`) as well as a CLI flag (`-name.validation-scheme`)
2324
* [ENHANCEMENT] Tenant Federation: Add a # of query result limit logic when the `-tenant-federation.regex-matcher-enabled` is enabled. #6845
2425
* [ENHANCEMENT] Query Frontend: Add a `cortex_slow_queries_total` metric to track # of slow queries per user. #6859
2526
* [ENHANCEMENT] Query Frontend: Change to return 400 when the tenant resolving fail. #6715

docs/configuration/config-file-reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ 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+
# Set to "legacy" to enforce strict legacy-compatible name rules.
72+
# CLI flag: -name.validation-scheme
73+
[name_validation_scheme: <legacy | utf8> | default = "legacy"]
74+
7175
resource_monitor:
7276
# Comma-separated list of resources to monitor. Supported values are cpu and
7377
# heap, which tracks metrics from github.com/prometheus/procfs and

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ require (
4545
github.com/prometheus/client_model v0.6.2
4646
github.com/prometheus/common v0.63.0
4747
// Prometheus maps version 2.x.y to tags v0.x.y.
48-
github.com/prometheus/prometheus v0.303.1
48+
github.com/prometheus/prometheus v1.99.0
4949
github.com/segmentio/fasthash v1.0.3
5050
github.com/sony/gobreaker v1.0.0
5151
github.com/spf13/afero v1.11.0

pkg/cortex/configinit/init.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

pkg/cortex/cortex.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ 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"
18+
prom_config "github.com/prometheus/prometheus/config"
1719
"github.com/prometheus/prometheus/promql"
1820
prom_storage "github.com/prometheus/prometheus/storage"
1921
"github.com/weaveworks/common/server"
@@ -31,7 +33,6 @@ import (
3133
"github.com/cortexproject/cortex/pkg/configs"
3234
configAPI "github.com/cortexproject/cortex/pkg/configs/api"
3335
"github.com/cortexproject/cortex/pkg/configs/db"
34-
_ "github.com/cortexproject/cortex/pkg/cortex/configinit"
3536
"github.com/cortexproject/cortex/pkg/cortex/storage"
3637
"github.com/cortexproject/cortex/pkg/cortexpb"
3738
"github.com/cortexproject/cortex/pkg/distributor"
@@ -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-
ResourceMonitor configs.ResourceMonitor `yaml:"resource_monitor"`
94+
Target flagext.StringSliceCSV `yaml:"target"`
95+
AuthEnabled bool `yaml:"auth_enabled"`
96+
PrintConfig bool `yaml:"-"`
97+
HTTPPrefix string `yaml:"http_prefix"`
98+
ResourceMonitor configs.ResourceMonitor `yaml:"resource_monitor"`
99+
NameValidationScheme string `yaml:"name_validation_scheme"`
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", "legacy", "Validation scheme for metric and label names. Set to utf8 to allow UTF-8 characters.")
149152

150153
c.API.RegisterFlags(f)
151154
c.registerServerFlagsWithChangedDefaultValues(f)
@@ -181,6 +184,11 @@ func (c *Config) RegisterFlags(f *flag.FlagSet) {
181184
// Validate the cortex config and returns an error if the validation
182185
// doesn't pass
183186
func (c *Config) Validate(log log.Logger) error {
187+
switch c.NameValidationScheme {
188+
case "", prom_config.LegacyValidationConfig, prom_config.UTF8ValidationConfig:
189+
default:
190+
return fmt.Errorf("invalid name validation scheme: %s", c.NameValidationScheme)
191+
}
184192
if err := c.validateYAMLEmptyNodes(); err != nil {
185193
return err
186194
}
@@ -349,7 +357,12 @@ func New(cfg Config) (*Cortex, error) {
349357
}
350358
os.Exit(0)
351359
}
352-
360+
//nolint:staticcheck // SA1019: using deprecated NameValidationScheme intentionally as a temporary compatibility workaround for UTF-8 migration issues
361+
if cfg.NameValidationScheme == prom_config.UTF8ValidationConfig {
362+
model.NameValidationScheme = model.UTF8Validation
363+
} else {
364+
model.NameValidationScheme = model.LegacyValidation
365+
}
353366
// Swap out the default resolver to support multiple tenant IDs separated by a '|'
354367
if cfg.TenantFederation.Enabled {
355368
util_log.WarnExperimentalUse("tenant-federation")

pkg/cortex/cortex_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/prometheus/client_golang/prometheus"
17+
prom_config "github.com/prometheus/prometheus/config"
1718
"github.com/stretchr/testify/assert"
1819
"github.com/stretchr/testify/require"
1920
"github.com/weaveworks/common/server"
@@ -217,6 +218,42 @@ func TestConfigValidation(t *testing.T) {
217218
},
218219
expectedError: nil,
219220
},
221+
{
222+
name: "should not fail validation for empty name validation scheme (use legacy by default)",
223+
getTestConfig: func() *Config {
224+
configuration := newDefaultConfig()
225+
configuration.NameValidationScheme = ""
226+
return configuration
227+
},
228+
expectedError: nil,
229+
},
230+
{
231+
name: "should not fail validation for legacy name validation scheme",
232+
getTestConfig: func() *Config {
233+
configuration := newDefaultConfig()
234+
configuration.NameValidationScheme = prom_config.LegacyValidationConfig
235+
return configuration
236+
},
237+
expectedError: nil,
238+
},
239+
{
240+
name: "should not fail validation for utf-8 name validation scheme",
241+
getTestConfig: func() *Config {
242+
configuration := newDefaultConfig()
243+
configuration.NameValidationScheme = prom_config.UTF8ValidationConfig
244+
return configuration
245+
},
246+
expectedError: nil,
247+
},
248+
{
249+
name: "should fail validation for invalid name validation scheme",
250+
getTestConfig: func() *Config {
251+
configuration := newDefaultConfig()
252+
configuration.NameValidationScheme = "invalid"
253+
return configuration
254+
},
255+
expectedError: fmt.Errorf("invalid name validation scheme"),
256+
},
220257
} {
221258
t.Run(tc.name, func(t *testing.T) {
222259
err := tc.getTestConfig().Validate(nil)

pkg/distributor/distributor_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"google.golang.org/grpc/status"
3636

3737
promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
38-
_ "github.com/cortexproject/cortex/pkg/cortex/configinit"
3938
"github.com/cortexproject/cortex/pkg/cortexpb"
4039
"github.com/cortexproject/cortex/pkg/ha"
4140
"github.com/cortexproject/cortex/pkg/ingester"

pkg/util/validation/validate_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"strings"
66
"testing"
77

8+
"github.com/cortexproject/cortex/pkg/cortexpb"
9+
util_log "github.com/cortexproject/cortex/pkg/util/log"
810
"github.com/go-kit/log"
911
"github.com/prometheus/client_golang/prometheus"
1012
"github.com/prometheus/client_golang/prometheus/testutil"
@@ -15,10 +17,6 @@ import (
1517
"github.com/stretchr/testify/assert"
1618
"github.com/stretchr/testify/require"
1719
"github.com/weaveworks/common/httpgrpc"
18-
19-
_ "github.com/cortexproject/cortex/pkg/cortex/configinit"
20-
"github.com/cortexproject/cortex/pkg/cortexpb"
21-
util_log "github.com/cortexproject/cortex/pkg/util/log"
2220
)
2321

2422
func TestValidateLabels(t *testing.T) {

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)