From 70b0a346168e9aef79b909b8ecd5e02777a4a17c Mon Sep 17 00:00:00 2001 From: Harkishen Singh Date: Sun, 21 Jun 2020 21:26:59 +0530 Subject: [PATCH] Exit early on invalid config file (#7399) * Reload config file at start Signed-off-by: Harkishen-Singh * relocated config checking Signed-off-by: Harkishen-Singh * change log lever Signed-off-by: Harkishen-Singh * add helpful comment Signed-off-by: Harkishen-Singh --- cmd/prometheus/main.go | 6 ++++++ cmd/prometheus/main_test.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index c6066b72f..d7342499f 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -278,6 +278,12 @@ func main() { os.Exit(2) } + // Throw error for invalid config before starting other components. + if _, err := config.LoadFile(cfg.configFile); err != nil { + level.Error(logger).Log("msg", fmt.Sprintf("Error loading config (--config.file=%s)", cfg.configFile), "err", err) + os.Exit(2) + } + cfg.web.ReadTimeout = time.Duration(cfg.webTimeout) // Default -web.route-prefix to path of -web.external-url. if cfg.web.RoutePrefix == "" { diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index 3a56d0c14..f556437b2 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -111,7 +111,7 @@ func TestFailedStartupExitCode(t *testing.T) { } fakeInputFile := "fake-input-file" - expectedExitStatus := 1 + expectedExitStatus := 2 prom := exec.Command(promPath, "-test.main", "--config.file="+fakeInputFile) err := prom.Run()