Skip to content

Commit

Permalink
Metrics initialization (#2767)
Browse files Browse the repository at this point in the history
* Add builder metrics initialization

* Add pull_closed metrics initialization

* Add builder metrics initialization

* use InitCounter from metrics package to initialize prometheus counters
  • Loading branch information
Fabianoshz authored Dec 17, 2022
1 parent a96a88e commit 6a7f79e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 25 deletions.
26 changes: 11 additions & 15 deletions server/events/instrumented_project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import (
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/metrics"
"github.com/uber-go/tally"
)

type InstrumentedProjectCommandBuilder struct {
ProjectCommandBuilder
Logger logging.SimpleLogging
scope tally.Scope
}

func (b *InstrumentedProjectCommandBuilder) BuildApplyCommands(ctx *command.Context, comment *CommentCommand) ([]command.ProjectContext, error) {
scope := ctx.Scope.SubScope("builder")

timer := scope.Timer(metrics.ExecutionTimeMetric).Start()
timer := b.scope.Timer(metrics.ExecutionTimeMetric).Start()
defer timer.Stop()

executionSuccess := scope.Counter(metrics.ExecutionSuccessMetric)
executionError := scope.Counter(metrics.ExecutionErrorMetric)
executionSuccess := b.scope.Counter(metrics.ExecutionSuccessMetric)
executionError := b.scope.Counter(metrics.ExecutionErrorMetric)

projectCmds, err := b.ProjectCommandBuilder.BuildApplyCommands(ctx, comment)

Expand All @@ -33,13 +33,11 @@ func (b *InstrumentedProjectCommandBuilder) BuildApplyCommands(ctx *command.Cont

}
func (b *InstrumentedProjectCommandBuilder) BuildAutoplanCommands(ctx *command.Context) ([]command.ProjectContext, error) {
scope := ctx.Scope.SubScope("builder")

timer := scope.Timer(metrics.ExecutionTimeMetric).Start()
timer := b.scope.Timer(metrics.ExecutionTimeMetric).Start()
defer timer.Stop()

executionSuccess := scope.Counter(metrics.ExecutionSuccessMetric)
executionError := scope.Counter(metrics.ExecutionErrorMetric)
executionSuccess := b.scope.Counter(metrics.ExecutionSuccessMetric)
executionError := b.scope.Counter(metrics.ExecutionErrorMetric)

projectCmds, err := b.ProjectCommandBuilder.BuildAutoplanCommands(ctx)

Expand All @@ -54,13 +52,11 @@ func (b *InstrumentedProjectCommandBuilder) BuildAutoplanCommands(ctx *command.C

}
func (b *InstrumentedProjectCommandBuilder) BuildPlanCommands(ctx *command.Context, comment *CommentCommand) ([]command.ProjectContext, error) {
scope := ctx.Scope.SubScope("builder")

timer := scope.Timer(metrics.ExecutionTimeMetric).Start()
timer := b.scope.Timer(metrics.ExecutionTimeMetric).Start()
defer timer.Stop()

executionSuccess := scope.Counter(metrics.ExecutionSuccessMetric)
executionError := scope.Counter(metrics.ExecutionErrorMetric)
executionSuccess := b.scope.Counter(metrics.ExecutionSuccessMetric)
executionError := b.scope.Counter(metrics.ExecutionErrorMetric)

projectCmds, err := b.ProjectCommandBuilder.BuildPlanCommands(ctx, comment)

Expand Down
36 changes: 30 additions & 6 deletions server/events/instrumented_project_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,52 @@ package events
import (
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/metrics"
"github.com/uber-go/tally"
)

type IntrumentedCommandRunner interface {
Plan(ctx command.ProjectContext) command.ProjectResult
PolicyCheck(ctx command.ProjectContext) command.ProjectResult
Apply(ctx command.ProjectContext) command.ProjectResult
ApprovePolicies(ctx command.ProjectContext) command.ProjectResult
}

type InstrumentedProjectCommandRunner struct {
ProjectCommandRunner
projectCommandRunner ProjectCommandRunner
scope tally.Scope
}

func NewInstrumentedProjectCommandRunner(scope tally.Scope, projectCommandRunner ProjectCommandRunner) *InstrumentedProjectCommandRunner {
scope = scope.SubScope("project")

for _, m := range []string{metrics.ExecutionSuccessMetric, metrics.ExecutionErrorMetric, metrics.ExecutionFailureMetric} {
metrics.InitCounter(scope, m)
}

return &InstrumentedProjectCommandRunner{
projectCommandRunner: projectCommandRunner,
scope: scope,
}
}

func (p *InstrumentedProjectCommandRunner) Plan(ctx command.ProjectContext) command.ProjectResult {
return RunAndEmitStats("plan", ctx, p.ProjectCommandRunner.Plan)
return RunAndEmitStats("plan", ctx, p.projectCommandRunner.Plan, p.scope)
}

func (p *InstrumentedProjectCommandRunner) PolicyCheck(ctx command.ProjectContext) command.ProjectResult {
return RunAndEmitStats("policy check", ctx, p.ProjectCommandRunner.PolicyCheck)
return RunAndEmitStats("policy check", ctx, p.projectCommandRunner.PolicyCheck, p.scope)
}

func (p *InstrumentedProjectCommandRunner) Apply(ctx command.ProjectContext) command.ProjectResult {
return RunAndEmitStats("apply", ctx, p.ProjectCommandRunner.Apply)
return RunAndEmitStats("apply", ctx, p.projectCommandRunner.Apply, p.scope)
}

func RunAndEmitStats(commandName string, ctx command.ProjectContext, execute func(ctx command.ProjectContext) command.ProjectResult) command.ProjectResult {
func (p *InstrumentedProjectCommandRunner) ApprovePolicies(ctx command.ProjectContext) command.ProjectResult {
return RunAndEmitStats("approve policies", ctx, p.projectCommandRunner.Apply, p.scope)
}

func RunAndEmitStats(commandName string, ctx command.ProjectContext, execute func(ctx command.ProjectContext) command.ProjectResult, scope tally.Scope) command.ProjectResult {
// ensures we are differentiating between project level command and overall command
scope := ctx.Scope.SubScope("project")
scope = ctx.SetScopeTags(scope)
logger := ctx.Log

Expand Down
7 changes: 6 additions & 1 deletion server/events/instrumented_pull_closed_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ type InstrumentedPullClosedExecutor struct {
func NewInstrumentedPullClosedExecutor(
scope tally.Scope, log logging.SimpleLogging, cleaner PullCleaner,
) PullCleaner {
scope = scope.SubScope("pullclosed_cleanup")

for _, m := range []string{metrics.ExecutionSuccessMetric, metrics.ExecutionErrorMetric} {
metrics.InitCounter(scope, m)
}

return &InstrumentedPullClosedExecutor{
scope: scope.SubScope("pullclosed_cleanup"),
scope: scope,
log: log,
cleaner: cleaner,
}
Expand Down
8 changes: 8 additions & 0 deletions server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/runatlantis/atlantis/server/core/config/valid"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/metrics"

"github.com/pkg/errors"

Expand Down Expand Up @@ -54,6 +55,12 @@ func NewInstrumentedProjectCommandBuilder(
scope tally.Scope,
logger logging.SimpleLogging,
) *InstrumentedProjectCommandBuilder {
scope = scope.SubScope("builder")

for _, m := range []string{metrics.ExecutionSuccessMetric, metrics.ExecutionErrorMetric} {
metrics.InitCounter(scope, m)
}

return &InstrumentedProjectCommandBuilder{
ProjectCommandBuilder: NewProjectCommandBuilder(
policyChecksSupported,
Expand All @@ -74,6 +81,7 @@ func NewInstrumentedProjectCommandBuilder(
logger,
),
Logger: logger,
scope: scope,
}
}

Expand Down
8 changes: 8 additions & 0 deletions server/metrics/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package metrics

import "github.com/uber-go/tally"

func InitCounter(scope tally.Scope, name string) {
s := scope.Counter(name)
s.Inc(0)
}
7 changes: 4 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,10 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
ProjectCommandRunner: projectCommandRunner,
JobURLSetter: jobs.NewJobURLSetter(router, commitStatusUpdater),
}
instrumentedProjectCmdRunner := &events.InstrumentedProjectCommandRunner{
ProjectCommandRunner: projectOutputWrapper,
}
instrumentedProjectCmdRunner := events.NewInstrumentedProjectCommandRunner(
statsScope,
projectOutputWrapper,
)

policyCheckCommandRunner := events.NewPolicyCheckCommandRunner(
dbUpdater,
Expand Down

0 comments on commit 6a7f79e

Please sign in to comment.