Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker: reset timer after collecting stats #24092

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions drivers/docker/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,33 @@ func (h *taskHandle) collectStats(ctx context.Context, destCh *usageSender, inte
timer, cancel := helper.NewSafeTimer(interval)
defer cancel()

collectOnce := func() {
defer timer.Reset(interval)
statsReader, err := h.dockerClient.ContainerStatsOneShot(ctx, h.containerID)
if err != nil && err != io.EOF {
h.logger.Debug("error collecting stats from container", "error", err)
return
}
defer statsReader.Body.Close()

var stats containerapi.Stats
if err := json.NewDecoder(statsReader.Body).Decode(&stats); err != nil {
h.logger.Error("error decoding stats data for container", "error", err)
return
}

resourceUsage := util.DockerStatsToTaskResourceUsage(&stats, compute)
destCh.send(resourceUsage)
}

for {
select {
case <-ctx.Done():
return
case <-h.doneCh:
return
case <-timer.C:
// ContainerStats returns a StatsResponseReader. Body of that reader
// contains the stats and implements io.Reader
statsReader, err := h.dockerClient.ContainerStatsOneShot(ctx, h.containerID)
if err != nil && err != io.EOF {
// An error occurred during stats collection, retry with backoff
h.logger.Debug("error collecting stats from container", "error", err)
continue
}

var stats containerapi.Stats

if err := json.NewDecoder(statsReader.Body).Decode(&stats); err != nil {
h.logger.Error("error unmarshalling stats data for container", "error", err)
_ = statsReader.Body.Close()
continue
}

resourceUsage := util.DockerStatsToTaskResourceUsage(&stats, compute)
destCh.send(resourceUsage)

_ = statsReader.Body.Close()
collectOnce()
}
}
}