Adding a query param to return time series of stats

This commit is contained in:
Diptanu Choudhury 2016-05-18 07:11:25 +02:00
parent c347b27e6a
commit 63166d0e46
2 changed files with 27 additions and 5 deletions

View File

@ -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
}

View File

@ -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()
}