v2: stats: fix errors

Also add some comments.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2020-01-15 12:22:56 +01:00
parent 3c7b7761ce
commit 0f6955c30e
2 changed files with 37 additions and 22 deletions

View File

@ -43,6 +43,8 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
return
}
// If the container isn't returning, then let's not bother and return
// immediately.
state, err := ctnr.State()
if err != nil {
utils.InternalServerError(w, err)
@ -56,15 +58,14 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
return
}
var preRead time.Time
var preCPUStats docker.CPUStats
stats, err := ctnr.GetContainerStats(&libpod.ContainerStats{})
if err != nil {
utils.InternalServerError(w, errors.Wrapf(err, "Failed to obtain Container %s stats", name))
return
}
var preRead time.Time
var preCPUStats docker.CPUStats
if query.Stream {
preRead = time.Now()
preCPUStats = docker.CPUStats{
@ -81,16 +82,19 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
time.Sleep(DefaultStatsPeriod)
}
// Anonymous function to get the cgroup on demand.
getCgroup := func() *cgroups.CgroupControl {
cgroupPath, err := ctnr.CGroupPath()
if err != nil {
utils.InternalServerError(w, err)
return
return &cgroups.CgroupControl{}
}
cgroup, err := cgroups.Load(cgroupPath)
if err != nil {
utils.InternalServerError(w, err)
return
return &cgroups.CgroupControl{}
}
return cgroup
}
for ok := true; ok; ok = query.Stream {
@ -104,12 +108,13 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
continue
}
stats, err := ctnr.GetContainerStats(stats)
if err != nil {
utils.InternalServerError(w, err)
return
}
cgroup := getCgroup()
cgroupStat, err := cgroup.Stat()
if err != nil {
cgroupStat = &cgroups.Metrics{}
}
stats, err := ctnr.GetContainerStats(stats)
if err != nil {
utils.InternalServerError(w, err)
return
@ -120,8 +125,13 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
return
}
// FIXME: network inspection does not yet work entirely
net := make(map[string]docker.NetworkStats)
net[inspect.NetworkSettings.EndpointID] = docker.NetworkStats{
networkName := inspect.NetworkSettings.EndpointID
if networkName == "" {
networkName = "network"
}
net[networkName] = docker.NetworkStats{
RxBytes: stats.NetInput,
RxPackets: 0,
RxErrors: 0,
@ -192,17 +202,21 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
preRead = s.Read
bits, err := json.Marshal(s.CPUStats)
if err != nil {
logrus.Errorf("unable to marshal cpu stats: %q", err)
logrus.Errorf("Unable to marshal cpu stats: %q", err)
}
if err := json.Unmarshal(bits, &preCPUStats); err != nil {
logrus.Errorf("unable to unmarshal previous stats: %q", err)
logrus.Errorf("Unable to unmarshal previous stats: %q", err)
}
// Only sleep when we're streaming.
if query.Stream {
time.Sleep(DefaultStatsPeriod)
}
}
}
func toBlkioStatEntry(entries []cgroups.BlkIOEntry) []docker.BlkioStatEntry {
results := make([]docker.BlkioStatEntry, 0, len(entries))
results := make([]docker.BlkioStatEntry, len(entries))
for i, e := range entries {
bits, err := json.Marshal(e)
if err != nil {

View File

@ -33,6 +33,7 @@ func WriteResponse(w http.ResponseWriter, code int, value interface{}) {
}
func WriteJSON(w http.ResponseWriter, code int, value interface{}) {
// FIXME: we don't need to write the header in all/some circumstances.
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)