mirror of https://github.com/docker/docs.git
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
|
@ -572,7 +572,16 @@ func (s *Server) getContainersStats(version version.Version, w http.ResponseWrit
|
||||||
return fmt.Errorf("Missing parameter")
|
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 {
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/daemon/execdriver"
|
"github.com/docker/docker/daemon/execdriver"
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (daemon *Daemon) ContainerStats(name string, stream bool, out io.Writer) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var pre_cpu_stats types.CpuStats
|
|
||||||
for first_v := range updates {
|
var preCpuStats types.CpuStats
|
||||||
first_update := first_v.(*execdriver.ResourceStats)
|
getStat := func(v interface{}) *types.Stats {
|
||||||
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 {
|
|
||||||
update := v.(*execdriver.ResourceStats)
|
update := v.(*execdriver.ResourceStats)
|
||||||
ss := convertToAPITypes(update.Stats)
|
ss := convertStatsToAPITypes(update.Stats)
|
||||||
ss.PreCpuStats = pre_cpu_stats
|
ss.PreCpuStats = preCpuStats
|
||||||
ss.MemoryStats.Limit = uint64(update.MemoryLimit)
|
ss.MemoryStats.Limit = uint64(update.MemoryLimit)
|
||||||
ss.Read = update.Read
|
ss.Read = update.Read
|
||||||
ss.CpuStats.SystemUsage = update.SystemUsage
|
ss.CpuStats.SystemUsage = update.SystemUsage
|
||||||
pre_cpu_stats = ss.CpuStats
|
preCpuStats = ss.CpuStats
|
||||||
if err := enc.Encode(ss); err != nil {
|
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
|
// TODO: handle the specific broken pipe
|
||||||
daemon.UnsubscribeToContainerStats(name, updates)
|
daemon.UnsubscribeToContainerStats(name, updates)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !stream {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"github.com/docker/libcontainer/cgroups"
|
"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.
|
// 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{}
|
s := &types.Stats{}
|
||||||
if ls.Interfaces != nil {
|
if ls.Interfaces != nil {
|
||||||
s.Network = types.Network{}
|
s.Network = types.Network{}
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"github.com/docker/libcontainer"
|
"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.
|
// 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.
|
// TODO Windows. Refactor accordingly to fill in stats.
|
||||||
s := &types.Stats{}
|
s := &types.Stats{}
|
||||||
return s
|
return s
|
||||||
|
|
|
@ -10,14 +10,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) {
|
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)
|
id := strings.TrimSpace(out)
|
||||||
err := waitRun(id)
|
err := waitRun(id)
|
||||||
c.Assert(err, check.IsNil)
|
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(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
|
var v *types.Stats
|
||||||
err = json.NewDecoder(body).Decode(&v)
|
err = json.NewDecoder(body).Decode(&v)
|
||||||
|
|
Loading…
Reference in New Issue