diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index e8d2d4d6fa..54969b1601 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -1,6 +1,7 @@ package promscrape import ( + "flag" "fmt" "io/ioutil" "net/url" @@ -15,6 +16,11 @@ import ( "gopkg.in/yaml.v2" ) +var ( + strictParse = flag.Bool("promscrape.config.strictParse", false, "Whether to allow only supported fields in '-promscrape.config'. "+ + "This option may be used for errors detection in '-promscrape.config' file") +) + // Config represents essential parts from Prometheus config defined at https://prometheus.io/docs/prometheus/latest/configuration/configuration/ type Config struct { Global GlobalConfig `yaml:"global"` @@ -101,7 +107,7 @@ func loadConfig(path string) (cfg *Config, err error) { } func (cfg *Config) parse(data []byte, path string) error { - if err := yaml.Unmarshal(data, cfg); err != nil { + if err := unmarshalMaybeStrict(data, cfg); err != nil { return fmt.Errorf("cannot unmarshal data: %s", err) } absPath, err := filepath.Abs(path) @@ -120,6 +126,16 @@ func (cfg *Config) parse(data []byte, path string) error { return nil } +func unmarshalMaybeStrict(data []byte, dst interface{}) error { + var err error + if *strictParse { + err = yaml.UnmarshalStrict(data, dst) + } else { + err = yaml.Unmarshal(data, dst) + } + return err +} + func (cfg *Config) fileSDConfigsCount() int { n := 0 for i := range cfg.ScrapeConfigs {