Skip to content

Commit

Permalink
feat: instrumentation, intitial work (harness#2475)
Browse files Browse the repository at this point in the history
* requested changes
* requested changes
* feat: instrumentation, intitial work
  • Loading branch information
enver-bisevac authored and Harness committed Aug 18, 2024
1 parent eccb57f commit 6d0db95
Show file tree
Hide file tree
Showing 33 changed files with 762 additions and 40 deletions.
18 changes: 18 additions & 0 deletions app/api/controller/pullreq/comment_apply_suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/bootstrap"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/app/services/protection"
"github.com/harness/gitness/contextutil"
"github.com/harness/gitness/git"
Expand Down Expand Up @@ -367,6 +368,23 @@ func (c *Controller) CommentApplySuggestions(
log.Ctx(ctx).Warn().Err(err).Msg("failed to publish PR changed event")
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypePRSuggestionApplied,
Principal: session.Principal.ToPrincipalInfo(),
Path: repo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: repo.ID,
instrument.PropertyRepositoryName: repo.Identifier,
instrument.PropertyPullRequestID: pr.Number,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf(
"failed to insert instrumentation record for pull request suggestion applied operation: %s",
err,
)
}

return CommentApplySuggestionsOutput{
CommitID: commitOut.CommitID.String(),
RuleViolations: violations,
Expand Down
17 changes: 17 additions & 0 deletions app/api/controller/pullreq/comment_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
events "github.com/harness/gitness/app/events/pullreq"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/errors"
"github.com/harness/gitness/git"
"github.com/harness/gitness/store"
Expand Down Expand Up @@ -224,6 +225,22 @@ func (c *Controller) CommentCreate(
c.reportCommentCreated(ctx, pr, session.Principal.ID, act.ID, act.IsReply())
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeCreatePRComment,
Principal: session.Principal.ToPrincipalInfo(),
Path: repo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: repo.ID,
instrument.PropertyRepositoryName: repo.Identifier,
instrument.PropertyPullRequestID: pr.Number,
instrument.PropertyCommentIsReplied: in.IsReply(),
instrument.PropertyCommentContainsSuggestion: len(parseSuggestions(in.Text)) > 0,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for create pull request comment operation: %s", err)
}

return act, nil
}

Expand Down
4 changes: 4 additions & 0 deletions app/api/controller/pullreq/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
pullreqevents "github.com/harness/gitness/app/events/pullreq"
"github.com/harness/gitness/app/services/codecomments"
"github.com/harness/gitness/app/services/codeowners"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/app/services/label"
locker "github.com/harness/gitness/app/services/locker"
"github.com/harness/gitness/app/services/migrate"
Expand Down Expand Up @@ -68,6 +69,7 @@ type Controller struct {
locker *locker.Locker
importer *migrate.PullReq
labelSvc *label.Service
instrumentation instrument.Service
}

func NewController(
Expand Down Expand Up @@ -95,6 +97,7 @@ func NewController(
locker *locker.Locker,
importer *migrate.PullReq,
labelSvc *label.Service,
instrumentation instrument.Service,
) *Controller {
return &Controller{
tx: tx,
Expand All @@ -121,6 +124,7 @@ func NewController(
locker: locker,
importer: importer,
labelSvc: labelSvc,
instrumentation: instrumentation,
}
}

Expand Down
15 changes: 15 additions & 0 deletions app/api/controller/pullreq/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/harness/gitness/app/bootstrap"
pullreqevents "github.com/harness/gitness/app/events/pullreq"
"github.com/harness/gitness/app/services/codeowners"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/app/services/protection"
"github.com/harness/gitness/contextutil"
"github.com/harness/gitness/errors"
Expand Down Expand Up @@ -498,6 +499,20 @@ func (c *Controller) Merge(
log.Ctx(ctx).Warn().Err(err).Msg("failed to publish PR changed event")
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeMergePullRequest,
Principal: session.Principal.ToPrincipalInfo(),
Path: sourceRepo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: sourceRepo.ID,
instrument.PropertyRepositoryName: sourceRepo.Identifier,
instrument.PropertyPullRequestID: pr.Number,
instrument.PropertyMergeStrategy: in.Method,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for merge pr operation: %s", err)
}
return &types.MergeResponse{
SHA: mergeOutput.MergeSHA.String(),
BranchDeleted: branchDeleted,
Expand Down
15 changes: 15 additions & 0 deletions app/api/controller/pullreq/pr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
pullreqevents "github.com/harness/gitness/app/events/pullreq"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/git"
"github.com/harness/gitness/git/sha"
"github.com/harness/gitness/types"
Expand Down Expand Up @@ -154,6 +155,20 @@ func (c *Controller) Create(
log.Ctx(ctx).Warn().Err(err).Msg("failed to publish PR changed event")
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeCreatePullRequest,
Principal: session.Principal.ToPrincipalInfo(),
Path: sourceRepo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: sourceRepo.ID,
instrument.PropertyRepositoryName: sourceRepo.Identifier,
instrument.PropertyPullRequestID: pr.Number,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for create pull request operation: %s", err)
}

return pr, nil
}

Expand Down
16 changes: 16 additions & 0 deletions app/api/controller/pullreq/review_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
events "github.com/harness/gitness/app/events/pullreq"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/git"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"
Expand Down Expand Up @@ -142,6 +143,21 @@ func (c *Controller) ReviewSubmit(
log.Ctx(ctx).Err(err).Msgf("failed to write pull request activity after review submit")
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeReviewPullRequest,
Principal: session.Principal.ToPrincipalInfo(),
Path: repo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: repo.ID,
instrument.PropertyRepositoryName: repo.Identifier,
instrument.PropertyPullRequestID: pr.Number,
instrument.PropertyDecision: in.Decision,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for review pull request operation: %s", err)
}

return review, nil
}

Expand Down
31 changes: 24 additions & 7 deletions app/api/controller/pullreq/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
pullreqevents "github.com/harness/gitness/app/events/pullreq"
"github.com/harness/gitness/app/services/codecomments"
"github.com/harness/gitness/app/services/codeowners"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/app/services/label"
"github.com/harness/gitness/app/services/locker"
"github.com/harness/gitness/app/services/migrate"
Expand Down Expand Up @@ -50,16 +51,32 @@ func ProvideController(tx dbtx.Transactor, urlProvider url.Provider, authorizer
pullreqService *pullreq.Service, ruleManager *protection.Manager, sseStreamer sse.Streamer,
codeOwners *codeowners.Service, locker *locker.Locker, importer *migrate.PullReq,
labelSvc *label.Service,
instrumentation instrument.Service,
) *Controller {
return NewController(tx, urlProvider, authorizer,
pullReqStore, pullReqActivityStore,
return NewController(tx,
urlProvider,
authorizer,
pullReqStore,
pullReqActivityStore,
codeCommentsView,
pullReqReviewStore, pullReqReviewerStore,
pullReqReviewStore,
pullReqReviewerStore,
repoStore,
principalStore, principalInfoCache,
fileViewStore, membershipStore,
principalStore,
principalInfoCache,
fileViewStore,
membershipStore,
checkStore,
rpcClient, eventReporter,
rpcClient,
eventReporter,
codeCommentMigrator,
pullreqService, ruleManager, sseStreamer, codeOwners, locker, importer, labelSvc)
pullreqService,
ruleManager,
sseStreamer,
codeOwners,
locker,
importer,
labelSvc,
instrumentation,
)
}
4 changes: 4 additions & 0 deletions app/api/controller/repo/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
repoevents "github.com/harness/gitness/app/events/repo"
"github.com/harness/gitness/app/services/codeowners"
"github.com/harness/gitness/app/services/importer"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/app/services/keywordsearch"
"github.com/harness/gitness/app/services/label"
"github.com/harness/gitness/app/services/locker"
Expand Down Expand Up @@ -94,6 +95,7 @@ type Controller struct {
repoCheck Check
publicAccess publicaccess.Service
labelSvc *label.Service
instrumentation instrument.Service
}

func NewController(
Expand Down Expand Up @@ -122,6 +124,7 @@ func NewController(
repoCheck Check,
publicAccess publicaccess.Service,
labelSvc *label.Service,
instrumentation instrument.Service,
) *Controller {
return &Controller{
defaultBranch: config.Git.DefaultBranch,
Expand Down Expand Up @@ -149,6 +152,7 @@ func NewController(
repoCheck: repoCheck,
publicAccess: publicAccess,
labelSvc: labelSvc,
instrumentation: instrumentation,
}
}

Expand Down
15 changes: 14 additions & 1 deletion app/api/controller/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/harness/gitness/app/bootstrap"
"github.com/harness/gitness/app/githook"
"github.com/harness/gitness/app/paths"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/audit"
"github.com/harness/gitness/git"
"github.com/harness/gitness/resources"
Expand Down Expand Up @@ -164,7 +165,19 @@ func (c *Controller) Create(ctx context.Context, session *auth.Session, in *Crea
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert audit log for create repository operation: %s", err)
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeRepositoryCreate,
Principal: session.Principal.ToPrincipalInfo(),
Path: repo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: repo.ID,
instrument.PropertyRepositoryName: repo.Identifier,
instrument.PropertyRepositoryCreationType: instrument.CreationTypeCreate,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for create repository operation: %s", err)
}
// index repository if files are created
if !repo.IsEmpty {
err = c.indexer.Index(ctx, repo)
Expand Down
16 changes: 16 additions & 0 deletions app/api/controller/repo/create_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import (

"github.com/harness/gitness/app/api/controller"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/app/services/protection"
"github.com/harness/gitness/git"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

"github.com/rs/zerolog/log"
)

// CreateBranchInput used for branch creation apis.
Expand Down Expand Up @@ -93,5 +96,18 @@ func (c *Controller) CreateBranch(ctx context.Context,
return nil, nil, fmt.Errorf("failed to map branch: %w", err)
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeCreateBranch,
Principal: session.Principal.ToPrincipalInfo(),
Path: repo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: repo.ID,
instrument.PropertyRepositoryName: repo.Identifier,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for create branch operation: %s", err)
}

return &branch, nil, nil
}
15 changes: 15 additions & 0 deletions app/api/controller/repo/create_commit_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (

"github.com/harness/gitness/app/api/controller"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/app/services/protection"
"github.com/harness/gitness/git"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

"github.com/rs/zerolog/log"
)

// CreateCommitTagInput used for tag creation apis.
Expand Down Expand Up @@ -101,5 +104,17 @@ func (c *Controller) CreateCommitTag(ctx context.Context,
return nil, nil, fmt.Errorf("failed to map tag received from service output: %w", err)
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeCreateTag,
Principal: session.Principal.ToPrincipalInfo(),
Path: repo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: repo.ID,
instrument.PropertyRepositoryName: repo.Identifier,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for create tag operation: %s", err)
}
return &commitTag, nil, nil
}
14 changes: 14 additions & 0 deletions app/api/controller/repo/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/paths"
"github.com/harness/gitness/app/services/importer"
"github.com/harness/gitness/app/services/instrument"
"github.com/harness/gitness/audit"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -114,6 +115,19 @@ func (c *Controller) Import(ctx context.Context, session *auth.Session, in *Impo
log.Warn().Msgf("failed to insert audit log for import repository operation: %s", err)
}

err = c.instrumentation.Track(ctx, instrument.Event{
Type: instrument.EventTypeRepositoryCreate,
Principal: session.Principal.ToPrincipalInfo(),
Path: repo.Path,
Properties: map[instrument.Property]any{
instrument.PropertyRepositoryID: repo.ID,
instrument.PropertyRepositoryName: repo.Identifier,
instrument.PropertyRepositoryCreationType: instrument.CreationTypeImport,
},
})
if err != nil {
log.Ctx(ctx).Warn().Msgf("failed to insert instrumentation record for import repository operation: %s", err)
}
return GetRepoOutputWithAccess(ctx, false, repo), nil
}

Expand Down
Loading

0 comments on commit 6d0db95

Please sign in to comment.