mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/promscrape: add -promscrape.config.strictParse
flag for detecting errors in -promscrape.config
file
This commit is contained in:
parent
7fbfef2aee
commit
4017163393
1 changed files with 17 additions and 1 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue