Skip to content

Commit 6137211

Browse files
authored
Merge pull request #4172 from fahedouch/refacto-and-fix-stats-showAll
fix stats for exited container and introduce some refacto
2 parents 2bd1b18 + bcdb5eb commit 6137211

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

pkg/cmd/container/stats.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
9393
closeChan := make(chan error)
9494

9595
var err error
96-
var w = options.Stdout
96+
w := options.Stdout
9797
var tmpl *template.Template
9898
switch options.Format {
9999
case "", "table":
@@ -128,7 +128,6 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
128128
return
129129
}
130130
}
131-
132131
}
133132

134133
// getContainerList get all existing containers (only used when calling `nerdctl stats` without arguments).
@@ -145,7 +144,9 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
145144
continue
146145
}
147146
}
148-
s := statsutil.NewStats(c.ID())
147+
// if an error occurs when getting labels, the ID alone is sufficient for the stats screen.
148+
clabels, _ := c.Labels(ctx)
149+
s := statsutil.NewStats(c.ID(), containerutil.GetContainerName(clabels))
149150
if cStats.add(s) {
150151
waitFirst.Add(1)
151152
go collect(ctx, options.GOptions, s, waitFirst, c.ID(), !options.NoStream)
@@ -176,7 +177,11 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
176177
return
177178
}
178179
}
179-
s := statsutil.NewStats(datacc.ID)
180+
// Load the container to get its ID for retrieving the container name from Labels.
181+
// if an error occurs, the ID alone is sufficient for the stats screen.
182+
container, _ := client.LoadContainer(ctx, datacc.ID)
183+
clabels, _ := container.Labels(ctx)
184+
s := statsutil.NewStats(datacc.ID, containerutil.GetContainerName(clabels))
180185
if cStats.add(s) {
181186
waitFirst.Add(1)
182187
go collect(ctx, options.GOptions, s, waitFirst, datacc.ID, !options.NoStream)
@@ -219,7 +224,9 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
219224
walker := &containerwalker.ContainerWalker{
220225
Client: client,
221226
OnFound: func(ctx context.Context, found containerwalker.Found) error {
222-
s := statsutil.NewStats(found.Container.ID())
227+
// if an error occurs when getting labels, the ID alone is sufficient for the stats screen.
228+
clabels, _ := found.Container.Labels(ctx)
229+
s := statsutil.NewStats(found.Container.ID(), containerutil.GetContainerName(clabels))
223230
if cStats.add(s) {
224231
waitFirst.Add(1)
225232
go collect(ctx, options.GOptions, s, waitFirst, found.Container.ID(), !options.NoStream)
@@ -249,7 +256,7 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
249256

250257
// firstTick is for creating distant CPU readings.
251258
// firstTick stats are not displayed.
252-
var firstTick = true
259+
firstTick := true
253260
for range ticker.C {
254261
cleanScreen()
255262
ccstats := []statsutil.StatsEntry{}
@@ -353,25 +360,17 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s
353360
s.SetError(err)
354361
return
355362
}
356-
357363
go func() {
358364
previousStats := new(statsutil.ContainerStats)
359365
firstSet := true
360366
for {
361-
//task is in the for loop to avoid nil task just after Container creation
367+
// task is in the for loop to avoid nil task just after Container creation
362368
task, err := container.Task(ctx, nil)
363369
if err != nil {
364370
u <- err
365371
continue
366372
}
367373

368-
//labels is in the for loop to avoid nil labels just after Container creation
369-
clabels, err := container.Labels(ctx)
370-
if err != nil {
371-
u <- err
372-
continue
373-
}
374-
375374
metric, err := task.Metrics(ctx)
376375
if err != nil {
377376
u <- err
@@ -395,16 +394,14 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s
395394
u <- err
396395
continue
397396
}
398-
statsEntry.Name = containerutil.GetContainerName(clabels)
399-
statsEntry.ID = container.ID()
400397

401398
if firstSet {
402399
firstSet = false
403400
} else {
404401
s.SetStatistics(statsEntry)
405402
}
406403
u <- nil
407-
//sleep to create distant CPU readings
404+
// sleep to create distant CPU readings
408405
time.Sleep(500 * time.Millisecond)
409406
}
410407
}()
@@ -421,7 +418,7 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s
421418
}
422419
case err := <-u:
423420
if err != nil {
424-
if !errdefs.IsNotFound(err) {
421+
if !errdefs.IsNotFound(err) && !strings.Contains(err.Error(), "no such file or directory") {
425422
s.SetError(err)
426423
continue
427424
}

pkg/statsutil/stats.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,20 @@ type ContainerStats struct {
6969
}
7070

7171
// NewStats is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/formatter_stats.go#L113-L116
72-
func NewStats(containerID string) *Stats {
73-
return &Stats{StatsEntry: StatsEntry{ID: containerID}}
72+
func NewStats(containerID string, containerName string) *Stats {
73+
return &Stats{StatsEntry: StatsEntry{ID: containerID, Name: containerName}}
7474
}
7575

7676
// SetStatistics is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/formatter_stats.go#L87-L93
7777
func (cs *Stats) SetStatistics(s StatsEntry) {
7878
cs.mutex.Lock()
7979
defer cs.mutex.Unlock()
80+
// The statsEntry ID and Name fields are already populated within the cs.StatsEntry
81+
cStatsName := cs.StatsEntry.Name
82+
cStatsID := cs.StatsEntry.ID
8083
cs.StatsEntry = s
84+
cs.StatsEntry.Name = cStatsName
85+
cs.StatsEntry.ID = cStatsID
8186
}
8287

8388
// GetStatistics is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/formatter_stats.go#L95-L100

0 commit comments

Comments
 (0)