Skip to content

feat: expose git ops metrics #5582

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

Merged
merged 2 commits into from
Aug 12, 2024
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
39 changes: 38 additions & 1 deletion pkg/deployment/gitOps/git/GitServiceAzure.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
bean2 "github.com/devtron-labs/devtron/api/bean/gitOps"
globalUtil "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/retryFunc"
"github.com/microsoft/azure-devops-go-api/azuredevops"
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
Expand All @@ -39,6 +40,12 @@ type GitAzureClient struct {
}

func (impl GitAzureClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {

start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("GetRepoUrl", "GitAzureClient", start, err)
}()

url, exists, err := impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
return "", err
Expand Down Expand Up @@ -67,7 +74,12 @@ func NewGitAzureClient(token string, host string, project string, logger *zap.Su
}, err
}

func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) (err error) {
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
}()

clientAzure := *impl.client
gitRepository, err := clientAzure.GetRepository(context.Background(), git.GetRepositoryArgs{
RepositoryId: &config.GitRepoName,
Expand All @@ -85,6 +97,12 @@ func (impl GitAzureClient) DeleteRepository(config *bean2.GitOpsConfigDto) error
}

func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
}()

detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
url, repoExists, err := impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
Expand Down Expand Up @@ -152,6 +170,13 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
}

func (impl GitAzureClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {

var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateReadme", "GitAzureClient", start, err)
}()

cfg := &ChartConfig{
ChartName: config.GitRepoName,
ChartLocation: "",
Expand Down Expand Up @@ -272,6 +297,12 @@ func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig
}

func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl string, exists bool, err error) {

start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("repoExists", "GitAzureClient", start, err)
}()

ctx := context.Background()
// Get first page of the list of team projects for your organization
clientAzure := *impl.client
Expand All @@ -295,6 +326,12 @@ func (impl GitAzureClient) repoExists(repoName, projectName string) (repoUrl str
}

func (impl GitAzureClient) ensureProjectAvailabilityOnHttp(repoName string) (bool, error) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnHttp", "GitAzureClient", start, err)
}()

for count := 0; count < 5; count++ {
_, exists, err := impl.repoExists(repoName, impl.project)
if err == nil && exists {
Expand Down
40 changes: 37 additions & 3 deletions pkg/deployment/gitOps/git/GitServiceBitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,30 @@ func NewGitBitbucketClient(username, token, host string, logger *zap.SugaredLogg
}
}

func (impl GitBitbucketClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
func (impl GitBitbucketClient) DeleteRepository(config *bean2.GitOpsConfigDto) (err error) {
start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("DeleteRepository", "GitBitbucketClient", start, err)
}()
repoOptions := &bitbucket.RepositoryOptions{
Owner: config.BitBucketWorkspaceId,
RepoSlug: config.GitRepoName,
IsPrivate: "true",
Project: config.BitBucketProjectKey,
}
_, err := impl.client.Repositories.Repository.Delete(repoOptions)
_, err = impl.client.Repositories.Repository.Delete(repoOptions)
if err != nil {
impl.logger.Errorw("error in deleting repo gitlab", "repoName", repoOptions.RepoSlug, "err", err)
impl.logger.Errorw("error in deleting repo gitlab", "repoName", config.GitRepoName, "err", err)
}
return err
}

func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("GetRepoUrl", "GitBitbucketClient", start, err)
}()

repoOptions := &bitbucket.RepositoryOptions{
Owner: config.BitBucketWorkspaceId,
Project: config.BitBucketProjectKey,
Expand All @@ -95,6 +104,12 @@ func (impl GitBitbucketClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUr
}

func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
var err error
start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("CreateRepository", "GitBitbucketClient", start, err)
}()

detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)

workSpaceId := config.BitBucketWorkspaceId
Expand All @@ -107,6 +122,7 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
Description: config.Description,
Project: projectKey,
}

