Skip to content

Commit dad1d36

Browse files
committed
cli/command/container: move debug logs to call-site
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e50c94f commit dad1d36

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

cli/command/container/stats.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
"time"
1212

1313
"github.com/containerd/errdefs"
14+
"github.com/containerd/log"
1415
"github.com/docker/cli/cli"
1516
"github.com/docker/cli/cli/command"
1617
"github.com/docker/cli/cli/command/completion"
1718
"github.com/docker/cli/cli/command/formatter"
1819
flagsHelper "github.com/docker/cli/cli/flags"
1920
"github.com/moby/moby/api/types/events"
2021
"github.com/moby/moby/client"
21-
"github.com/sirupsen/logrus"
2222
"github.com/spf13/cobra"
2323
)
2424

@@ -139,24 +139,34 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
139139
eh := newEventHandler()
140140
if options.All {
141141
eh.setHandler(events.ActionCreate, func(e events.Message) {
142-
s := NewStats(e.Actor.ID)
143-
if cStats.add(s) {
142+
if s := NewStats(e.Actor.ID); cStats.add(s) {
144143
waitFirst.Add(1)
144+
log.G(ctx).WithFields(map[string]any{
145+
"event": e.Action,
146+
"container": e.Actor.ID,
147+
}).Debug("collecting stats for container")
145148
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
146149
}
147150
})
148151
}
149152

150153
eh.setHandler(events.ActionStart, func(e events.Message) {
151-
s := NewStats(e.Actor.ID)
152-
if cStats.add(s) {
154+
if s := NewStats(e.Actor.ID); cStats.add(s) {
153155
waitFirst.Add(1)
156+
log.G(ctx).WithFields(map[string]any{
157+
"event": e.Action,
158+
"container": e.Actor.ID,
159+
}).Debug("collecting stats for container")
154160
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
155161
}
156162
})
157163

158164
if !options.All {
159165
eh.setHandler(events.ActionDie, func(e events.Message) {
166+
log.G(ctx).WithFields(map[string]any{
167+
"event": e.Action,
168+
"container": e.Actor.ID,
169+
}).Debug("stop collecting stats for container")
160170
cStats.remove(e.Actor.ID)
161171
})
162172
}
@@ -209,9 +219,11 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
209219
return err
210220
}
211221
for _, ctr := range cs {
212-
s := NewStats(ctr.ID)
213-
if cStats.add(s) {
222+
if s := NewStats(ctr.ID); cStats.add(s) {
214223
waitFirst.Add(1)
224+
log.G(ctx).WithFields(map[string]any{
225+
"container": ctr.ID,
226+
}).Debug("collecting stats for container")
215227
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
216228
}
217229
}
@@ -230,9 +242,11 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
230242
// Create the list of containers, and start collecting stats for all
231243
// containers passed.
232244
for _, ctr := range options.Containers {
233-
s := NewStats(ctr)
234-
if cStats.add(s) {
245+
if s := NewStats(ctr); cStats.add(s) {
235246
waitFirst.Add(1)
247+
log.G(ctx).WithFields(map[string]any{
248+
"container": ctr,
249+
}).Debug("collecting stats for container")
236250
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
237251
}
238252
}
@@ -257,7 +271,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
257271
}
258272

259273
format := options.Format
260-
if len(format) == 0 {
274+
if format == "" {
261275
if len(dockerCLI.ConfigFile().StatsFormat) > 0 {
262276
format = dockerCLI.ConfigFile().StatsFormat
263277
} else {
@@ -357,11 +371,11 @@ func (eh *eventHandler) watch(c <-chan events.Message) {
357371
continue
358372
}
359373
if e.Actor.ID == "" {
360-
logrus.WithField("event", e).Errorf("event handler: received %s event with empty ID", e.Action)
374+
log.G(context.TODO()).WithField("event", e).Errorf("event handler: received %s event with empty ID", e.Action)
361375
continue
362376
}
363377

364-
logrus.WithField("event", e).Debugf("event handler: received %s event for: %s", e.Action, e.Actor.ID)
378+
log.G(context.TODO()).WithField("event", e).Debugf("event handler: received %s event for: %s", e.Action, e.Actor.ID)
365379
go h(e)
366380
}
367381
}

cli/command/container/stats_helpers.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/moby/moby/api/types/container"
1212
"github.com/moby/moby/client"
13-
"github.com/sirupsen/logrus"
1413
)
1514

1615
type stats struct {
@@ -51,7 +50,6 @@ func (s *stats) isKnownContainer(cid string) (int, bool) {
5150
}
5251

5352
func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, streamStats bool, waitFirst *sync.WaitGroup) {
54-
logrus.Debugf("collecting stats for %s", s.Container)
5553
var (
5654
getFirst bool
5755
previousCPU uint64

0 commit comments

Comments
 (0)