Skip to content

Commit

Permalink
Merge pull request #5847 from hashicorp/f-5554-metrics-namespace-label
Browse files Browse the repository at this point in the history
metrics: add namespace label to allocation metrics
  • Loading branch information
Chris Baker committed Jun 18, 2019
2 parents 25f7876 + 80fa207 commit 2eacf19
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

IMPROVEMENTS:
* api: use region from job hcl when not provided as query parameter in job registration and plan endpoints [[GH-5664](https://github.com/hashicorp/nomad/pull/5664)]
* metrics: add namespace label as appropriate to metrics [[GH-5847](https://github.com/hashicorp/nomad/issues/5847)]

BUG FIXES:

Expand Down
4 changes: 4 additions & 0 deletions client/allocrunner/taskrunner/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ func (tr *TaskRunner) initLabels() {
Name: "task",
Value: tr.taskName,
},
{
Name: "namespace",
Value: tr.alloc.Namespace,
},
}

if tr.alloc.Job.ParentID != "" {
Expand Down
31 changes: 31 additions & 0 deletions client/allocrunner/taskrunner/task_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,3 +2021,34 @@ func testWaitForTaskToStart(t *testing.T, tr *TaskRunner) {
require.NoError(t, err)
})
}

// TestTaskRunner_BaseLabels tests that the base labels for the task metrics
// are set appropriately.
func TestTaskRunner_BaseLabels(t *testing.T) {
t.Parallel()
require := require.New(t)

alloc := mock.BatchAlloc()
alloc.Namespace = "not-default"
task := alloc.Job.TaskGroups[0].Tasks[0]
task.Driver = "raw_exec"
task.Config = map[string]interface{}{
"command": "whoami",
}

config, cleanup := testTaskRunnerConfig(t, alloc, task.Name)
defer cleanup()

tr, err := NewTaskRunner(config)
require.NoError(err)

labels := map[string]string{}
for _, e := range tr.baseLabels {
labels[e.Name] = e.Value
}
require.Equal(alloc.Job.Name, labels["job"])
require.Equal(alloc.TaskGroup, labels["task_group"])
require.Equal(task.Name, labels["task"])
require.Equal(alloc.ID, labels["alloc_id"])
require.Equal(alloc.Namespace, labels["namespace"])
}
4 changes: 4 additions & 0 deletions nomad/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ func (s *Server) iterateJobSummaryMetrics(summary *structs.JobSummary) {
Name: "task_group",
Value: name,
},
{
Name: "namespace",
Value: summary.Namespace,
},
}

if strings.Contains(summary.JobID, "/dispatch-") {
Expand Down

0 comments on commit 2eacf19

Please sign in to comment.