mirror of
https://github.com/prometheus/prometheus
synced 2024-12-27 00:53:12 +00:00
fix(utf8): fix config logic for name validation
We should only overwrite the ScrapeConfig if it is empty. Added tests part of https://github.com/prometheus/prometheus/issues/13095 Signed-off-by: Owen Williams <owen.williams@grafana.com>
This commit is contained in:
parent
1b86d54c7f
commit
436a439ed2
@ -781,7 +781,9 @@ func (c *ScrapeConfig) Validate(globalConfig GlobalConfig) error {
|
||||
default:
|
||||
return fmt.Errorf("unknown name validation method specified, must be either 'legacy' or 'utf8', got %s", globalConfig.MetricNameValidationScheme)
|
||||
}
|
||||
c.MetricNameValidationScheme = globalConfig.MetricNameValidationScheme
|
||||
if c.MetricNameValidationScheme == "" {
|
||||
c.MetricNameValidationScheme = globalConfig.MetricNameValidationScheme
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ package config
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -2300,3 +2301,52 @@ func TestScrapeConfigDisableCompression(t *testing.T) {
|
||||
|
||||
require.False(t, got.ScrapeConfigs[0].EnableCompression)
|
||||
}
|
||||
|
||||
func TestScrapeConfigNameValidationSettings(t *testing.T) {
|
||||
model.NameValidationScheme = model.UTF8Validation
|
||||
defer func() {
|
||||
model.NameValidationScheme = model.LegacyValidation
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
inputFile string
|
||||
expectScheme string
|
||||
}{
|
||||
{
|
||||
name: "blank config implies default",
|
||||
inputFile: "scrape_config_default_validation_mode",
|
||||
expectScheme: "",
|
||||
},
|
||||
{
|
||||
name: "global setting implies local settings",
|
||||
inputFile: "scrape_config_global_validation_mode",
|
||||
expectScheme: "utf8",
|
||||
},
|
||||
{
|
||||
name: "local setting",
|
||||
inputFile: "scrape_config_local_validation_mode",
|
||||
expectScheme: "utf8",
|
||||
},
|
||||
{
|
||||
name: "local setting overrides global setting",
|
||||
inputFile: "scrape_config_local_global_validation_mode",
|
||||
expectScheme: "legacy",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
want, err := LoadFile(fmt.Sprintf("testdata/%s.yml", tc.inputFile), false, false, log.NewNopLogger())
|
||||
require.NoError(t, err)
|
||||
|
||||
out, err := yaml.Marshal(want)
|
||||
|
||||
require.NoError(t, err)
|
||||
got := &Config{}
|
||||
require.NoError(t, yaml.UnmarshalStrict(out, got))
|
||||
|
||||
require.Equal(t, tc.expectScheme, got.ScrapeConfigs[0].MetricNameValidationScheme)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
2
config/testdata/scrape_config_default_validation_mode.yml
vendored
Normal file
2
config/testdata/scrape_config_default_validation_mode.yml
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
scrape_configs:
|
||||
- job_name: prometheus
|
4
config/testdata/scrape_config_global_validation_mode.yml
vendored
Normal file
4
config/testdata/scrape_config_global_validation_mode.yml
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
global:
|
||||
metric_name_validation_scheme: utf8
|
||||
scrape_configs:
|
||||
- job_name: prometheus
|
5
config/testdata/scrape_config_local_global_validation_mode.yml
vendored
Normal file
5
config/testdata/scrape_config_local_global_validation_mode.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
global:
|
||||
metric_name_validation_scheme: utf8
|
||||
scrape_configs:
|
||||
- job_name: prometheus
|
||||
metric_name_validation_scheme: legacy
|
3
config/testdata/scrape_config_local_validation_mode.yml
vendored
Normal file
3
config/testdata/scrape_config_local_validation_mode.yml
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
scrape_configs:
|
||||
- job_name: prometheus
|
||||
metric_name_validation_scheme: utf8
|
Loading…
Reference in New Issue
Block a user