From b755ab934172eca0ef439ac73f995bc65c852e71 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Tue, 24 May 2016 16:41:35 -0700 Subject: [PATCH] Changed the stats endpoints --- command/agent/alloc_endpoint.go | 38 ++++++++++++++++++++++++ command/agent/http.go | 3 +- command/agent/stats_endpoint.go | 52 ++------------------------------- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/command/agent/alloc_endpoint.go b/command/agent/alloc_endpoint.go index 7668bfd85..0558d7d45 100644 --- a/command/agent/alloc_endpoint.go +++ b/command/agent/alloc_endpoint.go @@ -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 +} diff --git a/command/agent/http.go b/command/agent/http.go index 8db323373..5b5f6fd51 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -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)) diff --git a/command/agent/stats_endpoint.go b/command/agent/stats_endpoint.go index 7c1c8e5dc..8a3fb8d16 100644 --- a/command/agent/stats_endpoint.go +++ b/command/agent/stats_endpoint.go @@ -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 }