Changed the stats endpoints

This commit is contained in:
Diptanu Choudhury
2016-05-24 16:41:35 -07:00
parent b273eb82a5
commit b755ab9341
3 changed files with 42 additions and 51 deletions

View File

@@ -53,3 +53,41 @@ func (s *HTTPServer) AllocSpecificRequest(resp http.ResponseWriter, req *http.Re
}
return out.Alloc, nil
}
func (s *HTTPServer) ClientAllocRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if s.agent.client == nil {
return nil, clientNotRunning
}
reqSuffix := strings.TrimPrefix(req.URL.Path, "/v1/client/allocation/")
// tokenize the suffix of the path to get the alloc id and find the action
// invoked on the alloc id
tokens := strings.Split(reqSuffix, "/")
if len(tokens) == 1 || tokens[1] != "stats" {
return nil, CodedError(404, "url not found")
}
allocID := tokens[0]
clientStats := s.agent.client.StatsReporter()
allocStats, ok := clientStats.AllocStats()[allocID]
if !ok {
return nil, CodedError(404, "alloc not running in node")
}
if task := req.URL.Query().Get("task"); task != "" {
taskStats, err := allocStats.TaskStats(task)
if err != nil {
return nil, CodedError(404, "task not present in allocation")
}
return taskStats.ResourceUsage(), 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()
}
return res, nil
}

View File

@@ -114,7 +114,8 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) {
s.mux.HandleFunc("/v1/evaluation/", s.wrap(s.EvalSpecificRequest))
s.mux.HandleFunc("/v1/client/fs/", s.wrap(s.FsRequest))
s.mux.HandleFunc("/v1/client/stats/", s.wrap(s.StatsRequest))
s.mux.HandleFunc("/v1/client/stats/", s.wrap(s.ClientStatsRequest))
s.mux.HandleFunc("/v1/client/allocation/", s.wrap(s.ClientAllocRequest))
s.mux.HandleFunc("/v1/agent/self", s.wrap(s.AgentSelfRequest))
s.mux.HandleFunc("/v1/agent/join", s.wrap(s.AgentJoinRequest))

View File

@@ -1,62 +1,14 @@
package agent
import (
"fmt"
"net/http"
"strconv"
"github.com/hashicorp/nomad/client"
)
func (s *HTTPServer) StatsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
func (s *HTTPServer) ClientStatsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if s.agent.client == nil {
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
var allocID, task string
if allocID = req.URL.Query().Get("allocation"); allocID == "" {
return clientStats.HostStats(), nil
}
// Check if the allocation is running on the node
allocStats, ok := clientStats.AllocStats()[allocID]
if !ok {
return nil, fmt.Errorf("alloc %q is not running on this client", allocID)
}
// Return the resource usage of the task if the task name is specified
if task = req.URL.Query().Get("task"); task != "" {
taskStats, err := allocStats.TaskStats(task)
if err != nil {
return nil, err
}
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] = 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()
return clientStats.HostStats(), nil
}