Skip to content

Commit

Permalink
Add configurable timeout to influxdb input
Browse files Browse the repository at this point in the history
closes #1773
  • Loading branch information
sparrc committed Sep 16, 2016
1 parent 8d32855 commit 6648c10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
### Bugfixes

- [#1775](https://github.com/influxdata/telegraf/issues/1775): Prometheus output: Fix bug with multi-batch writes.
- [#1738](https://github.com/influxdata/telegraf/issues/1738): Fix unmarshal of influxdb metrics with null tags
- [#1738](https://github.com/influxdata/telegraf/issues/1738): Fix unmarshal of influxdb metrics with null tags.
- [#1773](https://github.com/influxdata/telegraf/issues/1773): Add configurable timeout to influxdb input plugin.

## v1.0 [2016-09-08]

Expand Down
33 changes: 22 additions & 11 deletions plugins/inputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import (
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)

type InfluxDB struct {
URLs []string `toml:"urls"`

Timeout internal.Duration

client *http.Client
}

func (*InfluxDB) Description() string {
Expand All @@ -32,13 +37,26 @@ func (*InfluxDB) SampleConfig() string {
urls = [
"http://localhost:8086/debug/vars"
]
## http request & header timeout
timeout = "5s"
`
}

func (i *InfluxDB) Gather(acc telegraf.Accumulator) error {
if len(i.URLs) == 0 {
i.URLs = []string{"http://localhost:8086/debug/vars"}
}

if i.client == nil {
i.client = &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: i.Timeout.Duration,
},
Timeout: i.Timeout.Duration,
}
}

errorChannel := make(chan error, len(i.URLs))

var wg sync.WaitGroup
Expand Down Expand Up @@ -104,15 +122,6 @@ type memstats struct {
GCCPUFraction float64 `json:"GCCPUFraction"`
}

var tr = &http.Transport{
ResponseHeaderTimeout: time.Duration(3 * time.Second),
}

var client = &http.Client{
Transport: tr,
Timeout: time.Duration(4 * time.Second),
}

// Gathers data from a particular URL
// Parameters:
// acc : The telegraf Accumulator to use
Expand All @@ -127,7 +136,7 @@ func (i *InfluxDB) gatherURL(
shardCounter := 0
now := time.Now()

resp, err := client.Get(url)
resp, err := i.client.Get(url)
if err != nil {
return err
}
Expand Down Expand Up @@ -248,6 +257,8 @@ func (i *InfluxDB) gatherURL(

func init() {
inputs.Add("influxdb", func() telegraf.Input {
return &InfluxDB{}
return &InfluxDB{
Timeout: internal.Duration{Duration: time.Second * 5},
}
})
}

0 comments on commit 6648c10

Please sign in to comment.