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

Parametrized/periodic jobs per child tagged metric emmision #4392

Merged
merged 1 commit into from
Jun 21, 2018
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
20 changes: 20 additions & 0 deletions client/allocrunner/taskrunner/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,26 @@ func NewTaskRunner(logger *log.Logger, config *config.Config,
},
}

if tc.alloc.Job.ParentID != "" {
tc.baseLabels = append(tc.baseLabels, metrics.Label{
Name: "parent_id",
Value: tc.alloc.Job.ParentID,
})
if strings.Contains(tc.alloc.Job.Name, "/dispatch-") {
tc.baseLabels = append(tc.baseLabels, metrics.Label{
Name: "dispatch_id",
Value: strings.Split(tc.alloc.Job.Name, "/dispatch-")[1],
Copy link
Contributor

Choose a reason for hiding this comment

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

See below comment. Consider pulling this into a helper function to reduce code duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for the input, I wrote helper function hour ago and now I'm trying to figure where to place it =)

Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, its probably fine to not introduce a helper method. This and the other place we do this, are in two different packages and we would have to put one helper method in a util package that both can import. Doesn't seem worth it this time..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That what's stopped me - 2 different packages without imports intersection.
Thanks @preetapan, and thanks @chelseakomlo for really usefull code review tips!

})
}
if strings.Contains(tc.alloc.Job.Name, "/periodic-") {
tc.baseLabels = append(tc.baseLabels, metrics.Label{
Name: "periodic_id",
Value: strings.Split(tc.alloc.Job.Name, "/periodic-")[1],
Copy link
Contributor

Choose a reason for hiding this comment

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

First check the length after splitting the string to ensure the length is as expected before assigning the second element.

})
}
return tc
}

return tc
}

Expand Down
25 changes: 25 additions & 0 deletions nomad/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"golang.org/x/time/rate"

"strings"

"github.com/armon/go-metrics"
memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -625,6 +627,29 @@ func (s *Server) publishJobSummaryMetrics(stopCh chan struct{}) {
Value: name,
},
}

if strings.Contains(summary.JobID, "/dispatch-") {
jobInfo := strings.Split(summary.JobID, "/dispatch-")
labels = append(labels, metrics.Label{
Name: "parent_id",
Value: jobInfo[0],
}, metrics.Label{
Name: "dispatch_id",
Value: jobInfo[1],
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't assume that this array will have two elements, first check for safety.

})
}

if strings.Contains(summary.JobID, "/periodic-") {
jobInfo := strings.Split(summary.JobID, "/periodic-")
labels = append(labels, metrics.Label{
Name: "parent_id",
Value: jobInfo[0],
}, metrics.Label{
Name: "periodic_id",
Value: jobInfo[1],
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure that the length of jobInfo is what is expected for safety.

Copy link
Contributor

Choose a reason for hiding this comment

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

In this specific case, the contains check in line 642 guards against that. If the string ends with /periodic- that slice should still have 2 elements with one being the empty string.

})
Copy link
Contributor

Choose a reason for hiding this comment

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

Two points overall:

  • Consider adding a helper function where there is code duplication
  • Don't assume the length of the array is what is as expected, first check before indexing.

}

metrics.SetGaugeWithLabels([]string{"nomad", "job_summary", "queued"},
float32(tgSummary.Queued), labels)
metrics.SetGaugeWithLabels([]string{"nomad", "job_summary", "complete"},
Expand Down