Skip to content

Commit 434193f

Browse files
authored
Merge pull request #6865 from thaJeztah/cleanup_stats
cli/command/container: RunStats: pass ctx to stats event handlers and refractor to DRY
2 parents adc5466 + 3f51d0a commit 434193f

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

cli/command/container/stats.go

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -144,36 +144,21 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
144144
}
145145

146146
eh := newEventHandler()
147+
addEvents := []events.Action{events.ActionStart}
147148
if options.All {
148-
eh.setHandler(events.ActionCreate, func(e events.Message) {
149-
if s := NewStats(e.Actor.ID); cStats.add(s) {
150-
waitFirst.Add(1)
151-
log.G(ctx).WithFields(log.Fields{
152-
"event": e.Action,
153-
"container": e.Actor.ID,
154-
}).Debug("collecting stats for container")
155-
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
156-
}
157-
})
149+
addEvents = append(addEvents, events.ActionCreate)
158150
}
159-
160-
eh.setHandler(events.ActionStart, func(e events.Message) {
151+
eh.setHandler(addEvents, func(ctx context.Context, e events.Message) {
161152
if s := NewStats(e.Actor.ID); cStats.add(s) {
162153
waitFirst.Add(1)
163-
log.G(ctx).WithFields(log.Fields{
164-
"event": e.Action,
165-
"container": e.Actor.ID,
166-
}).Debug("collecting stats for container")
154+
log.G(ctx).Debug("collecting stats for container")
167155
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
168156
}
169157
})
170158

171159
if !options.All {
172-
eh.setHandler(events.ActionDie, func(e events.Message) {
173-
log.G(ctx).WithFields(log.Fields{
174-
"event": e.Action,
175-
"container": e.Actor.ID,
176-
}).Debug("stop collecting stats for container")
160+
eh.setHandler([]events.Action{events.ActionDie}, func(ctx context.Context, e events.Message) {
161+
log.G(ctx).Debug("stop collecting stats for container")
177162
cStats.remove(e.Actor.ID)
178163
})
179164
}
@@ -216,7 +201,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
216201
}
217202

218203
eventChan := make(chan events.Message)
219-
go eh.watch(eventChan)
204+
go eh.watch(ctx, eventChan)
220205
stopped := make(chan struct{})
221206
go monitorContainerEvents(started, eventChan, stopped)
222207
defer close(stopped)
@@ -369,33 +354,38 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
369354

370355
// newEventHandler initializes and returns an eventHandler
371356
func newEventHandler() *eventHandler {
372-
return &eventHandler{handlers: make(map[events.Action]func(events.Message))}
357+
return &eventHandler{handlers: make(map[events.Action]func(context.Context, events.Message))}
373358
}
374359

375360
// eventHandler allows for registering specific events to setHandler.
376361
type eventHandler struct {
377-
handlers map[events.Action]func(events.Message)
362+
handlers map[events.Action]func(context.Context, events.Message)
378363
}
379364

380-
func (eh *eventHandler) setHandler(action events.Action, handler func(events.Message)) {
381-
eh.handlers[action] = handler
365+
func (eh *eventHandler) setHandler(actions []events.Action, handler func(context.Context, events.Message)) {
366+
for _, action := range actions {
367+
eh.handlers[action] = handler
368+
}
382369
}
383370

384371
// watch ranges over the passed in event chan and processes the events based on the
385372
// handlers created for a given action.
386373
// To stop watching, close the event chan.
387-
func (eh *eventHandler) watch(c <-chan events.Message) {
374+
func (eh *eventHandler) watch(ctx context.Context, c <-chan events.Message) {
388375
for e := range c {
389376
h, exists := eh.handlers[e.Action]
390377
if !exists {
391378
continue
392379
}
393380
if e.Actor.ID == "" {
394-
log.G(context.TODO()).WithField("event", e).Errorf("event handler: received %s event with empty ID", e.Action)
381+
log.G(ctx).WithField("event", e).Errorf("event handler: received %s event with empty ID", e.Action)
395382
continue
396383
}
384+
logger := log.G(ctx).WithFields(log.Fields{
385+
"event": e.Action,
386+
"container": e.Actor.ID,
387+
})
397388

398-
log.G(context.TODO()).WithField("event", e).Debugf("event handler: received %s event for: %s", e.Action, e.Actor.ID)
399-
go h(e)
389+
go h(log.WithLogger(ctx, logger), e)
400390
}
401391
}

0 commit comments

Comments
 (0)