Skip to content

Commit

Permalink
feature: Add metrics to bootstrapGitops (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4sen authored and laszlocph committed Dec 20, 2023
1 parent 097e6ed commit bc04dac
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/dashboard/server/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/gimlet-io/gimlet-cli/cmd/dashboard/config"
"github.com/gimlet-io/gimlet-cli/cmd/dashboard/dynamicconfig"
Expand All @@ -30,6 +31,7 @@ import (
gitConfig "github.com/go-git/go-git/v5/config"
gitHttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/gorilla/securecookie"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -201,6 +203,7 @@ func bootstrapGitops(w http.ResponseWriter, r *http.Request) {
}

ctx := r.Context()
perf := ctx.Value("perf").(*prometheus.HistogramVec)
config := ctx.Value("config").(*config.Config)
dynamicConfig := ctx.Value("dynamicConfig").(*dynamicconfig.DynamicConfig)
tokenManager := ctx.Value("tokenManager").(customScm.NonImpersonatedTokenManager)
Expand Down Expand Up @@ -236,26 +239,33 @@ func bootstrapGitops(w http.ResponseWriter, r *http.Request) {

user := ctx.Value("user").(*model.User)

t0 := time.Now()
_, err = AssureRepoExists(
environment.InfraRepo,
user.AccessToken,
gitToken,
user.Login,
gitServiceImpl,
)
perf.WithLabelValues("gimlet_bootstrap_gitops_infra_repo_exists").Observe(float64(time.Since(t0).Seconds()))

if err != nil {
logrus.Errorf("cannot assure repo exists: %s", err)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

t0 = time.Now()
_, err = AssureRepoExists(
environment.AppsRepo,
user.AccessToken,
gitToken,
user.Login,
gitServiceImpl,
)
perf.WithLabelValues("gimlet_bootstrap_gitops_apps_repo_exists").Observe(float64(time.Since(t0).Seconds()))

if err != nil {
logrus.Errorf("cannot assure repo exists: %s", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -267,6 +277,8 @@ func bootstrapGitops(w http.ResponseWriter, r *http.Request) {

scmURL := dynamicConfig.ScmURL()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)

t0 = time.Now()
_, _, err = BootstrapEnv(
gitRepoCache,
gitServiceImpl,
Expand All @@ -280,12 +292,16 @@ func bootstrapGitops(w http.ResponseWriter, r *http.Request) {
false,
scmURL,
)
perf.WithLabelValues("gimlet_bootstrap_gitops_bootstrap_infra_repo").Observe(float64(time.Since(t0).Seconds()))

if err != nil {
logrus.Error(err)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

t0 = time.Now()
_, _, err = BootstrapEnv(
gitRepoCache,
gitServiceImpl,
Expand All @@ -299,6 +315,8 @@ func bootstrapGitops(w http.ResponseWriter, r *http.Request) {
true,
scmURL,
)
perf.WithLabelValues("gimlet_bootstrap_gitops_bootstrap_apps_repo").Observe(float64(time.Since(t0).Seconds()))

if err != nil {
logrus.Error(err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -313,6 +331,7 @@ func bootstrapGitops(w http.ResponseWriter, r *http.Request) {
return
}

t0 = time.Now()
_, err = BootstrapNotifications(
gitRepoCache,
config.Host,
Expand All @@ -321,19 +340,25 @@ func bootstrapGitops(w http.ResponseWriter, r *http.Request) {
gitToken,
gitUser,
)
perf.WithLabelValues("gimlet_bootstrap_gitops_bootstrap_notifications").Observe(float64(time.Since(t0).Seconds()))

if err != nil {
logrus.Error(err)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

t0 = time.Now()
err = installAgent(environment, gitRepoCache, config, dynamicConfig, gitToken)
perf.WithLabelValues("gimlet_bootstrap_gitops_install_agent").Observe(float64(time.Since(t0).Seconds()))

if err != nil {
logrus.Errorf("cannot install agent: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}
Expand Down

0 comments on commit bc04dac

Please sign in to comment.