فهرست منبع

Implement docker stats with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera 9 سال پیش
والد
کامیت
e59d54bd99
3فایلهای تغییر یافته به همراه33 افزوده شده و 22 حذف شده
  1. 1 0
      api/client/client.go
  2. 22 0
      api/client/lib/container_stats.go
  3. 10 22
      api/client/stats.go

+ 1 - 0
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)

+ 22 - 0
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
+}

+ 10 - 22
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 {