@@ -16,6 +16,7 @@ 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"
|
||||
)
|
||||
|
||||
@@ -23,6 +24,7 @@ import (
|
||||
type Docker struct {
|
||||
Endpoint string
|
||||
ContainerNames []string
|
||||
Timeout internal.Duration
|
||||
|
||||
client DockerClient
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
@@ -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
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user