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

httpjson: support configurable response_timeout #1651

Merged
merged 4 commits into from
Aug 30, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 7 additions & 4 deletions plugins/inputs/httpjson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

The httpjson plugin can collect data from remote URLs which respond with JSON. Then it flattens JSON and finds all numeric values, treating them as floats.

For example, if you have a service called _mycollector_, which has HTTP endpoint for gathering stats at http://my.service.com/_stats, you would configure the HTTP JSON
plugin like this:
For example, if you have a service called _mycollector_, which has HTTP endpoint for gathering stats at http://my.service.com/_stats, you would configure the HTTP JSON plugin like this:

```
[[inputs.httpjson]]
Expand All @@ -15,12 +14,17 @@ plugin like this:

# HTTP method to use (case-sensitive)
method = "GET"

# Set response_timeout (default 5 seconds)
response_timeout = "5s"
```

`name` is used as a prefix for the measurements.

`method` specifies HTTP method to use for requests.

`response_timeout` specifies timeout to wait to get the response

You can also specify which keys from server response should be considered tags:

```
Expand Down Expand Up @@ -94,8 +98,7 @@ httpjson_mycollector_b_e,service='service01',server='http://my.service.com/_stat

# Example 2, Multiple Services:

There is also the option to collect JSON from multiple services, here is an
example doing that.
There is also the option to collect JSON from multiple services, here is an example doing that.

```
[[inputs.httpjson]]
Expand Down
24 changes: 16 additions & 8 deletions plugins/inputs/httpjson/httpjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"github.com/influxdata/telegraf/plugins/parsers"
)

// HttpJson struct
type HttpJson struct {
Name string
Servers []string
Method string
TagKeys []string
Parameters map[string]string
Headers map[string]string
Name string
Servers []string
Method string
TagKeys []string
ResponseTimeout internal.Duration
Parameters map[string]string
Headers map[string]string

// Path to CA file
SSLCA string `toml:"ssl_ca"`
Expand Down Expand Up @@ -79,6 +81,8 @@ var sampleConfig = `
"http://localhost:9999/stats/",
"http://localhost:9998/stats/",
]
## Set response_timeout (default 5 seconds)
response_timeout = "5s"

## HTTP method to use: GET or POST (case-sensitive)
method = "GET"
Expand Down Expand Up @@ -126,12 +130,12 @@ func (h *HttpJson) Gather(acc telegraf.Accumulator) error {
return err
}
tr := &http.Transport{
ResponseHeaderTimeout: time.Duration(3 * time.Second),
ResponseHeaderTimeout: h.ResponseTimeout.Duration,
TLSClientConfig: tlsCfg,
}
client := &http.Client{
Transport: tr,
Timeout: time.Duration(4 * time.Second),
Timeout: h.ResponseTimeout.Duration,
}
h.client.SetHTTPClient(client)
}
Expand Down Expand Up @@ -221,6 +225,10 @@ func (h *HttpJson) gatherServer(
// string: body of the response
// error : Any error that may have occurred
func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
// Set default values
if h.ResponseTimeout.Duration < time.Second {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be set in the init() function instead

h.ResponseTimeout.Duration = time.Second * 5
}
// Prepare URL
requestURL, err := url.Parse(serverURL)
if err != nil {
Expand Down