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 c114849
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ based on _prefix_ in addition to globs. This means that a filter like
- [#1125](https://github.com/influxdata/telegraf/pull/1125): Wrap all exec command runners with a timeout, so hung os processes don't halt Telegraf.
- [#1113](https://github.com/influxdata/telegraf/pull/1113): Set MaxRetry and RequiredAcks defaults in Kafka output.
- [#1090](https://github.com/influxdata/telegraf/issues/1090): [agent] and [global_tags] config sometimes not getting applied.
- [#1133](https://github.com/influxdata/telegraf/issues/1133): Use a timeout for docker list & stat cmds.
- [#1052](https://github.com/influxdata/telegraf/issues/1052): Docker panic fix when decode fails.

## v0.12.1 [2016-04-14]

Expand Down
5 changes: 5 additions & 0 deletions etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@
# 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"


# # Read statistics from one or many dovecot servers
Expand Down Expand Up @@ -600,6 +602,9 @@
# ## Commands array
# commands = ["/tmp/test.sh", "/usr/bin/mycollector --foo=bar"]
#
# ## Timeout for each command to complete.
# timeout = "5s"
#
# ## measurement name suffix (for separating different commands)
# name_suffix = "_mycollector"
#
Expand Down
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 c114849

Please sign in to comment.