Skip to content

Commit 49a4004

Browse files
feat: expose git ops metrics (#5582)
1 parent c0aac33 commit 49a4004

File tree

4 files changed

+174
-8
lines changed

4 files changed

+174
-8
lines changed

pkg/deployment/gitOps/git/GitServiceAzure.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
bean2 "github.com/devtron-labs/devtron/api/bean/gitOps"
25+
globalUtil "github.com/devtron-labs/devtron/util"
2526
"github.com/devtron-labs/devtron/util/retryFunc"
2627
"github.com/microsoft/azure-devops-go-api/azuredevops"
2728
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
@@ -39,6 +40,12 @@ type GitAzureClient struct {
3940
}
4041

4142
func (impl GitAzureClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
43+
44+
start := time.Now()
45+
defer func() {
46+
globalUtil.TriggerGitOpsMetrics("GetRepoUrl", "GitAzureClient", start, err)
47+
}()
48+
4249
url, exists, err := impl.repoExists(config.GitRepoName, impl.project)
4350
if err != nil {
4451
return "", err
@@ -67,7 +74,12 @@ func NewGitAzureClient(token string, host string, project string, logger *zap.Su
6774
}, err
6875
}
6976

70-
func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
77+
func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) (err error) {
78+
start := time.Now()
79+
defer func() {
80+
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
81+
}()
82+
7183
clientAzure := *impl.client
7284
gitRepository, err := clientAzure.GetRepository(context.Background(), git.GetRepositoryArgs{
7385
RepositoryId: &config.GitRepoName,
@@ -85,6 +97,12 @@ func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) error
8597
}
8698

8799
func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
100+
var err error
101+
start := time.Now()
102+
defer func() {
103+
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
104+
}()
105+
88106
detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
89107
url, repoExists, err := impl.repoExists(config.GitRepoName, impl.project)
90108
if err != nil {
@@ -152,6 +170,13 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
152170
}
153171

154172
func (impl GitAzureClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {
173+
174+
var err error
175+
start := time.Now()
176+
defer func() {
177+
globalUtil.TriggerGitOpsMetrics("CreateReadme", "GitAzureClient", start, err)
178+
}()
179+
155180
cfg := &ChartConfig{
156181
ChartName: config.GitRepoName,
157182
ChartLocation: "",
@@ -272,6 +297,12 @@ func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig
272297
}
273298

274299
func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl string, exists bool, err error) {
300+
301+
start := time.Now()
302+
defer func() {
303+
globalUtil.TriggerGitOpsMetrics("repoExists", "GitAzureClient", start, err)
304+
}()
305+
275306
ctx := context.Background()
276307
// Get first page of the list of team projects for your organization
277308
clientAzure := *impl.client
@@ -295,6 +326,12 @@ func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl str
295326
}
296327

297328
func (impl GitAzureClient) ensureProjectAvailabilityOnHttp(repoName string) (bool, error) {
329+
var err error
330+
start := time.Now()
331+
defer func() {
332+
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnHttp", "GitAzureClient", start, err)
333+
}()
334+
298335
for count := 0; count < 5; count++ {
299336
_, exists, err := impl.repoExists(repoName, impl.project)
300337
if err == nil && exists {

pkg/deployment/gitOps/git/GitServiceBitbucket.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,30 @@ func NewGitBitbucketClient(username, token, host string, logger *zap.SugaredLogg
6363
}
6464
}
6565

66-
func (impl GitBitbucketClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
66+
func (impl GitBitbucketClient) DeleteRepository(config *bean2.GitOpsConfigDto) (err error) {
67+
start := time.Now()
68+
defer func() {
69+
util.TriggerGitOpsMetrics("DeleteRepository", "GitBitbucketClient", start, err)
70+
}()
6771
repoOptions := &bitbucket.RepositoryOptions{
6872
Owner: config.BitBucketWorkspaceId,
6973
RepoSlug: config.GitRepoName,
7074
IsPrivate: "true",
7175
Project: config.BitBucketProjectKey,
7276
}
73-
_, err := impl.client.Repositories.Repository.Delete(repoOptions)
77+
_, err = impl.client.Repositories.Repository.Delete(repoOptions)
7478
if err != nil {
75-
impl.logger.Errorw("error in deleting repo gitlab", "repoName", repoOptions.RepoSlug, "err", err)
79+
impl.logger.Errorw("error in deleting repo gitlab", "repoName", config.GitRepoName, "err", err)
7680
}
7781
return err
7882
}
7983

8084
func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
85+
start := time.Now()
86+
defer func() {
87+
util.TriggerGitOpsMetrics("GetRepoUrl", "GitBitbucketClient", start, err)
88+
}()
89+
8190
repoOptions := &bitbucket.RepositoryOptions{
8291
Owner: config.BitBucketWorkspaceId,
8392
Project: config.BitBucketProjectKey,
@@ -95,6 +104,12 @@ func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUr
95104
}
96105

97106
func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
107+
var err error
108+
start := time.Now()
109+
defer func() {
110+
util.TriggerGitOpsMetrics("CreateRepository", "GitBitbucketClient", start, err)
111+
}()
112+
98113
detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
99114

100115
workSpaceId := config.BitBucketWorkspaceId
@@ -107,6 +122,7 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
107122
Description: config.Description,
108123
Project: projectKey,
109124
}
125+
110126
repoUrl, repoExists, err := impl.repoExists(repoOptions)
111127
if err != nil {
112128
impl.logger.Errorw("error in communication with bitbucket", "repoOptions", repoOptions, "err", err)
@@ -168,6 +184,12 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
168184
}
169185

170186
func (impl GitBitbucketClient) repoExists(repoOptions *bitbucket.RepositoryOptions) (repoUrl string, exists bool, err error) {
187+
188+
start := time.Now()
189+
defer func() {
190+
util.TriggerGitOpsMetrics("repoExists", "GitBitbucketClient", start, err)
191+
}()
192+
171193
repo, err := impl.client.Repositories.Repository.Get(repoOptions)
172194
if repo == nil && err.Error() == BITBUCKET_REPO_NOT_FOUND_ERROR {
173195
return "", false, nil
@@ -202,6 +224,12 @@ func getDir() string {
202224
}
203225

204226
func (impl GitBitbucketClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {
227+
var err error
228+
start := time.Now()
229+
defer func() {
230+
util.TriggerGitOpsMetrics("CreateReadme", "GitBitbucketClient", start, err)
231+
}()
232+
205233
cfg := &ChartConfig{
206234
ChartName: config.GitRepoName,
207235
ChartLocation: "",
@@ -242,6 +270,12 @@ func (impl GitBitbucketClient) cleanUp(cloneDir string) {
242270
}
243271

244272
func (impl GitBitbucketClient) CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *bean2.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error) {
273+
274+
start := time.Now()
275+
defer func() {
276+
util.TriggerGitOpsMetrics("CommitValues", "GitBitbucketClient", start, err)
277+
}()
278+
245279
homeDir, err := os.UserHomeDir()
246280
if err != nil {
247281
impl.logger.Errorw("error in getting home dir", "err", err)

pkg/deployment/gitOps/git/GitServiceGithub.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/tls"
2222
"fmt"
2323
bean2 "github.com/devtron-labs/devtron/api/bean/gitOps"
24+
globalUtil "github.com/devtron-labs/devtron/util"
2425
"github.com/devtron-labs/devtron/util/retryFunc"
2526
"github.com/google/go-github/github"
2627
"go.uber.org/zap"
@@ -76,7 +77,13 @@ func NewGithubClient(host string, token string, org string, logger *zap.SugaredL
7677
}
7778

7879
func (impl GitHubClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
79-
_, err := impl.client.Repositories.Delete(context.Background(), config.GitHubOrgId, config.GitRepoName)
80+
var err error
81+
start := time.Now()
82+
defer func() {
83+
globalUtil.TriggerGitOpsMetrics("DeleteRepository", "GitHubClient", start, err)
84+
}()
85+
86+
_, err = impl.client.Repositories.Delete(context.Background(), config.GitHubOrgId, config.GitRepoName)
8087
if err != nil {
8188
impl.logger.Errorw("repo deletion failed for github", "repo", config.GitRepoName, "err", err)
8289
return err
@@ -85,9 +92,15 @@ func (impl GitHubClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
8592
}
8693

8794
func (impl GitHubClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
95+
var err error
96+
start := time.Now()
97+
defer func() {
98+
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitHubClient", start, err)
99+
}()
100+
88101
detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
89102
repoExists := true
90-
url, err := impl.GetRepoUrl(config)
103+
url, err = impl.GetRepoUrl(config)
91104
if err != nil {
92105
responseErr, ok := err.(*github.ErrorResponse)
93106
if !ok || responseErr.Response.StatusCode != 404 {
@@ -158,6 +171,12 @@ func (impl GitHubClient) CreateRepository(ctx context.Context, config *bean2.Git
158171
}
159172

160173
func (impl GitHubClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {
174+
var err error
175+
start := time.Now()
176+
defer func() {
177+
globalUtil.TriggerGitOpsMetrics("CreateReadme", "GitHubClient", start, err)
178+
}()
179+
161180
cfg := &ChartConfig{
162181
ChartName: config.GitRepoName,
163182
ChartLocation: "",
@@ -176,6 +195,12 @@ func (impl GitHubClient) CreateReadme(ctx context.Context, config *bean2.GitOpsC
176195
}
177196

178197
func (impl GitHubClient) CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *bean2.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error) {
198+
199+
start := time.Now()
200+
defer func() {
201+
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitHubClient", start, err)
202+
}()
203+
179204
branch := "master"
180205
path := filepath.Join(config.ChartLocation, config.FileName)
181206
newFile := false
@@ -226,6 +251,11 @@ func (impl GitHubClient) CommitValues(ctx context.Context, config *ChartConfig,
226251
}
227252

228253
func (impl GitHubClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
254+
start := time.Now()
255+
defer func() {
256+
globalUtil.TriggerGitOpsMetrics("GetRepoUrl", "GitHubClient", start, err)
257+
}()
258+
229259
ctx := context.Background()
230260
repo, _, err := impl.client.Repositories.Get(ctx, impl.org, config.GitRepoName)
231261
if err != nil {
@@ -235,6 +265,12 @@ func (impl GitHubClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl stri
235265
}
236266

237267
func (impl GitHubClient) ensureProjectAvailabilityOnHttp(config *bean2.GitOpsConfigDto) (bool, error) {
268+
var err error
269+
start := time.Now()
270+
defer func() {
271+
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnHttp", "GitHubClient", start, err)
272+
}()
273+
238274
count := 0
239275
for count < 3 {
240276
count = count + 1
@@ -255,6 +291,12 @@ func (impl GitHubClient) ensureProjectAvailabilityOnHttp(config *bean2.GitOpsCon
255291
}
256292

257293
func (impl GitHubClient) ensureProjectAvailabilityOnSsh(projectName string, repoUrl string) (bool, error) {
294+
var err error
295+
start := time.Now()
296+
defer func() {
297+
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnSsh", "GitHubClient", start, err)
298+
}()
299+
258300
count := 0
259301
for count < 3 {
260302
count = count + 1

0 commit comments

Comments
 (0)