From 134a0c7d7843775ec1300ef1b0031320ff891770 Mon Sep 17 00:00:00 2001 From: bwplotka Date: Thu, 12 Dec 2024 10:57:27 +0000 Subject: [PATCH] Improved error. Signed-off-by: bwplotka --- config/config.go | 6 ++++-- config/config_test.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 5d49287899..83a4f5860d 100644 --- a/config/config.go +++ b/config/config.go @@ -309,11 +309,13 @@ func (c Config) String() string { // GetScrapeConfigs returns the read-only, validated scrape configurations including // the ones from the scrape_config_files. // This method does not write to config, and it's concurrency safe (the pointer receiver is for efficiency). -// This method also assumes the Config was created by Load, due to races, +// This method also assumes the Config was created by Load or LoadFile function, it returns error +// if it was not. We can't re-validate or apply globals here due to races, // read more https://github.com/prometheus/prometheus/issues/15538. func (c *Config) GetScrapeConfigs() ([]*ScrapeConfig, error) { if !c.loaded { - return nil, errors.New("config was not validated and loaded; GetScrapeConfigs method can only be used on configuration from the config.Load or config.LoadFile") + // Programmatic error, we warn before more confusing errors would happen due to lack of the globalization. + return nil, errors.New("scrape config cannot be fetched, main config was not validated and loaded correctly; should not happen") } scfgs := make([]*ScrapeConfig, len(c.ScrapeConfigs)) diff --git a/config/config_test.go b/config/config_test.go index 0b1feea8b1..437b858b00 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2557,7 +2557,7 @@ func TestGetScrapeConfigs_Loaded(t *testing.T) { t.Run("without load", func(t *testing.T) { c := &Config{} _, err := c.GetScrapeConfigs() - require.EqualError(t, err, "main config scrape configs was not validated and loaded; GetScrapeConfigs method can only be used on configuration from the config.Load or config.LoadFile") + require.EqualError(t, err, "scrape config cannot be fetched, main config was not validated and loaded correctly; should not happen") }) t.Run("with load", func(t *testing.T) { c, err := Load("", promslog.NewNopLogger())