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

Fetch container stats in parallel #3127

Merged
merged 1 commit into from
Dec 6, 2016
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
26 changes: 22 additions & 4 deletions metricbeat/module/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,35 @@ func FetchStats(client *docker.Client) ([]Stat, error) {
return nil, err
}

var wg sync.WaitGroup

containersList := []Stat{}
queue := make(chan Stat, 1)
wg.Add(len(containers))

for _, container := range containers {
// This is currently very inefficient as docker calculates the average for each request,
// means each request will take at least 2s: https://github.com/docker/docker/blob/master/cli/command/container/stats_helpers.go#L148
// Getting all stats at once is implemented here: https://github.com/docker/docker/pull/25361
containersList = append(containersList, exportContainerStats(client, &container))
go func(container docker.APIContainers) {
queue <- exportContainerStats(client, &container)
}(container)
}

go func() {
for container := range queue {
containersList = append(containersList, container)
wg.Done()
}
}()

wg.Wait()

return containersList, err
}

// exportContainerStats loads stats for the given container
//
// This is currently very inefficient as docker calculates the average for each request,
// means each request will take at least 2s: https://github.com/docker/docker/blob/master/cli/command/container/stats_helpers.go#L148
// Getting all stats at once is implemented here: https://github.com/docker/docker/pull/25361
func exportContainerStats(client *docker.Client, container *docker.APIContainers) Stat {
var wg sync.WaitGroup
var event Stat
Expand Down