diff --git a/api/client/client.go b/api/client/client.go index 30e3bf9bb6..d3c502ac1e 100644 --- a/api/client/client.go +++ b/api/client/client.go @@ -33,6 +33,7 @@ type apiClient interface { ContainerRename(containerID, newContainerName string) error ContainerRestart(containerID string, timeout int) error ContainerStatPath(containerID, path string) (types.ContainerPathStat, error) + ContainerStats(containerID string, stream bool) (io.ReadCloser, error) ContainerStart(containerID string) error ContainerStop(containerID string, timeout int) error ContainerTop(containerID string, arguments []string) (types.ContainerProcessList, error) diff --git a/api/client/lib/container_stats.go b/api/client/lib/container_stats.go new file mode 100644 index 0000000000..a18fae7a0b --- /dev/null +++ b/api/client/lib/container_stats.go @@ -0,0 +1,22 @@ +package lib + +import ( + "io" + "net/url" +) + +// ContainerStats returns near realtime stats for a given container. +// It's up to the caller to close the io.ReadCloser returned. +func (cli *Client) ContainerStats(containerID string, stream bool) (io.ReadCloser, error) { + query := url.Values{} + query.Set("stream", "0") + if stream { + query.Set("stream", "1") + } + + resp, err := cli.get("/containers/"+containerID+"/stats", query, nil) + if err != nil { + return nil, err + } + return resp.body, err +} diff --git a/api/client/stats.go b/api/client/stats.go index 659764bfbc..6ffee7f27c 100644 --- a/api/client/stats.go +++ b/api/client/stats.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "net/url" "sort" "strings" "sync" @@ -37,26 +36,19 @@ type stats struct { } func (s *containerStats) Collect(cli *DockerCli, streamStats bool) { - v := url.Values{} - if streamStats { - v.Set("stream", "1") - } else { - v.Set("stream", "0") - } - serverResp, err := cli.call("GET", "/containers/"+s.Name+"/stats?"+v.Encode(), nil, nil) + responseBody, err := cli.client.ContainerStats(s.Name, streamStats) if err != nil { s.mu.Lock() s.err = err s.mu.Unlock() return } - - defer serverResp.body.Close() + defer responseBody.Close() var ( previousCPU uint64 previousSystem uint64 - dec = json.NewDecoder(serverResp.body) + dec = json.NewDecoder(responseBody) u = make(chan error, 1) ) go func() { @@ -156,18 +148,13 @@ func (cli *DockerCli) CmdStats(args ...string) error { showAll := len(names) == 0 if showAll { - v := url.Values{} - if *all { - v.Set("all", "1") + options := types.ContainerListOptions{ + All: *all, } - body, _, err := readBody(cli.call("GET", "/containers/json?"+v.Encode(), nil, nil)) + cs, err := cli.client.ContainerList(options) if err != nil { return err } - var cs []types.Container - if err := json.Unmarshal(body, &cs); err != nil { - return err - } for _, c := range cs { names = append(names, c.ID[:12]) } @@ -202,14 +189,15 @@ func (cli *DockerCli) CmdStats(args ...string) error { err error } getNewContainers := func(c chan<- watch) { - res, err := cli.call("GET", "/events", nil, nil) + options := types.EventsOptions{} + resBody, err := cli.client.Events(options) if err != nil { c <- watch{err: err} return } - defer res.body.Close() + defer resBody.Close() - dec := json.NewDecoder(res.body) + dec := json.NewDecoder(resBody) for { var j *jsonmessage.JSONMessage if err := dec.Decode(&j); err != nil {