lib/promscrape: add -promscrape.config.strictParse flag for detecting errors in -promscrape.config file

This commit is contained in:
Aliaksandr Valialkin 2020-04-13 13:15:30 +03:00
parent 7fbfef2aee
commit 4017163393

View file

@ -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 {