Skip to content

Commit

Permalink
Initialize client only once
Browse files Browse the repository at this point in the history
  • Loading branch information
poblahblahblah committed Sep 14, 2017
1 parent f965bca commit 767fd53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
3 changes: 1 addition & 2 deletions plugins/inputs/nginx_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Structures for Nginx Plus have been built based on history of
- nginx_plus_processes, nginx_plus_connections, nginx_plus_ssl, nginx_plus_requests
- server
- port
- host

- nginx_plus_upstream, nginx_plus_stream_upstream
- host
Expand All @@ -79,7 +78,7 @@ Structures for Nginx Plus have been built based on history of
- upstream
- server
- port
- serverAddress
- upstream_address

### Example Output:

Expand Down
27 changes: 22 additions & 5 deletions plugins/inputs/nginx_plus/nginx_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ func (n *NginxPlus) Description() string {
func (n *NginxPlus) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup

// Create an HTTP client that is re-used for each
// collection interval

if n.client == nil {
client, err := n.createHttpClient()
if err != nil {
return err
}
n.client = client
}

for _, u := range n.Urls {
addr, err := url.Parse(u)
if err != nil {
Expand All @@ -61,16 +72,22 @@ func (n *NginxPlus) Gather(acc telegraf.Accumulator) error {
return nil
}

func (n *NginxPlus) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
func (n *NginxPlus) createHttpClient() (*http.Client, error) {

if n.ResponseTimeout.Duration < time.Second {
n.ResponseTimeout.Duration = time.Second * 5
}

client := &http.Client{
Timeout: n.ResponseTimeout.Duration,
Transport: &http.Transport{},
Timeout: n.ResponseTimeout.Duration,
}

resp, err := client.Get(addr.String())
return client, nil
}

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

if err != nil {
return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err)
Expand Down Expand Up @@ -412,7 +429,7 @@ func (s *Status) gatherUpstreamMetrics(tags map[string]string, acc telegraf.Accu
for k, v := range upstreamTags {
peerTags[k] = v
}
peerTags["serverAddress"] = peer.Server
peerTags["upstream_address"] = peer.Server
if peer.ID != nil {
peerTags["id"] = strconv.Itoa(*peer.ID)
}
Expand Down Expand Up @@ -525,7 +542,7 @@ func (s *Status) gatherStreamMetrics(tags map[string]string, acc telegraf.Accumu
for k, v := range upstreamTags {
peerTags[k] = v
}
peerTags["serverAddress"] = peer.Server
peerTags["upstream_address"] = peer.Server
peerTags["id"] = strconv.Itoa(peer.ID)
acc.AddFields("nginx_plus_stream_upstream_peer", peerFields, peerTags)
}
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/nginx_plus/nginx_plus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ func TestNginxPlusGeneratesMetrics(t *testing.T) {
}(),
},
map[string]string{
"server": host,
"port": port,
"upstream": "first_upstream",
"serverAddress": "1.2.3.123:80",
"id": "0",
"server": host,
"port": port,
"upstream": "first_upstream",
"upstream_address": "1.2.3.123:80",
"id": "0",
})

}

0 comments on commit 767fd53

Please sign in to comment.