Skip to content

Commit

Permalink
fix(utf8): fix config logic for name validation
Browse files Browse the repository at this point in the history
We should only overwrite the ScrapeConfig if it is empty.

Added tests

part of prometheus#13095

Signed-off-by: Owen Williams <owen.williams@grafana.com>
  • Loading branch information
ywwg committed Aug 23, 2024
1 parent 1b86d54 commit 436a439
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
50 changes: 50 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -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 changes: 2 additions & 0 deletions config/testdata/scrape_config_default_validation_mode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scrape_configs:
- job_name: prometheus
4 changes: 4 additions & 0 deletions config/testdata/scrape_config_global_validation_mode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global:
metric_name_validation_scheme: utf8
scrape_configs:
- job_name: prometheus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global:
metric_name_validation_scheme: utf8
scrape_configs:
- job_name: prometheus
metric_name_validation_scheme: legacy
3 changes: 3 additions & 0 deletions config/testdata/scrape_config_local_validation_mode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scrape_configs:
- job_name: prometheus
metric_name_validation_scheme: utf8

0 comments on commit 436a439

Please sign in to comment.