Skip to content

Commit bbcbcae

Browse files
committed
feat: add configurable name validation scheme (legacy or utf8
Signed-off-by: 7h3-3mp7y-m4n <[email protected]>
1 parent cc48e77 commit bbcbcae

File tree

12 files changed

+115
-32
lines changed

12 files changed

+115
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [FEATURE] Ruler: Add support for percentage based sharding for rulers. #6680
1313
* [FEATURE] Ruler: Add support for group labels. #6665
1414
* [FEATURE] Query federation: Introduce a regex tenant resolver to allow regex in `X-Scope-OrgID` value. #6713
15+
[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`)
1516
- Add an experimental `tenant-federation.regex-matcher-enabled` flag. If it enabled, user can input regex to `X-Scope-OrgId`, the matched tenantIDs are automatically involved. The user discovery is based on scanning block storage, so new users can get queries after uploading a block (generally 2h).
1617
- Add an experimental `tenant-federation.user-sync-interval` flag, it specifies how frequently to scan users. The scanned users are used to calculate matched tenantIDs.
1718
* [FEATURE] Experimental Support Parquet format: Implement parquet converter service to convert a TSDB block into Parquet and Parquet Queryable. #6716 #6743

docs/configuration/config-file-reference.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ 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+
# Validation scheme for metric and label names.
72+
# Set to "utf8" to allow UTF-8 characters in metric and label names.
73+
# Set to "legacy" to enforce strict legacy-compatible name rules.
74+
# CLI flag: -name.validation_scheme
75+
[name_validation_scheme: <legacy | utf8> | default = "legacy"]
76+
7177
resource_monitor:
7278
# Comma-separated list of resources to monitor. Supported values are cpu and
7379
# heap, which tracks metrics from github.com/prometheus/procfs and

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ require (
4343
github.com/prometheus/alertmanager v0.28.1
4444
github.com/prometheus/client_golang v1.22.0
4545
github.com/prometheus/client_model v0.6.2
46-
github.com/prometheus/common v0.63.0
46+
github.com/prometheus/common v0.65.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

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
838838
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
839839
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
840840
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
841-
github.com/prometheus/common v0.63.0 h1:YR/EIY1o3mEFP/kZCD7iDMnLPlGyuU2Gb3HIcXnA98k=
842-
github.com/prometheus/common v0.63.0/go.mod h1:VVFF/fBIoToEnWRVkYoXEkq3R3paCoxG9PXP74SnV18=
841+
github.com/prometheus/common v0.65.0 h1:QDwzd+G1twt//Kwj/Ww6E9FQq1iVMmODnILtW1t2VzE=
842+
github.com/prometheus/common v0.65.0/go.mod h1:0gZns+BLRQ3V6NdaerOhMbwwRbNh9hkGINtQAsP5GS8=
843843
github.com/prometheus/exporter-toolkit v0.14.0 h1:NMlswfibpcZZ+H0sZBiTjrA3/aBFHkNZqE+iCj5EmRg=
844844
github.com/prometheus/exporter-toolkit v0.14.0/go.mod h1:Gu5LnVvt7Nr/oqTBUC23WILZepW0nffNo10XdhQcwWA=
845845
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=

pkg/cortex/cortex.go

Lines changed: 22 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"
@@ -90,11 +92,12 @@ var (
9092

9193
// Config is the root config for Cortex.
9294
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"`
95+
Target flagext.StringSliceCSV `yaml:"target"`
96+
AuthEnabled bool `yaml:"auth_enabled"`
97+
PrintConfig bool `yaml:"-"`
98+
HTTPPrefix string `yaml:"http_prefix"`
99+
ResourceMonitor configs.ResourceMonitor `yaml:"resource_monitor"`
100+
NameValidationScheme string `yaml:"name_validation_scheme"`
98101

99102
ExternalQueryable prom_storage.Queryable `yaml:"-"`
100103
ExternalPusher ruler.Pusher `yaml:"-"`
@@ -146,7 +149,7 @@ func (c *Config) RegisterFlags(f *flag.FlagSet) {
146149
f.BoolVar(&c.AuthEnabled, "auth.enabled", true, "Set to false to disable auth.")
147150
f.BoolVar(&c.PrintConfig, "print.config", false, "Print the config and exit.")
148151
f.StringVar(&c.HTTPPrefix, "http.prefix", "/api/prom", "HTTP path prefix for Cortex API.")
149-
152+
f.StringVar(&c.NameValidationScheme, "name.validation_scheme", "legacy", "Validation scheme for metric and label names. Set to utf8 to allow UTF-8 characters.")
150153
c.API.RegisterFlags(f)
151154
c.registerServerFlagsWithChangedDefaultValues(f)
152155
c.Distributor.RegisterFlags(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+
if c.NameValidationScheme != "" &&
188+
c.NameValidationScheme != prom_config.LegacyValidationConfig &&
189+
c.NameValidationScheme != prom_config.UTF8ValidationConfig {
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,14 @@ func New(cfg Config) (*Cortex, error) {
349357
}
350358
os.Exit(0)
351359
}
352-
360+
switch cfg.NameValidationScheme {
361+
case "", prom_config.LegacyValidationConfig:
362+
model.NameValidationScheme = model.LegacyValidation
363+
case prom_config.UTF8ValidationConfig:
364+
model.NameValidationScheme = model.UTF8Validation
365+
default:
366+
return nil, fmt.Errorf("invalid name validation scheme")
367+
}
353368
// Swap out the default resolver to support multiple tenant IDs separated by a '|'
354369
if cfg.TenantFederation.Enabled {
355370
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)

vendor/github.com/prometheus/common/config/http_config.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/prometheus/common/expfmt/text_parse.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/prometheus/common/model/labels.go

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

vendor/github.com/prometheus/common/model/time.go

Lines changed: 22 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)