Added some docs to resource stats endpoint
This commit is contained in:
parent
b9feae89ce
commit
9a96dddc07
|
@ -82,6 +82,8 @@ func DefaultConfig() *config.Config {
|
|||
}
|
||||
}
|
||||
|
||||
// ClientStatsReporter exposes all the APIs related to resource usage of a Nomad
|
||||
// Client
|
||||
type ClientStatsReporter interface {
|
||||
AllocStats() map[string]AllocStatsReporter
|
||||
HostStats() *stats.HostStats
|
||||
|
@ -412,10 +414,14 @@ func (c *Client) Node() *structs.Node {
|
|||
return c.config.Node
|
||||
}
|
||||
|
||||
// StatsReporter exposes the various APIs related resource usage of a Nomad
|
||||
// client
|
||||
func (c *Client) StatsReporter() ClientStatsReporter {
|
||||
return c
|
||||
}
|
||||
|
||||
// AllocStats returns all the stats reporter of the allocations running on a
|
||||
// Nomad client
|
||||
func (c *Client) AllocStats() map[string]AllocStatsReporter {
|
||||
res := make(map[string]AllocStatsReporter)
|
||||
for alloc, ar := range c.allocs {
|
||||
|
@ -424,6 +430,7 @@ func (c *Client) AllocStats() map[string]AllocStatsReporter {
|
|||
return res
|
||||
}
|
||||
|
||||
// HostStats returns all the stats related to a Nomad client
|
||||
func (c *Client) HostStats() *stats.HostStats {
|
||||
val := c.resourceUsage.Peek()
|
||||
ru, _ := val.(*stats.HostStats)
|
||||
|
|
|
@ -9,26 +9,35 @@ func (s *HTTPServer) StatsRequest(resp http.ResponseWriter, req *http.Request) (
|
|||
if s.agent.client == nil {
|
||||
return nil, clientNotRunning
|
||||
}
|
||||
cStatsReporter := s.agent.client.StatsReporter()
|
||||
|
||||
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 cStatsReporter.HostStats(), nil
|
||||
return clientStats.HostStats(), nil
|
||||
}
|
||||
allocStats := cStatsReporter.AllocStats()
|
||||
arStatsReporter, ok := allocStats[allocID]
|
||||
|
||||
// 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 != "" {
|
||||
taskStatsReporter, err := arStatsReporter.TaskStats(task)
|
||||
taskStats, err := allocStats.TaskStats(task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskStatsReporter.ResourceUsage(), nil
|
||||
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, sr := range arStatsReporter.AllocStats() {
|
||||
res[task] = sr.ResourceUsage()
|
||||
for task, taskStats := range allocStats.AllocStats() {
|
||||
res[task] = taskStats.ResourceUsage()
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue