Skip to content

Commit f05f5bd

Browse files
Pothulapatiroboquat
authored andcommitted
[ws-rollout] Add prometheus init check
When given a non-usable `--prometheus-url`, We start the rollout without verifying if the prometheus is reachable or not. This is a problem as we will be unable to get the metrics from prometheus and hence the rollout will be reverted later causing unnecessary time waste. This can be prevented by performing a simple check to see if the prometheus is reachable or not. `up` query is used instead of key metrics as we can't be sure of their existence. Signed-off-by: Tarun Pothulapati <[email protected]>
1 parent 72af187 commit f05f5bd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

components/workspace-rollout-job/cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ var rootCmd = &cobra.Command{
9696
return err
9797
}
9898

99+
// Check if prometheus is reachable
100+
err = analysis.CheckPrometheusReachable(ctx, conf.prometheusURL)
101+
if err != nil {
102+
log.WithError(err).Fatal("init: prometheus is not reachable")
103+
return err
104+
}
105+
99106
prometheusAnalyzer, err := analysis.NewWorkspaceKeyMetricsAnalyzer(ctx, config, conf.prometheusURL, conf.targetPositivePercentage, 30305)
100107
if err != nil {
101108
log.WithError(err).Fatal("failed to create a prometheus client")

components/workspace-rollout-job/pkg/analysis/analyzer.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ package analysis
66

77
import (
88
"context"
9+
"time"
10+
11+
"github.com/prometheus/client_golang/api"
12+
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
913
)
1014

1115
type Decision int
@@ -21,3 +25,24 @@ type Analyzer interface {
2125
// repeatedly to determine whether to move forward on the rollout or not.
2226
MoveForward(ctx context.Context, clusterName string) (Decision, error)
2327
}
28+
29+
func CheckPrometheusReachable(ctx context.Context, prometheusURL string) error {
30+
if prometheusURL == "" {
31+
return nil
32+
}
33+
34+
client, err := api.NewClient(api.Config{
35+
Address: prometheusURL,
36+
})
37+
if err != nil {
38+
return err
39+
}
40+
41+
v1Client := v1.NewAPI(client)
42+
_, _, err = v1Client.Query(ctx, "up", time.Now())
43+
if err != nil {
44+
return err
45+
}
46+
47+
return nil
48+
}

0 commit comments

Comments
 (0)