Skip to content

Commit

Permalink
Moving Client to NginxPlus Struct; Adding timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
poblahblahblah committed Sep 13, 2017
1 parent f3d0931 commit f965bca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
8 changes: 5 additions & 3 deletions plugins/inputs/nginx_plus/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Telegraf Plugin: nginx_plus

Nginx Plus is a commercial version of the open source web server Nginx. The use this plugin you will need a license. For more information about the differences between Nginx (F/OSS) and Nginx Plus, [click here](https://www.nginx.com/blog/whats-difference-nginx-foss-nginx-plus/).

Structures for Nginx Plus have been built based on history of
[status module documentation](http://nginx.org/en/docs/http/ngx_http_status_module.html)

### Configuration:

```
Expand Down Expand Up @@ -103,9 +108,6 @@ It produces:

### Reference material

Structures for Nginx Plus have been built based on history of
[status module documentation](http://nginx.org/en/docs/http/ngx_http_status_module.html)

Subsequent versions of status response structure available here:

- [version 1](http://web.archive.org/web/20130805111222/http://nginx.org/en/docs/http/ngx_http_status_module.html)
Expand Down
24 changes: 16 additions & 8 deletions plugins/inputs/nginx_plus/nginx_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ import (
"time"

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

type NginxPlus struct {
Urls []string

client *http.Client

ResponseTimeout internal.Duration
}

var sampleConfig = `
## An array of ngx_http_status_module or status URI to gather stats.
urls = ["http://localhost/status"]
# HTTP response timeout (default: 5s)
response_timeout = "5s"
`

func (n *NginxPlus) SampleConfig() string {
Expand Down Expand Up @@ -53,17 +61,17 @@ func (n *NginxPlus) Gather(acc telegraf.Accumulator) error {
return nil
}

var tr = &http.Transport{
ResponseHeaderTimeout: time.Duration(3 * time.Second),
}
func (n *NginxPlus) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
if n.ResponseTimeout.Duration < time.Second {
n.ResponseTimeout.Duration = time.Second * 5
}

var client = &http.Client{
Transport: tr,
Timeout: time.Duration(4 * time.Second),
}
client := &http.Client{
Timeout: n.ResponseTimeout.Duration,
}

func (n *NginxPlus) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
resp, err := client.Get(addr.String())

if err != nil {
return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err)
}
Expand Down

0 comments on commit f965bca

Please sign in to comment.