Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ws-rollout] Add prometheus init check #16000

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions components/workspace-rollout-job/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ var rootCmd = &cobra.Command{
return err
}

// Check if prometheus is reachable
err = analysis.CheckPrometheusReachable(ctx, conf.prometheusURL)
if err != nil {
log.WithError(err).Fatal("init: prometheus is not reachable")
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could remove the error return here, log.Fatal will already exit the process

}

prometheusAnalyzer, err := analysis.NewWorkspaceKeyMetricsAnalyzer(ctx, config, conf.prometheusURL, conf.targetPositivePercentage, 30305)
if err != nil {
log.WithError(err).Fatal("failed to create a prometheus client")
Expand Down
25 changes: 25 additions & 0 deletions components/workspace-rollout-job/pkg/analysis/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package analysis

import (
"context"
"time"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
)

type Decision int
Expand All @@ -21,3 +25,24 @@ type Analyzer interface {
// repeatedly to determine whether to move forward on the rollout or not.
MoveForward(ctx context.Context, clusterName string) (Decision, error)
}

func CheckPrometheusReachable(ctx context.Context, prometheusURL string) error {
if prometheusURL == "" {
return nil
}
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about moving this function to WorkspaceKeyMetricsAnalyzer, e.g. WorkspaceKeyMetricsAnalyzer.CheckPrometheusReachable(), and then use the api client of the analyzer to check if it's reachable. You'd only need the v1Client.Query(ctx, "up", time.Now()) part then, deduplicates the construction of the API clients


client, err := api.NewClient(api.Config{
Address: prometheusURL,
})
if err != nil {
return err
}

v1Client := v1.NewAPI(client)
_, _, err = v1Client.Query(ctx, "up", time.Now())
if err != nil {
return err
}

return nil
}