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 4 commits into
base: master
Choose a base branch
from

Conversation

av153k
Copy link

@av153k av153k commented May 8, 2025

What this PR does:

  • Adds name_validation_scheme as yaml flag and name.validation.scheme as a flag
  • Remove config init and set the model.NameValidationScheme directly in New() method to creating a Cortex object

Which issue(s) this PR fixes:
Fixes #6702

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@av153k av153k force-pushed the feat/namevalidationscheme-flag branch from 2186312 to 146188f Compare May 8, 2025 07:00
@av153k av153k marked this pull request as ready for review May 8, 2025 09:54
@av153k
Copy link
Author

av153k commented May 8, 2025

I am working on the tests for this.

@av153k
Copy link
Author

av153k commented May 10, 2025

Added test for name validation scheme in TestConfigValidation in cortex_test.go

Copy link
Contributor

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Thanks @av153k. Great work and I think it is on the right track. Just few comments

@@ -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

Copy link
Author

Choose a reason for hiding this comment

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

The empty case will be handled when we create a new context object as you can see later on in the file.

And in the validation, it does check for non-empty string to validate so I think that will pass as well. Let me know if I need to add something else as well.

Copy link
Author

Choose a reason for hiding this comment

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

I have made the changes and allowed for legacy to be used by default in method to create a new cortex object.

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 unify the code to make it cleaner. You can just use a single switch statement.

switch c.NameValidationScheme {
  case "", prom_config.LegacyValidationConfig:
    model.NameValidationScheme = model.LegacyValidation
  case prom_config.UTF8ValidationConfig:
    model.NameValidationScheme = model.UTF8Validation
  default:
    // return invalid error
}

@@ -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.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed this in the latest commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

You need to regenerate the doc after updating flag description

Copy link
Author

Choose a reason for hiding this comment

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

I made the changes to the doc config-file-reference.md but missed including it in the commit. I am assuming you mean that, or is there any command to generate the docs automatically?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you do make docs it can generate docs automatically

…andled empty name validation scheme and related tests

Signed-off-by: Abhishek Anand <[email protected]>
Copy link
Contributor

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Can you also update changelog?

name: "should not fail validation for utf-8 name validation scheme",
getTestConfig: func() *Config {
configuration := newDefaultConfig()
configuration.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.

Let's use const here

Copy link
Author

Choose a reason for hiding this comment

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

const as in from prometheus config, right?

name: "should not fail validation for legacy name validation scheme",
getTestConfig: func() *Config {
configuration := newDefaultConfig()
configuration.NameValidationScheme = "legacy"
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 use const here

expectedError: nil,
},
{
name: "should fail validation for invalid(anything other than legacy and utf-8) name validation scheme",
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just say should fail validation for invalid name validation scheme. No need to mention legacy and utf8

@av153k
Copy link
Author

av153k commented May 11, 2025

Can you also update changelog?

What would be the version of this change, though?

… in the config file and CLI flag, along with corresponding tests.

Signed-off-by: Abhishek Anand <[email protected]>
// Setting name validation scheme as legacy if provided with an empty string
if c.NameValidationScheme == "" {
c.NameValidationScheme = prom_config.LegacyValidationConfig
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove this code now as it will be handled in the switch statement

@@ -193,6 +201,10 @@ func (c *Config) Validate(log log.Logger) error {
return errInvalidHTTPPrefix
}

if c.NameValidationScheme != "" && c.NameValidationScheme != prom_config.LegacyValidationConfig && c.NameValidationScheme != prom_config.UTF8ValidationConfig {
return fmt.Errorf("invalid name validation scheme")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove this code now as it will be handled in the switch statement

@@ -39,6 +39,7 @@
* [BUGFIX] Querier: Fix panic when marshaling QueryResultRequest. #6601
* [BUGFIX] Ingester: Avoid resharding for query when restart readonly ingesters. #6642
* [BUGFIX] Query Frontend: Fix query frontend per `user` metrics clean up. #6698
* [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`)
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 move the changelog entry to L14. Features are put together

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make NameValidationScheme configurable
2 participants