From f9847352b4b5fe6e810559e63681c680454a5367 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 21 May 2020 15:22:01 +0300 Subject: [PATCH] app/vmagent: add `-dryRun` option for checking all the configs mentioned in command-line flags without running `vmagent` Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/362 --- app/vmagent/main.go | 15 +++++++++++++++ app/vmagent/remotewrite/remotewrite.go | 15 +++++++++++++++ lib/promscrape/scraper.go | 9 +++++++++ 3 files changed, 39 insertions(+) diff --git a/app/vmagent/main.go b/app/vmagent/main.go index 1c27f813a..00950e051 100644 --- a/app/vmagent/main.go +++ b/app/vmagent/main.go @@ -40,6 +40,8 @@ var ( "Telnet put messages and HTTP /api/put messages are simultaneously served on TCP port. "+ "Usually :4242 must be set. Doesn't work if empty") opentsdbHTTPListenAddr = flag.String("opentsdbHTTPListenAddr", "", "TCP address to listen for OpentTSDB HTTP put requests. Usually :4242 must be set. Doesn't work if empty") + dryRun = flag.Bool("dryRun", false, "Whether to check only config files without running vmagent. The following files are checked: "+ + "-promscrape.config, -remoteWrite.relabelConfig, -remoteWrite.urlRelabelConfig . See also -promscrape.config.dryRun") ) var ( @@ -55,6 +57,19 @@ func main() { envflag.Parse() buildinfo.Init() logger.Init() + + if *dryRun { + flag.Set("promscrape.config.strictParse", "true") + if err := remotewrite.CheckRelabelConfigs(); err != nil { + logger.Fatalf("error when checking relabel configs: %s", err) + } + if err := promscrape.CheckConfig(); err != nil { + logger.Fatalf("error when checking Prometheus config: %s", err) + } + logger.Infof("all the configs are ok; exitting with 0 status code") + return + } + logger.Infof("starting vmagent at %q...", *httpListenAddr) startTime := time.Now() remotewrite.Init() diff --git a/app/vmagent/remotewrite/remotewrite.go b/app/vmagent/remotewrite/remotewrite.go index c6390cc81..9f747a7a1 100644 --- a/app/vmagent/remotewrite/remotewrite.go +++ b/app/vmagent/remotewrite/remotewrite.go @@ -32,6 +32,21 @@ var ( "Disk usage is unlimited if the value is set to 0") ) +// CheckRelabelConfigs checks -remoteWrite.relabelConfig and -remoteWrite.urlRelabelConfig. +func CheckRelabelConfigs() error { + if *relabelConfigPathGlobal != "" { + if _, err := promrelabel.LoadRelabelConfigs(*relabelConfigPathGlobal); err != nil { + return fmt.Errorf("cannot load -remoteWrite.relabelConfig=%q: %s", *relabelConfigPathGlobal, err) + } + } + for _, path := range *relabelConfigPaths { + if _, err := promrelabel.LoadRelabelConfigs(path); err != nil { + return fmt.Errorf("cannot load relabel configs from -remoteWrite.urlRelabelConfig=%q: %s", path, err) + } + } + return nil +} + var rwctxs []*remoteWriteCtx // Init initializes remotewrite. diff --git a/lib/promscrape/scraper.go b/lib/promscrape/scraper.go index 54751283a..900313694 100644 --- a/lib/promscrape/scraper.go +++ b/lib/promscrape/scraper.go @@ -37,6 +37,15 @@ var ( "See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config for details") ) +// CheckConfig checks -promscrape.config for errors and unsupported options. +func CheckConfig() error { + if *promscrapeConfigFile == "" { + return fmt.Errorf("missing -promscrape.config option") + } + _, _, err := loadConfig(*promscrapeConfigFile) + return err +} + // Init initializes Prometheus scraper with config from the `-promscrape.config`. // // Scraped data is passed to pushData.