diff --git a/api/server/server.go b/api/server/server.go index b85ff197b7..f1ab06dfb7 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -572,7 +572,16 @@ func (s *Server) getContainersStats(version version.Version, w http.ResponseWrit return fmt.Errorf("Missing parameter") } - return s.daemon.ContainerStats(vars["name"], boolValueOrDefault(r, "stream", true), ioutils.NewWriteFlusher(w)) + stream := boolValueOrDefault(r, "stream", true) + var out io.Writer + if !stream { + w.Header().Set("Content-Type", "application/json") + out = w + } else { + out = ioutils.NewWriteFlusher(w) + } + + return s.daemon.ContainerStats(vars["name"], stream, out) } func (s *Server) getContainersLogs(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { diff --git a/daemon/stats.go b/daemon/stats.go index 6798ad887e..81e2f192ee 100644 --- a/daemon/stats.go +++ b/daemon/stats.go @@ -2,9 +2,10 @@ package daemon import ( "encoding/json" + "io" + "github.com/docker/docker/api/types" "github.com/docker/docker/daemon/execdriver" - "io" ) func (daemon *Daemon) ContainerStats(name string, stream bool, out io.Writer) error { @@ -12,31 +13,39 @@ func (daemon *Daemon) ContainerStats(name string, stream bool, out io.Writer) er if err != nil { return err } - var pre_cpu_stats types.CpuStats - for first_v := range updates { - first_update := first_v.(*execdriver.ResourceStats) - first_stats := convertToAPITypes(first_update.Stats) - pre_cpu_stats = first_stats.CpuStats - pre_cpu_stats.SystemUsage = first_update.SystemUsage - break - } - enc := json.NewEncoder(out) - for v := range updates { + + var preCpuStats types.CpuStats + getStat := func(v interface{}) *types.Stats { update := v.(*execdriver.ResourceStats) - ss := convertToAPITypes(update.Stats) - ss.PreCpuStats = pre_cpu_stats + ss := convertStatsToAPITypes(update.Stats) + ss.PreCpuStats = preCpuStats ss.MemoryStats.Limit = uint64(update.MemoryLimit) ss.Read = update.Read ss.CpuStats.SystemUsage = update.SystemUsage - pre_cpu_stats = ss.CpuStats - if err := enc.Encode(ss); err != nil { + preCpuStats = ss.CpuStats + return ss + } + + enc := json.NewEncoder(out) + + if !stream { + // prime the cpu stats so they aren't 0 in the final output + s := getStat(<-updates) + + // now pull stats again with the cpu stats primed + s = getStat(<-updates) + err := enc.Encode(s) + daemon.UnsubscribeToContainerStats(name, updates) + return err + } + + for v := range updates { + s := getStat(v) + if err := enc.Encode(s); err != nil { // TODO: handle the specific broken pipe daemon.UnsubscribeToContainerStats(name, updates) return err } - if !stream { - break - } } return nil } diff --git a/daemon/stats_linux.go b/daemon/stats_linux.go index 31f0ed3de7..146f09491d 100644 --- a/daemon/stats_linux.go +++ b/daemon/stats_linux.go @@ -6,9 +6,9 @@ import ( "github.com/docker/libcontainer/cgroups" ) -// convertToAPITypes converts the libcontainer.Stats to the api specific +// convertStatsToAPITypes converts the libcontainer.Stats to the api specific // structs. This is done to preserve API compatibility and versioning. -func convertToAPITypes(ls *libcontainer.Stats) *types.Stats { +func convertStatsToAPITypes(ls *libcontainer.Stats) *types.Stats { s := &types.Stats{} if ls.Interfaces != nil { s.Network = types.Network{} diff --git a/daemon/stats_windows.go b/daemon/stats_windows.go index 1edea87bc9..13d0d59162 100644 --- a/daemon/stats_windows.go +++ b/daemon/stats_windows.go @@ -5,9 +5,9 @@ import ( "github.com/docker/libcontainer" ) -// convertToAPITypes converts the libcontainer.Stats to the api specific +// convertStatsToAPITypes converts the libcontainer.Stats to the api specific // structs. This is done to preserve API compatibility and versioning. -func convertToAPITypes(ls *libcontainer.Stats) *types.Stats { +func convertStatsToAPITypes(ls *libcontainer.Stats) *types.Stats { // TODO Windows. Refactor accordingly to fill in stats. s := &types.Stats{} return s diff --git a/integration-cli/docker_api_stats_test.go b/integration-cli/docker_api_stats_test.go index a2be352e2b..6a143fed19 100644 --- a/integration-cli/docker_api_stats_test.go +++ b/integration-cli/docker_api_stats_test.go @@ -10,14 +10,16 @@ import ( ) func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) { - out, _ := dockerCmd(c, "run", "-d", "--cpu-quota=2000", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello';done") + out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello'; usleep 100000; done") id := strings.TrimSpace(out) err := waitRun(id) c.Assert(err, check.IsNil) - _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "") + resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "") c.Assert(err, check.IsNil) + c.Assert(resp.ContentLength > 0, check.Equals, true, check.Commentf("should not use chunked encoding")) + c.Assert(resp.Header.Get("Content-Type"), check.Equals, "application/json") var v *types.Stats err = json.NewDecoder(body).Decode(&v)