repoUrl, repoExists, err := impl.repoExists(repoOptions)
if err != nil {
impl.logger.Errorw("error in communication with bitbucket", "repoOptions", repoOptions, "err", err)
Expand Down Expand Up @@ -168,6 +184,12 @@ func (impl GitBitbucketClient) CreateRepository(ctx context.Context, config *bea
}

func (impl GitBitbucketClient) repoExists(repoOptions *bitbucket.RepositoryOptions) (repoUrl string, exists bool, err error) {

start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("repoExists", "GitBitbucketClient", start, err)
}()

repo, err := impl.client.Repositories.Repository.Get(repoOptions)
if repo == nil && err.Error() == BITBUCKET_REPO_NOT_FOUND_ERROR {
return "", false, nil
Expand Down Expand Up @@ -202,6 +224,12 @@ func getDir() string {
}

func (impl GitBitbucketClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {
var err error
start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("CreateReadme", "GitBitbucketClient", start, err)
}()

cfg := &ChartConfig{
ChartName: config.GitRepoName,
ChartLocation: "",
Expand Down Expand Up @@ -242,6 +270,12 @@ func (impl GitBitbucketClient) cleanUp(cloneDir string) {
}

func (impl GitBitbucketClient) CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *bean2.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error) {

start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("CommitValues", "GitBitbucketClient", start, err)
}()

homeDir, err := os.UserHomeDir()
if err != nil {
impl.logger.Errorw("error in getting home dir", "err", err)
Expand Down
46 changes: 44 additions & 2 deletions pkg/deployment/gitOps/git/GitServiceGithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"fmt"
bean2 "github.com/devtron-labs/devtron/api/bean/gitOps"
globalUtil "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/retryFunc"
"github.com/google/go-github/github"
"go.uber.org/zap"
Expand Down Expand Up @@ -76,7 +77,13 @@ func NewGithubClient(host string, token string, org string, logger *zap.SugaredL
}

func (impl GitHubClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
_, err := impl.client.Repositories.Delete(context.Background(), config.GitHubOrgId, config.GitRepoName)
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("DeleteRepository", "GitHubClient", start, err)
}()

_, err = impl.client.Repositories.Delete(context.Background(), config.GitHubOrgId, config.GitRepoName)
if err != nil {
impl.logger.Errorw("repo deletion failed for github", "repo", config.GitRepoName, "err", err)
return err
Expand All @@ -85,9 +92,15 @@ func (impl GitHubClient) DeleteRepository(config *bean2.GitOpsConfigDto) error {
}

func (impl GitHubClient) CreateRepository(ctx context.Context, config *bean2.GitOpsConfigDto) (url string, isNew bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitHubClient", start, err)
}()

detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
repoExists := true
url, err := impl.GetRepoUrl(config)
url, err = impl.GetRepoUrl(config)
if err != nil {
responseErr, ok := err.(*github.ErrorResponse)
if !ok || responseErr.Response.StatusCode != 404 {
Expand Down Expand Up @@ -158,6 +171,12 @@ func (impl GitHubClient) CreateRepository(ctx context.Context, config *bean2.Git
}

func (impl GitHubClient) CreateReadme(ctx context.Context, config *bean2.GitOpsConfigDto) (string, error) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateReadme", "GitHubClient", start, err)
}()

cfg := &ChartConfig{
ChartName: config.GitRepoName,
ChartLocation: "",
Expand All @@ -176,6 +195,12 @@ func (impl GitHubClient) CreateReadme(ctx context.Context, config *bean2.GitOpsC
}

func (impl GitHubClient) CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *bean2.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error) {

start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitHubClient", start, err)
}()

branch := "master"
path := filepath.Join(config.ChartLocation, config.FileName)
newFile := false
Expand Down Expand Up @@ -226,6 +251,11 @@ func (impl GitHubClient) CommitValues(ctx context.Context, config *ChartConfig,
}

func (impl GitHubClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl string, err error) {
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("GetRepoUrl", "GitHubClient", start, err)
}()

ctx := context.Background()
repo, _, err := impl.client.Repositories.Get(ctx, impl.org, config.GitRepoName)
if err != nil {
Expand All @@ -235,6 +265,12 @@ func (impl GitHubClient) GetRepoUrl(config *bean2.GitOpsConfigDto) (repoUrl stri
}

func (impl GitHubClient) ensureProjectAvailabilityOnHttp(config *bean2.GitOpsConfigDto) (bool, error) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnHttp", "GitHubClient", start, err)
}()

count := 0
for count < 3 {
count = count + 1
Expand All @@ -255,6 +291,12 @@ func (impl GitHubClient) ensureProjectAvailabilityOnHttp(config *bean2.GitOpsCon
}

func (impl GitHubClient) ensureProjectAvailabilityOnSsh(projectName string, repoUrl string) (bool, error) {
var err error
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("ensureProjectAvailabilityOnSsh", "GitHubClient", start, err)
}()

count := 0
for count < 3 {
count = count + 1
Expand Down
Loading
Loading