Skip to content

Commit

Permalink
Fetch container stats in parallel (#3127) (#3135)
Browse files Browse the repository at this point in the history
Currently fetching container stats is very slow as each request takes up to 2 seconds. To improve the fetching time if lots of containers are around, this creates the rrequests in parallel. The main downside is that this opens lots of connections. This fix should only temporary until the bulk api is available: moby/moby#25361
(cherry picked from commit 78f547f)
  • Loading branch information
ruflin authored and tsg committed Dec 7, 2016
1 parent 6d53ca1 commit 83aee76
Showing 1 changed file with 22 additions and 4 deletions.
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

0 comments on commit 83aee76

Please sign in to comment.