diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index c4e3fe7914..6a3fe0c736 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -1195,7 +1195,8 @@ func main() { g.Add( func() error { <-reloadReady.C - + ticker := time.NewTicker(time.Duration(cfg.autoReloadInterval)) + defer ticker.Stop() for { select { case <-hup: @@ -1220,15 +1221,19 @@ func main() { } } } - case <-time.Tick(time.Duration(cfg.autoReloadInterval)): + + case <-ticker.C: if !cfg.enableAutoReload { continue } currentChecksum, err := config.GenerateChecksum(cfg.configFile) if err != nil { - checksum = currentChecksum + logger.Error("Failed to generate checksum during configuration reload", "err", err) - } else if currentChecksum == checksum { + + continue + } + if currentChecksum == checksum { continue } logger.Info("Configuration file change detected, reloading the configuration.") @@ -1237,6 +1242,8 @@ func main() { logger.Error("Error reloading config", "err", err) } else { checksum = currentChecksum + ticker.Stop() + ticker = time.NewTicker(time.Duration(cfg.autoReloadInterval)) } case <-cancel: return nil diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index e4262f1b3b..e1ae5aab05 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -417,7 +417,27 @@ func getCurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames func TestAgentSuccessfulStartup(t *testing.T) { t.Parallel() - prom := exec.Command(promPath, "-test.main", "--agent", "--web.listen-address=0.0.0.0:0", "--config.file="+agentConfig) + tempDir := t.TempDir() + configPath := filepath.Join(tempDir, "agent.yml") + + configContent := ` +global: + scrape_interval: 15s + +scrape_configs: + - job_name: "prometheus" + static_configs: + - targets: ["localhost:9090"] +` + require.NoError(t, os.WriteFile(configPath, []byte(configContent), 0o644)) + + prom := exec.Command(promPath, + "-test.main", + "--enable-feature=agent", + "--web.listen-address=0.0.0.0:0", + "--config.file="+configPath, + "--storage.tsdb.path="+filepath.Join(tempDir, "data"), + ) require.NoError(t, prom.Start()) actualExitStatus := 0 @@ -428,8 +448,8 @@ func TestAgentSuccessfulStartup(t *testing.T) { case err := <-done: t.Logf("prometheus agent should be still running: %v", err) actualExitStatus = prom.ProcessState.ExitCode() - case <-time.After(startupTime): - prom.Process.Kill() + case <-time.After(3 * time.Second): + _ = prom.Process.Kill() } require.Equal(t, 0, actualExitStatus) } diff --git a/cmd/prometheus/reload_test.go b/cmd/prometheus/reload_test.go index c59e51b316..6fcd987481 100644 --- a/cmd/prometheus/reload_test.go +++ b/cmd/prometheus/reload_test.go @@ -137,9 +137,12 @@ func runTestSteps(t *testing.T, steps []struct { require.NoError(t, os.WriteFile(configFilePath, []byte(step.configText), 0o644), "Failed to write config file for step") require.Eventually(t, func() bool { - return verifyScrapeInterval(t, baseURL, step.expectedInterval) && - verifyConfigReloadMetric(t, baseURL, step.expectedMetric) + if step.expectedMetric == 0 { + return verifyScrapeInterval(t, baseURL, step.expectedInterval) || verifyConfigReloadMetric(t, baseURL, step.expectedMetric) + } + return verifyScrapeInterval(t, baseURL, step.expectedInterval) && verifyConfigReloadMetric(t, baseURL, step.expectedMetric) }, 10*time.Second, 500*time.Millisecond, "Prometheus config reload didn't happen in time") + } }