Skip to content

Commit 6511d86

Browse files
authored
Merge pull request moby#51302 from thaJeztah/fix_one_shot_stats
daemon: Daemon.stats: fill-in container ID and Name when collecting
2 parents 370ca31 + e410daf commit 6511d86

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

daemon/stats.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
4848
var preRead time.Time
4949
getStatJSON := func(v any) *containertypes.StatsResponse {
5050
ss := v.(containertypes.StatsResponse)
51-
ss.Name = ctr.Name
52-
ss.ID = ctr.ID
5351
ss.PreCPUStats = preCPUStats
5452
ss.PreRead = preRead
5553
preCPUStats = ss.CPUStats

daemon/stats_unix.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ func (daemon *Daemon) stats(c *container.Container) (*containertypes.StatsRespon
4747
return nil, err
4848
}
4949
s := &containertypes.StatsResponse{
50+
Name: c.Name,
51+
ID: c.ID,
5052
Read: cs.Read,
5153
}
52-
stats := cs.Metrics
53-
switch t := stats.(type) {
54+
switch t := cs.Metrics.(type) {
5455
case *statsV1.Metrics:
5556
return daemon.statsV1(s, t)
5657
case *statsV2.Metrics:

daemon/stats_windows.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func (daemon *Daemon) stats(c *container.Container) (*containertypes.StatsRespon
2828

2929
// Start with an empty structure
3030
s := &containertypes.StatsResponse{
31+
Name: c.Name,
32+
ID: c.ID,
3133
Read: stats.Read,
3234
NumProcs: platform.NumProcs(),
3335
}

integration-cli/docker_api_containers_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,17 +1134,17 @@ func (s *DockerAPISuite) TestContainerAPIStatsWithNetworkDisabled(c *testing.T)
11341134
assert.NilError(c, err)
11351135
defer apiClient.Close()
11361136

1137-
_, err = apiClient.ContainerCreate(testutil.GetContext(c), client.ContainerCreateOptions{
1137+
ctr, err := apiClient.ContainerCreate(testutil.GetContext(c), client.ContainerCreateOptions{
11381138
Config: &config,
11391139
HostConfig: &container.HostConfig{},
11401140
NetworkingConfig: &network.NetworkingConfig{},
11411141
Name: name,
11421142
})
11431143
assert.NilError(c, err)
11441144

1145-
err = apiClient.ContainerStart(testutil.GetContext(c), name, client.ContainerStartOptions{})
1145+
err = apiClient.ContainerStart(testutil.GetContext(c), ctr.ID, client.ContainerStartOptions{})
11461146
assert.NilError(c, err)
1147-
cli.WaitRun(c, name)
1147+
cli.WaitRun(c, ctr.ID)
11481148

11491149
type b struct {
11501150
stats client.StatsResponseReader
@@ -1153,7 +1153,7 @@ func (s *DockerAPISuite) TestContainerAPIStatsWithNetworkDisabled(c *testing.T)
11531153
bc := make(chan b, 1)
11541154
go func() {
11551155
stats, err := apiClient.ContainerStats(testutil.GetContext(c), name, false)
1156-
bc <- b{stats, err}
1156+
bc <- b{stats: stats, err: err}
11571157
}()
11581158

11591159
// allow some time to stream the stats from the container
@@ -1167,7 +1167,10 @@ func (s *DockerAPISuite) TestContainerAPIStatsWithNetworkDisabled(c *testing.T)
11671167
c.Fatal("stream was not closed after container was removed")
11681168
case sr := <-bc:
11691169
assert.NilError(c, sr.err)
1170-
sr.stats.Body.Close()
1170+
var v container.StatsResponse
1171+
assert.NilError(c, json.NewDecoder(sr.stats.Body).Decode(&v))
1172+
assert.Check(c, is.Equal(v.ID, ctr.ID))
1173+
_ = sr.stats.Body.Close()
11711174
}
11721175
}
11731176

0 commit comments

Comments
 (0)