mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Fixes content-type/length for stats stream=false
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
parent
e70d680d72
commit
855a056af7
5 changed files with 45 additions and 25 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue