Improved error.

Signed-off-by: bwplotka <bwplotka@gmail.com>
This commit is contained in:
bwplotka 2024-12-12 10:57:27 +00:00
parent aa9fef75ac
commit 134a0c7d78
2 changed files with 5 additions and 3 deletions

View File

@ -309,11 +309,13 @@ func (c Config) String() string {
// GetScrapeConfigs returns the read-only, validated scrape configurations including // GetScrapeConfigs returns the read-only, validated scrape configurations including
// the ones from the scrape_config_files. // 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 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. // read more https://github.com/prometheus/prometheus/issues/15538.
func (c *Config) GetScrapeConfigs() ([]*ScrapeConfig, error) { func (c *Config) GetScrapeConfigs() ([]*ScrapeConfig, error) {
if !c.loaded { 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)) scfgs := make([]*ScrapeConfig, len(c.ScrapeConfigs))

View File

@ -2557,7 +2557,7 @@ func TestGetScrapeConfigs_Loaded(t *testing.T) {
t.Run("without load", func(t *testing.T) { t.Run("without load", func(t *testing.T) {
c := &Config{} c := &Config{}
_, err := c.GetScrapeConfigs() _, 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) { t.Run("with load", func(t *testing.T) {
c, err := Load("", promslog.NewNopLogger()) c, err := Load("", promslog.NewNopLogger())