Skip to content
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

bugfix: Metrics initialization #2767

Merged
merged 6 commits into from
Dec 17, 2022
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
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} {
Fabianoshz marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -627,9 +627,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