Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable timeout to bind input plugin http call #8508

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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