Skip to content

Commit

Permalink
Use a timeout for docker list & stat cmds
Browse files Browse the repository at this point in the history
closes #1133
  • Loading branch information
sparrc committed May 1, 2016
1 parent 4e9798d commit e68e2e2
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)

// Docker object
type Docker struct {
Endpoint string
ContainerNames []string
Timeout internal.Duration

client DockerClient
}
Expand Down Expand Up @@ -54,6 +56,8 @@ var sampleConfig = `
endpoint = "unix:///var/run/docker.sock"
## Only collect metrics for these containers, collect all if empty
container_names = []
## Timeout for docker list, info, and stats commands
timeout = "5s"
`

// Description returns input description
Expand Down Expand Up @@ -97,7 +101,9 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error {

// List containers
opts := types.ContainerListOptions{}
containers, err := d.client.ContainerList(context.Background(), opts)
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()
containers, err := d.client.ContainerList(ctx, opts)
if err != nil {
return err
}
Expand All @@ -106,7 +112,6 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
wg.Add(len(containers))
for _, container := range containers {

go func(c types.Container) {
defer wg.Done()
err := d.gatherContainer(c, acc)
Expand All @@ -127,7 +132,9 @@ func (d *Docker) gatherInfo(acc telegraf.Accumulator) error {
metadataFields := make(map[string]interface{})
now := time.Now()
// Get info from docker daemon
info, err := d.client.Info(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()
info, err := d.client.Info(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -210,7 +217,9 @@ func (d *Docker) gatherContainer(
}
}

r, err := d.client.ContainerStats(context.Background(), container.ID, false)
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()
r, err := d.client.ContainerStats(ctx, container.ID, false)
if err != nil {
log.Printf("Error getting docker stats: %s\n", err.Error())
}
Expand Down

0 comments on commit e68e2e2

Please sign in to comment.