From 21f0085d7f6962f4576995a514b63841add30041 Mon Sep 17 00:00:00 2001 From: Stephanie Engel Date: Thu, 3 Dec 2020 12:40:41 -0600 Subject: [PATCH] Add configurable timeout to bind input plugin http call --- plugins/inputs/bind/README.md | 1 + plugins/inputs/bind/bind.go | 19 +++++++++++++++---- plugins/inputs/bind/bind_test.go | 10 ++++++++++ plugins/inputs/bind/json_stats.go | 2 +- plugins/inputs/bind/xml_stats_v2.go | 2 +- plugins/inputs/bind/xml_stats_v3.go | 2 +- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/plugins/inputs/bind/README.md b/plugins/inputs/bind/README.md index e3bcf6a75b252..d67a02020f527 100644 --- a/plugins/inputs/bind/README.md +++ b/plugins/inputs/bind/README.md @@ -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. diff --git a/plugins/inputs/bind/bind.go b/plugins/inputs/bind/bind.go index 967c9031a2634..e27fdfc38ec71 100644 --- a/plugins/inputs/bind/bind.go +++ b/plugins/inputs/bind/bind.go @@ -8,6 +8,7 @@ import ( "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/plugins/inputs" ) @@ -15,6 +16,9 @@ type Bind struct { Urls []string GatherMemoryContexts bool GatherViews bool + Timeout config.Duration `toml:"timeout"` + + client http.Client } var sampleConfig = ` @@ -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" @@ -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 diff --git a/plugins/inputs/bind/bind_test.go b/plugins/inputs/bind/bind_test.go index 6ed953b691dd3..7ca79c1ef19a4 100644 --- a/plugins/inputs/bind/bind_test.go +++ b/plugins/inputs/bind/bind_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/assert" @@ -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 @@ -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 @@ -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 diff --git a/plugins/inputs/bind/json_stats.go b/plugins/inputs/bind/json_stats.go index 87b6065e2eb1c..906ab21d97a69 100644 --- a/plugins/inputs/bind/json_stats.go +++ b/plugins/inputs/bind/json_stats.go @@ -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 } diff --git a/plugins/inputs/bind/xml_stats_v2.go b/plugins/inputs/bind/xml_stats_v2.go index 5e17851fb671c..ce7116a199d9e 100644 --- a/plugins/inputs/bind/xml_stats_v2.go +++ b/plugins/inputs/bind/xml_stats_v2.go @@ -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 } diff --git a/plugins/inputs/bind/xml_stats_v3.go b/plugins/inputs/bind/xml_stats_v3.go index 89e4ea0b8fcb6..7d36e000b9d95 100644 --- a/plugins/inputs/bind/xml_stats_v3.go +++ b/plugins/inputs/bind/xml_stats_v3.go @@ -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 }