Add configurable timeout to bind input plugin http call (#8508)
This commit is contained in:
parent
f7d94430d2
commit
7f3773e8e7
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue