8000 scrape: set validation and escaping defaults in default config vars by ywwg · Pull Request #16751 · prometheus/prometheus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

scrape: set validation and escaping defaults in default config vars #16751

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

Merged
merged 1 commit into from
Jun 25, 2025

Conversation

ywwg
Copy link
Member
@ywwg ywwg commented Jun 18, 2025

Fixes #16750

Updated tests to rely on the existence of those values

@ywwg ywwg marked this pull request as ready for review June 18, 2025 16:51
@ywwg ywwg requested review from dashpole and krajorama June 18, 2025 16:51
@roidelapluie
Copy link
Member
roidelapluie commented Jun 19, 2025

What about the following approach?
(note: same should be done for Escaping Scheme)

diff --git a/config/config.go b/config/config.go
index a15bcfcf7..87568e876 100644
--- a/config/config.go
+++ b/config/config.go
@@ -172,9 +172,8 @@ var (
 		ScrapeProtocols:                DefaultScrapeProtocols,
 		ConvertClassicHistogramsToNHCB: false,
 		AlwaysScrapeClassicHistograms:  false,
-		// Matching the behavior in Validate(), default to UTF-8.
-		MetricNameValidationScheme: UTF8ValidationConfig,
-		MetricNameEscapingScheme:   model.AllowUTF8,
+		MetricNameValidationScheme:     UTF8ValidationConfig,
+		MetricNameEscapingScheme:       model.AllowUTF8,
 	}
 
 	DefaultRuntimeConfig = RuntimeConfig{
@@ -191,9 +190,6 @@ var (
 		HonorTimestamps:   true,
 		HTTPClientConfig:  config.DefaultHTTPClientConfig,
 		EnableCompression: true,
-		// Matching the behavior in Validate(), default to UTF-8.
-		MetricNameValidationScheme: UTF8ValidationConfig,
-		MetricNameEscapingScheme:   model.AllowUTF8,
 	}
 
 	// DefaultAlertmanagerConfig is the default alertmanager configuration.
@@ -784,10 +780,6 @@ func (c *ScrapeConfig) SetDirectory(dir string) {
 // UnmarshalYAML implements the yaml.Unmarshaler interface.
 func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
 	*c = DefaultScrapeConfig
-	// Unset default validation and escaping scheme values so they can inherit
-	// from the global config if they are blank in the file.
-	c.MetricNameValidationScheme = ""
-	c.MetricNameEscapingScheme = ""
 	if err := discovery.UnmarshalYAMLWithInlineConfigs(c, unmarshal); err != nil {
 		return err
 	}
@@ -950,6 +942,13 @@ func (c *ScrapeConfig) MarshalYAML() (interface{}, error) {
 // ToValidationScheme returns the validation scheme for the given string config value.
 func ToValidationScheme(s string) (validationScheme model.ValidationScheme, err error) {
 	switch s {
+	case "":
+		// This is a workaround for third party exporters that don't set the validation scheme.
+		if DefaultGlobalConfig.MetricNameValidationScheme == "" {
+			// Prevents infinite recursion.
+			panic("global metric name validation scheme is not set")
+		}
+		return ToValidationScheme(DefaultGlobalConfig.MetricNameValidationScheme)
 	case UTF8ValidationConfig:
 		validationScheme = model.UTF8Validation
 	case LegacyValidationConfig:

@ywwg
Copy link
Member Author
ywwg commented Jun 20, 2025

yeah I think that can work -- I'll try it

@ywwg
Copy link
Member Author
ywwg commented Jun 20, 2025

looking good, and actually the wrapper for fixing escaping scheme works very nicely.

Where is the right place to add a comment that people need to call Validate?

@ywwg ywwg requested a review from roidelapluie June 20, 2025 13:35
@dashpole
Copy link
Contributor

Maybe expand this comment to say that defaulting is done during validate?

// ScrapeTimeout, ScrapeInterval, ScrapeProtocols, AlwaysScrapeClassicHistograms, and ConvertClassicHistogramsToNHCB default to the configured globals.

Or add it to the type comment above DefaultScrapeConfig.

@roidelapluie
Copy link
Member

I think the best is probably in the type comment above DefaultScrapeConfig ; as Validate does more than just this.

@ywwg
Copy link
Member Author
ywwg commented Jun 25, 2025

I think the best is probably in the type comment above DefaultScrapeConfig ; as Validate does more than just this.

I'm tempted to unexport DefaultScrapeConfig (after a deprecation window) and instead create NewDefaultScrapeConfig() that creates a valid object with those defaults. We can do that in a subsequent PR though.

@ywwg ywwg force-pushed the owilliams/defaultconfig branch 2 times, most recently from 7ac65dd to 4033c3c Compare June 25, 2025 14:23
@ywwg ywwg requested a review from dashpole June 25, 2025 14:30
@ywwg ywwg force-pushed the owilliams/defaultconfig branch from 4033c3c to 76f5cea Compare June 25, 2025 14:31
Fixes #16750

Signed-off-by: Owen Williams <owen.williams@grafana.com>
@ywwg ywwg force-pushed the owilliams/defaultconfig branch from 76f5cea to 1ce7d48 Compare June 25, 2025 14:37
@ywwg ywwg merged commit aa1d46a into main Jun 25, 2025
45 checks passed
@ywwg ywwg deleted the owilliams/defaultconfig branch June 25, 2025 15:14
Copy link
Member
@bboreham bboreham 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 state whether this change needs to go into Prometheus release 3.5 please?
The description reads as only relevant to users of the code, but I couldn't easily figure out what the code changes do.

}

DefaultRuntimeConfig = RuntimeConfig{
// Go runtime tuning.
GoGC: getGoGC(),
}

// DefaultScrapeConfig is the default scrape configuration.
// DefaultScrapeConfig is the default scrape configuration. Users of this
// default MUST call Validate() on the config after creation, even if it's
Copy link
Member

Choose a reason for hiding this comment

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

This is unclear to me. What is "the config"? I can't find a call to Validate() on this object in Prometheus.
Maybe you meant something like "MUST call Validate() on any ScrapeConfig object, even a copy of DefaultScrapeConfig" ?

@ywwg
Copy link
Member Author
ywwg commented Jun 25, 2025

Can you state whether this change needs to go into Prometheus release 3.5 please? The description reads as only relevant to users of the code, but I couldn't easily figure out what the code changes do.

the most important thing is the linked bug which in turn links to an otel bug that happened because of this: open-telemetry/opentelemetry-collector-contrib#40788

Because the required fields were not set, they got a panic when the code executed on the invalid config object.

@ywwg
Copy link
Member Author
ywwg commented Jun 25, 2025

So yes, this should be part of 3.5

@bboreham
Copy link
Member

I don't understand how a bug in a different project means we need this change in Prometheus 3.5.
Considering just the Prometheus binary, what would end-users experience differently?

@ywwg
Copy link
Member Author
ywwg commented Jun 26, 2025

the effect of the bug was in another project, but the cause was a prometheus regression preventing a user from upgrading their collector. I recommend reading the upstream bug for the full context.

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

Successfully merging this pull request may close these issues.

DefaultGlobalConfig does not set required fields for Validation and Escaping
4 participants
0