Skip to content

Commit 22e8c7f

Browse files
authored
Merge pull request moby#50921 from thaJeztah/stats_errdefs
daemon: GetContainerStats: use errdefs for error-type handling
2 parents 102bb39 + eb2e296 commit 22e8c7f

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

daemon/stats.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"runtime"
88
"time"
99

10+
cerrdefs "github.com/containerd/errdefs"
1011
"github.com/containerd/log"
1112
containertypes "github.com/moby/moby/api/types/container"
1213
"github.com/moby/moby/v2/daemon/container"
@@ -98,8 +99,8 @@ func (daemon *Daemon) unsubscribeToContainerStats(c *container.Container, ch cha
9899
}
99100

100101
// GetContainerStats collects all the stats published by a container
101-
func (daemon *Daemon) GetContainerStats(container *container.Container) (*containertypes.StatsResponse, error) {
102-
stats, err := daemon.stats(container)
102+
func (daemon *Daemon) GetContainerStats(ctr *container.Container) (*containertypes.StatsResponse, error) {
103+
stats, err := daemon.stats(ctr)
103104
if err != nil {
104105
goto done
105106
}
@@ -113,22 +114,21 @@ func (daemon *Daemon) GetContainerStats(container *container.Container) (*contai
113114
}
114115

115116
// We already have the network stats on Windows directly from HCS.
116-
if !container.Config.NetworkDisabled && runtime.GOOS != "windows" {
117-
stats.Networks, err = daemon.getNetworkStats(container)
117+
if !ctr.Config.NetworkDisabled && runtime.GOOS != "windows" {
118+
stats.Networks, err = daemon.getNetworkStats(ctr)
118119
}
119120

120121
done:
121-
switch err.(type) {
122-
case nil:
123-
return stats, nil
124-
case errdefs.ErrConflict, errdefs.ErrNotFound:
125-
// return empty stats containing only name and ID if not running or not found
126-
return &containertypes.StatsResponse{
127-
Name: container.Name,
128-
ID: container.ID,
129-
}, nil
130-
default:
131-
log.G(context.TODO()).Errorf("collecting stats for container %s: %v", container.Name, err)
122+
if err != nil {
123+
if cerrdefs.IsNotFound(err) || cerrdefs.IsConflict(err) {
124+
// return empty stats containing only name and ID if not running or not found
125+
return &containertypes.StatsResponse{
126+
Name: ctr.Name,
127+
ID: ctr.ID,
128+
}, nil
129+
}
130+
log.G(context.TODO()).Errorf("collecting stats for container %s: %v", ctr.Name, err)
132131
return nil, err
133132
}
133+
return stats, nil
134134
}

0 commit comments

Comments
 (0)