Skip to content

Commit

Permalink
Add configurable timeout to bind input plugin http call (#8508)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanie-engel authored Dec 3, 2020
1 parent f7d9443 commit 7f3773e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions plugins/inputs/bind/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ not enable support for JSON statistics in their BIND packages.
trailing slash in the URL. Default is "http://localhost:8053/xml/v3".
- **gather_memory_contexts** bool: Report per-context memory statistics.
- **gather_views** bool: Report per-view query statistics.
- **timeout** Timeout for http requests made by bind nameserver (example: "4s").

The following table summarizes the URL formats which should be used, depending on your BIND
version and configured statistics channel.
Expand Down
19 changes: 15 additions & 4 deletions plugins/inputs/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
"time"

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

type Bind struct {
Urls []string
GatherMemoryContexts bool
GatherViews bool
Timeout config.Duration `toml:"timeout"`

client http.Client
}

var sampleConfig = `
Expand All @@ -23,11 +27,10 @@ var sampleConfig = `
# urls = ["http://localhost:8053/xml/v3"]
# gather_memory_contexts = false
# gather_views = false
`
var client = &http.Client{
Timeout: time.Duration(4 * time.Second),
}
## Timeout for http requests made by bind nameserver
# timeout = "4s"
`

func (b *Bind) Description() string {
return "Read BIND nameserver XML statistics"
Expand All @@ -37,6 +40,14 @@ func (b *Bind) SampleConfig() string {
return sampleConfig
}

func (b *Bind) Init() error {
b.client = http.Client{
Timeout: time.Duration(b.Timeout),
}

return nil
}

func (b *Bind) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup

Expand Down
10 changes: 10 additions & 0 deletions plugins/inputs/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
Expand All @@ -20,6 +21,9 @@ func TestBindJsonStats(t *testing.T) {
Urls: []string{ts.URL + "/json/v1"},
GatherMemoryContexts: true,
GatherViews: true,
client: http.Client{
Timeout: 4 * time.Second,
},
}

var acc testutil.Accumulator
Expand Down Expand Up @@ -190,6 +194,9 @@ func TestBindXmlStatsV2(t *testing.T) {
Urls: []string{ts.URL + "/xml/v2"},
GatherMemoryContexts: true,
GatherViews: true,
client: http.Client{
Timeout: 4 * time.Second,
},
}

var acc testutil.Accumulator
Expand Down Expand Up @@ -392,6 +399,9 @@ func TestBindXmlStatsV3(t *testing.T) {
Urls: []string{ts.URL + "/xml/v3"},
GatherMemoryContexts: true,
GatherViews: true,
client: http.Client{
Timeout: 4 * time.Second,
},
}

var acc testutil.Accumulator
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bind/json_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (b *Bind) readStatsJSON(addr *url.URL, acc telegraf.Accumulator) error {
for _, suffix := range [...]string{"/server", "/net", "/mem"} {
scrapeUrl := addr.String() + suffix

resp, err := client.Get(scrapeUrl)
resp, err := b.client.Get(scrapeUrl)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bind/xml_stats_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func addXMLv2Counter(acc telegraf.Accumulator, commonTags map[string]string, sta
func (b *Bind) readStatsXMLv2(addr *url.URL, acc telegraf.Accumulator) error {
var stats v2Root

resp, err := client.Get(addr.String())
resp, err := b.client.Get(addr.String())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/bind/xml_stats_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (b *Bind) readStatsXMLv3(addr *url.URL, acc telegraf.Accumulator) error {
for _, suffix := range [...]string{"/server", "/net", "/mem"} {
scrapeUrl := addr.String() + suffix

resp, err := client.Get(scrapeUrl)
resp, err := b.client.Get(scrapeUrl)
if err != nil {
return err
}
Expand Down

0 comments on commit 7f3773e

Please sign in to comment.