From 2814b1490fcb67ea6334c11d4486e01503ca1561 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 13 Apr 2020 13:15:30 +0300 Subject: [PATCH] lib/promscrape: add `-promscrape.config.strictParse` flag for detecting errors in `-promscrape.config` file --- lib/promscrape/config.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index e8d2d4d6f..54969b160 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 {