Skip to content

Commit

Permalink
add FetchLatestHostMetricValues
Browse files Browse the repository at this point in the history
  • Loading branch information
stanaka committed May 10, 2016
1 parent b4b6d72 commit 51eb1e4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
)

// MetricValue metric value
Expand Down Expand Up @@ -79,3 +80,35 @@ func (c *Client) FetchLatestMetricValues(hostIDs []string, metricNames []string)

return *(data.LatestMetricValues), err
}

// FetchLatestHostMetricValues fetch latest metrics
func (c *Client) FetchLatestHostMetricValues(hostID string, metricName string, from int64, to int64) ([]MetricValue, error) {
v := url.Values{}
v.Add("name", metricName)
v.Add("from", strconv.FormatInt(from, 10))
v.Add("to", strconv.FormatInt(to, 10))

req, err := http.NewRequest("GET",
fmt.Sprintf("%s?%s",
c.urlFor(fmt.Sprintf("/api/v0/hosts/%s/metrics", hostID)).String(),
v.Encode()),
nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var data struct {
MetricValues *[]MetricValue `json:"metrics"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}

return *(data.MetricValues), err
}

0 comments on commit 51eb1e4

Please sign in to comment.