Add configurable timeout to bind input plugin http call (#8508)

This commit is contained in:
Stephanie Engel 2020-12-03 13:42:50 -06:00 committed by GitHub
parent f7d94430d2
commit 7f3773e8e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 7 deletions

View File

@ -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". trailing slash in the URL. Default is "http://localhost:8053/xml/v3".
- **gather_memory_contexts** bool: Report per-context memory statistics. - **gather_memory_contexts** bool: Report per-context memory statistics.
- **gather_views** bool: Report per-view query 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 The following table summarizes the URL formats which should be used, depending on your BIND
version and configured statistics channel. version and configured statistics channel.

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
@ -15,6 +16,9 @@ type Bind struct {
Urls []string Urls []string
GatherMemoryContexts bool GatherMemoryContexts bool
GatherViews bool GatherViews bool
Timeout config.Duration `toml:"timeout"`
client http.Client
} }
var sampleConfig = ` var sampleConfig = `
@ -23,11 +27,10 @@ var sampleConfig = `
# urls = ["http://localhost:8053/xml/v3"] # urls = ["http://localhost:8053/xml/v3"]
# gather_memory_contexts = false # gather_memory_contexts = false
# gather_views = false # gather_views = false
`
var client = &http.Client{ ## Timeout for http requests made by bind nameserver
Timeout: time.Duration(4 * time.Second), # timeout = "4s"
} `
func (b *Bind) Description() string { func (b *Bind) Description() string {
return "Read BIND nameserver XML statistics" return "Read BIND nameserver XML statistics"
@ -37,6 +40,14 @@ func (b *Bind) SampleConfig() string {
return sampleConfig 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 { func (b *Bind) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup var wg sync.WaitGroup

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -20,6 +21,9 @@ func TestBindJsonStats(t *testing.T) {
Urls: []string{ts.URL + "/json/v1"}, Urls: []string{ts.URL + "/json/v1"},
GatherMemoryContexts: true, GatherMemoryContexts: true,
GatherViews: true, GatherViews: true,
client: http.Client{
Timeout: 4 * time.Second,
},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -190,6 +194,9 @@ func TestBindXmlStatsV2(t *testing.T) {
Urls: []string{ts.URL + "/xml/v2"}, Urls: []string{ts.URL + "/xml/v2"},
GatherMemoryContexts: true, GatherMemoryContexts: true,
GatherViews: true, GatherViews: true,
client: http.Client{
Timeout: 4 * time.Second,
},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
@ -392,6 +399,9 @@ func TestBindXmlStatsV3(t *testing.T) {
Urls: []string{ts.URL + "/xml/v3"}, Urls: []string{ts.URL + "/xml/v3"},
GatherMemoryContexts: true, GatherMemoryContexts: true,
GatherViews: true, GatherViews: true,
client: http.Client{
Timeout: 4 * time.Second,
},
} }
var acc testutil.Accumulator var acc testutil.Accumulator

View File

@ -155,7 +155,7 @@ func (b *Bind) readStatsJSON(addr *url.URL, acc telegraf.Accumulator) error {
for _, suffix := range [...]string{"/server", "/net", "/mem"} { for _, suffix := range [...]string{"/server", "/net", "/mem"} {
scrapeUrl := addr.String() + suffix scrapeUrl := addr.String() + suffix
resp, err := client.Get(scrapeUrl) resp, err := b.client.Get(scrapeUrl)
if err != nil { if err != nil {
return err return err
} }

View File

@ -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 { func (b *Bind) readStatsXMLv2(addr *url.URL, acc telegraf.Accumulator) error {
var stats v2Root var stats v2Root
resp, err := client.Get(addr.String()) resp, err := b.client.Get(addr.String())
if err != nil { if err != nil {
return err return err
} }

View File

@ -140,7 +140,7 @@ func (b *Bind) readStatsXMLv3(addr *url.URL, acc telegraf.Accumulator) error {
for _, suffix := range [...]string{"/server", "/net", "/mem"} { for _, suffix := range [...]string{"/server", "/net", "/mem"} {
scrapeUrl := addr.String() + suffix scrapeUrl := addr.String() + suffix
resp, err := client.Get(scrapeUrl) resp, err := b.client.Get(scrapeUrl)
if err != nil { if err != nil {
return err return err
} }