diff --git a/client/driver/executor/executor.go b/client/driver/executor/executor.go index da484fbfe..d2929a2d4 100644 --- a/client/driver/executor/executor.go +++ b/client/driver/executor/executor.go @@ -27,6 +27,10 @@ import ( "github.com/hashicorp/nomad/nomad/structs" ) +const ( + pidScanInterval = 5 * time.Second +) + // Executor is the interface which allows a driver to launch and supervise // a process type Executor interface { @@ -631,7 +635,7 @@ func (e *UniversalExecutor) collectPids() { case <-timer.C: pids, err := e.getAllPids() e.logger.Printf("DIPTANU PIDS %#v", pids) - timer.Reset(5 * time.Second) + timer.Reset(pidScanInterval) if err != nil { e.logger.Printf("[DEBUG] executor: error collecting pids: %v", err) } @@ -721,6 +725,5 @@ func (e *UniversalExecutor) resourceUsagePids() (*cstructs.TaskResourceUsage, er Swap: totalSwap, } - return &cstructs.TaskResourceUsage{MemoryStats: totalMemory, CpuStats: totalCPU}, nil - + return &cstructs.TaskResourceUsage{MemoryStats: totalMemory, CpuStats: totalCPU, Timestamp: ts}, nil } diff --git a/command/agent/stats_endpoint.go b/command/agent/stats_endpoint.go index 35d0d8c65..7c1c8e5dc 100644 --- a/command/agent/stats_endpoint.go +++ b/command/agent/stats_endpoint.go @@ -3,6 +3,9 @@ package agent import ( "fmt" "net/http" + "strconv" + + "github.com/hashicorp/nomad/client" ) func (s *HTTPServer) StatsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { @@ -10,6 +13,15 @@ func (s *HTTPServer) StatsRequest(resp http.ResponseWriter, req *http.Request) ( return nil, clientNotRunning } + var tsRequest bool + + // Check if the user has requested time series + if qp := req.URL.Query().Get("ts"); qp != "" { + if tsReq, err := strconv.ParseBool(qp); err == nil { + tsRequest = tsReq + } + } + clientStats := s.agent.client.StatsReporter() // Return the host stats if alloc ID is not present @@ -30,14 +42,21 @@ func (s *HTTPServer) StatsRequest(resp http.ResponseWriter, req *http.Request) ( if err != nil { return nil, err } - return taskStats.ResourceUsage(), nil + return s.getStats(tsRequest, taskStats), nil } // Return the resource usage of all the tasks in an allocation if task name // is not specified res := make(map[string]interface{}) for task, taskStats := range allocStats.AllocStats() { - res[task] = taskStats.ResourceUsage() + res[task] = s.getStats(tsRequest, taskStats) } return res, nil } + +func (s *HTTPServer) getStats(tsRequest bool, taskStats client.TaskStatsReporter) interface{} { + if tsRequest { + return taskStats.ResourceUsageTS() + } + return taskStats.ResourceUsage() +}