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

test case for 5540 #5590

Merged
merged 2 commits into from
Apr 30, 2019
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
71 changes: 70 additions & 1 deletion command/agent/metrics_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package agent
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

metrics "github.com/armon/go-metrics"
"github.com/armon/go-metrics"
cgbaker marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHTTP_MetricsWithIllegalMethod(t *testing.T) {
Expand Down Expand Up @@ -56,3 +61,67 @@ func TestHTTP_Metrics(t *testing.T) {
})
})
}

// When emitting metrics, the client should use the local copy of the allocs with
// updated task states (not the copy submitted by the server).
func TestHTTP_FreshClientAllocMetrics(t *testing.T) {
t.Parallel()
require := require.New(t)
numTasks := 10

httpTest(t, func(c *Config) {
c.Telemetry.PublishAllocationMetrics = true
c.Telemetry.PublishNodeMetrics = true
c.Telemetry.BackwardsCompatibleMetrics = false
c.Telemetry.DisableTaggedMetrics = false
}, func(s *TestAgent) {
// Create the job, wait for it to finish
job := mock.BatchJob()
job.TaskGroups[0].Count = numTasks
testutil.RegisterJob(t, s.RPC, job)
testutil.WaitForResult(func() (bool, error) {
time.Sleep(200 * time.Millisecond)
args := &structs.JobSpecificRequest{}
args.JobID = job.ID
args.QueryOptions.Region = "global"
var resp structs.SingleJobResponse
err := s.RPC("Job.GetJob", args, &resp)
return err == nil && resp.Job.Status == "dead", err
}, func(err error) {
require.Fail("timed-out waiting for job to complete")
cgbaker marked this conversation as resolved.
Show resolved Hide resolved
})

// wait for metrics to converge
var pending, running, terminal float32 = -1.0, -1.0, -1.0
testutil.WaitForResultRetries(100, func() (bool, error) {
time.Sleep(100 * time.Millisecond)
req, err := http.NewRequest("GET", "/v1/metrics", nil)
require.NoError(err)
respW := httptest.NewRecorder()

obj, err := s.Server.MetricsRequest(respW, req)
if err != nil {
return false, err
}

metrics := obj.(metrics.MetricsSummary)
for _, g := range metrics.Gauges {
if strings.HasSuffix(g.Name, "client.allocations.pending") {
pending = g.Value
}
if strings.HasSuffix(g.Name, "client.allocations.running") {
running = g.Value
}
if strings.HasSuffix(g.Name, "client.allocations.terminal") {
terminal = g.Value
}
}
// client alloc metrics should reflect that there is numTasks terminal allocs and no other allocs
return pending == float32(0) && running == float32(0) &&
terminal == float32(numTasks), nil
}, func(err error) {
require.Fail("timed out waiting for metrics to converge",
"pending: %v, running: %v, terminal: %v", pending, running, terminal)
})
})
}
25 changes: 21 additions & 4 deletions vendor/github.com/armon/go-metrics/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/armon/go-metrics/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions vendor/github.com/armon/go-metrics/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions vendor/github.com/armon/go-metrics/inmem.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions vendor/github.com/armon/go-metrics/inmem_endpoint.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading