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: Initialize metrics with empty tags #2847

Merged
merged 3 commits into from
Dec 21, 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
23 changes: 13 additions & 10 deletions server/events/command/project_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,23 @@ type ProjectContext struct {
ExecutionOrderGroup int
}

// SetScopeTags adds ProjectContext tags to a new returned scope.
func (p ProjectContext) SetScopeTags(scope tally.Scope) tally.Scope {
// SetProjectScopeTags adds ProjectContext tags to a new returned scope.
func (p ProjectContext) SetProjectScopeTags(scope tally.Scope) tally.Scope {
v := ""
if p.TerraformVersion != nil {
v = p.TerraformVersion.String()
}
return scope.Tagged(map[string]string{
"base_repo": p.BaseRepo.FullName,
"pr_number": strconv.Itoa(p.Pull.Num),
"project": p.ProjectName,
"project_path": p.RepoRelDir,
"terraform_version": v,
"workspace": p.Workspace,
})

tags := ProjectScopeTags{
BaseRepo: p.BaseRepo.FullName,
PrNumber: strconv.Itoa(p.Pull.Num),
Project: p.ProjectName,
ProjectPath: p.RepoRelDir,
TerraformVersion: v,
Workspace: p.Workspace,
}

return scope.Tagged(tags.Loadtags())
}

// GetShowResultFileName returns the filename (not the path) to store the tf show result
Expand Down
38 changes: 38 additions & 0 deletions server/events/command/scope_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package command

import (
"reflect"
"regexp"
"strings"
)

type ProjectScopeTags struct {
BaseRepo string
PrNumber string
Project string
ProjectPath string
TerraformVersion string
Workspace string
}

func (s ProjectScopeTags) Loadtags() map[string]string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be named like this?

Suggested change
func (s ProjectScopeTags) Loadtags() map[string]string {
func (s ProjectScopeTags) loadTags() map[string]string {

Copy link
Contributor Author

@Fabianoshz Fabianoshz Dec 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I think we have to keep this public since it is used on server/events/instrumented_project_command_runner.go, maybe the function name choice wasn't clear enough?

I guess that to make this private I could make an ScopeTags interface on the metrics package, and call the loadTags from there. Then ProjectScopeTags would be an implementation of that interface passed as argument to a function that load the tags on the scope. I'm not so sure this would work though, but I can give a try.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok, this was a nitpick.

tags := make(map[string]string)

v := reflect.ValueOf(s)
t := v.Type()

for i := 0; i < v.NumField(); i++ {
tags[toSnakeCase(t.Field(i).Name)] = v.Field(i).String()
}

return tags
}

func toSnakeCase(str string) string {
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")

snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
5 changes: 3 additions & 2 deletions server/events/instrumented_project_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type InstrumentedProjectCommandRunner struct {
}

func NewInstrumentedProjectCommandRunner(scope tally.Scope, projectCommandRunner ProjectCommandRunner) *InstrumentedProjectCommandRunner {
scope = scope.SubScope("project")
projectTags := command.ProjectScopeTags{}
scope = scope.Tagged(projectTags.Loadtags())

for _, m := range []string{metrics.ExecutionSuccessMetric, metrics.ExecutionErrorMetric, metrics.ExecutionFailureMetric} {
metrics.InitCounter(scope, m)
Expand Down Expand Up @@ -49,7 +50,7 @@ func (p *InstrumentedProjectCommandRunner) ApprovePolicies(ctx command.ProjectCo

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.SetScopeTags(scope)
scope = ctx.SetProjectScopeTags(scope)
logger := ctx.Log

executionTime := scope.Timer(metrics.ExecutionTimeMetric).Start()
Expand Down
2 changes: 1 addition & 1 deletion server/events/project_command_context_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cb *CommandScopedStatsProjectCommandContextBuilder) BuildProjectContext(
// specifically use the command name in the context instead of the arg
// since we can return multiple commands worth of contexts for a given command name arg
// to effectively pipeline them.
cmd.Scope = cmd.SetScopeTags(cmd.Scope)
cmd.Scope = cmd.SetProjectScopeTags(cmd.Scope)
projectCmds = append(projectCmds, cmd)
}

Expand Down