Check health & ready: move to flags (#12223)

This makes it more consistent with other command like import rules. We
don't have stricts rules and uniformity accross promtool unfortunately,
but I think it's better to only have the http config on relevant check
commands to avoid thinking Prometheus can e.g. check the config over the
wire.

Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
This commit is contained in:
Julien Pivotto 2023-04-05 09:45:39 +02:00 committed by GitHub
parent 6cecb87941
commit 391473141d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 24 deletions

View file

@ -89,7 +89,6 @@ func main() {
app.HelpFlag.Short('h') app.HelpFlag.Short('h')
checkCmd := app.Command("check", "Check the resources for validity.") checkCmd := app.Command("check", "Check the resources for validity.")
checkCmd.Flag("http.config.file", "HTTP client configuration file for promtool to connect to Prometheus.").PlaceHolder("<filename>").ExistingFileVar(&httpConfigFilePath)
sdCheckCmd := checkCmd.Command("service-discovery", "Perform service discovery for the given job name and report the results, including relabeling.") sdCheckCmd := checkCmd.Command("service-discovery", "Perform service discovery for the given job name and report the results, including relabeling.")
sdConfigFile := sdCheckCmd.Arg("config-file", "The prometheus config file.").Required().ExistingFile() sdConfigFile := sdCheckCmd.Arg("config-file", "The prometheus config file.").Required().ExistingFile()
@ -117,16 +116,12 @@ func main() {
).Required().ExistingFiles() ).Required().ExistingFiles()
checkServerHealthCmd := checkCmd.Command("healthy", "Check if the Prometheus server is healthy.") checkServerHealthCmd := checkCmd.Command("healthy", "Check if the Prometheus server is healthy.")
serverHealthURLArg := checkServerHealthCmd.Arg( checkServerHealthCmd.Flag("http.config.file", "HTTP client configuration file for promtool to connect to Prometheus.").PlaceHolder("<filename>").ExistingFileVar(&httpConfigFilePath)
"server", checkServerHealthCmd.Flag("url", "The URL for the Prometheus server.").Default("http://localhost:9090").URLVar(&serverURL)
"The URL of the Prometheus server to check (e.g. http://localhost:9090)",
).URL()
checkServerReadyCmd := checkCmd.Command("ready", "Check if the Prometheus server is ready.") checkServerReadyCmd := checkCmd.Command("ready", "Check if the Prometheus server is ready.")
serverReadyURLArg := checkServerReadyCmd.Arg( checkServerReadyCmd.Flag("http.config.file", "HTTP client configuration file for promtool to connect to Prometheus.").PlaceHolder("<filename>").ExistingFileVar(&httpConfigFilePath)
"server", checkServerReadyCmd.Flag("url", "The URL for the Prometheus server.").Default("http://localhost:9090").URLVar(&serverURL)
"The URL of the Prometheus server to check (e.g. http://localhost:9090)",
).URL()
checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.") checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.")
ruleFiles := checkRulesCmd.Arg( ruleFiles := checkRulesCmd.Arg(
@ -292,10 +287,10 @@ func main() {
os.Exit(CheckConfig(*agentMode, *checkConfigSyntaxOnly, newLintConfig(*checkConfigLint, *checkConfigLintFatal), *configFiles...)) os.Exit(CheckConfig(*agentMode, *checkConfigSyntaxOnly, newLintConfig(*checkConfigLint, *checkConfigLintFatal), *configFiles...))
case checkServerHealthCmd.FullCommand(): case checkServerHealthCmd.FullCommand():
os.Exit(checkErr(CheckServerStatus(*serverHealthURLArg, checkHealth, httpRoundTripper))) os.Exit(checkErr(CheckServerStatus(serverURL, checkHealth, httpRoundTripper)))
case checkServerReadyCmd.FullCommand(): case checkServerReadyCmd.FullCommand():
os.Exit(checkErr(CheckServerStatus(*serverReadyURLArg, checkReadiness, httpRoundTripper))) os.Exit(checkErr(CheckServerStatus(serverURL, checkReadiness, httpRoundTripper)))
case checkWebConfigCmd.FullCommand(): case checkWebConfigCmd.FullCommand():
os.Exit(CheckWebConfig(*webConfigFiles...)) os.Exit(CheckWebConfig(*webConfigFiles...))
@ -390,12 +385,10 @@ func (ls lintConfig) lintDuplicateRules() bool {
return ls.all || ls.duplicateRules return ls.all || ls.duplicateRules
} }
const promDefaultURL = "http://localhost:9090"
// Check server status - healthy & ready. // Check server status - healthy & ready.
func CheckServerStatus(serverURL *url.URL, checkEndpoint string, roundTripper http.RoundTripper) error { func CheckServerStatus(serverURL *url.URL, checkEndpoint string, roundTripper http.RoundTripper) error {
if serverURL == nil { if serverURL.Scheme == "" {
serverURL, _ = url.Parse(promDefaultURL) serverURL.Scheme = "http"
} }
config := api.Config{ config := api.Config{

View file

@ -58,7 +58,6 @@ Check the resources for validity.
| Flag | Description | | Flag | Description |
| --- | --- | | --- | --- |
| <code class="text-nowrap">--http.config.file</code> | HTTP client configuration file for promtool to connect to Prometheus. |
| <code class="text-nowrap">--extended</code> | Print extended information related to the cardinality of the metrics. | | <code class="text-nowrap">--extended</code> | Print extended information related to the cardinality of the metrics. |
@ -137,11 +136,12 @@ Check if the Prometheus server is healthy.
###### Arguments ###### Flags
| Argument | Description | | Flag | Description | Default |
| --- | --- | | --- | --- | --- |
| server | The URL of the Prometheus server to check (e.g. http://localhost:9090) | | <code class="text-nowrap">--http.config.file</code> | HTTP client configuration file for promtool to connect to Prometheus. | |
| <code class="text-nowrap">--url</code> | The URL for the Prometheus server. | `http://localhost:9090` |
@ -152,11 +152,12 @@ Check if the Prometheus server is ready.
###### Arguments ###### Flags
| Argument | Description | | Flag | Description | Default |
| --- | --- | | --- | --- | --- |
| server | The URL of the Prometheus server to check (e.g. http://localhost:9090) | | <code class="text-nowrap">--http.config.file</code> | HTTP client configuration file for promtool to connect to Prometheus. | |
| <code class="text-nowrap">--url</code> | The URL for the Prometheus server. | `http://localhost:9090` |