2015-10-17 11:38:50 +00:00
# HTTP JSON Plugin
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.
2016-08-30 15:23:15 +00:00
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:
2015-10-17 11:38:50 +00:00
```
2016-04-01 02:37:04 +00:00
[[inputs.httpjson]]
2015-10-17 11:38:50 +00:00
name = "mycollector"
servers = [
"http://my.service.com/_stats"
]
# HTTP method to use (case-sensitive)
method = "GET"
2016-08-30 15:23:15 +00:00
# Set response_timeout (default 5 seconds)
response_timeout = "5s"
2015-10-17 11:38:50 +00:00
```
2015-11-02 23:19:37 +00:00
`name` is used as a prefix for the measurements.
2015-10-17 11:38:50 +00:00
2015-11-02 23:19:37 +00:00
`method` specifies HTTP method to use for requests.
2015-10-17 11:38:50 +00:00
2016-08-30 15:23:15 +00:00
`response_timeout` specifies timeout to wait to get the response
2015-11-02 23:19:37 +00:00
You can also specify which keys from server response should be considered tags:
2015-10-17 11:38:50 +00:00
```
2016-04-01 02:37:04 +00:00
[[inputs.httpjson]]
2015-10-17 11:38:50 +00:00
...
tag_keys = [
"role",
"version"
]
```
2016-11-15 18:02:55 +00:00
If the JSON response is an array of objects, then each object will be parsed with the same configuration.
2015-10-17 11:38:50 +00:00
You can also specify additional request parameters for the service:
```
2016-04-01 02:37:04 +00:00
[[inputs.httpjson]]
2015-10-17 11:38:50 +00:00
...
2016-04-01 02:37:04 +00:00
[inputs.httpjson.parameters]
2015-10-17 11:38:50 +00:00
event_type = "cpu_spike"
threshold = "0.75"
```
2016-01-26 13:18:22 +00:00
You can also specify additional request header parameters for the service:
```
2016-04-01 02:37:04 +00:00
[[inputs.httpjson]]
2016-01-26 13:18:22 +00:00
...
2016-04-01 02:37:04 +00:00
[inputs.httpjson.headers]
2016-01-26 13:18:22 +00:00
X-Auth-Token = "my-xauth-token"
apiVersion = "v1"
```
2015-10-17 11:38:50 +00:00
2015-11-02 23:19:37 +00:00
# Example:
Let's say that we have a service named "mycollector" configured like this:
```
2016-04-01 02:37:04 +00:00
[[inputs.httpjson]]
name = "mycollector"
servers = [
"http://my.service.com/_stats"
]
# HTTP method to use (case-sensitive)
method = "GET"
tag_keys = ["service"]
2015-11-02 23:19:37 +00:00
```
which responds with the following JSON:
```json
{
"service": "service01",
"a": 0.5,
"b": {
"c": "some text",
"d": 0.1,
"e": 5
}
}
```
The collected metrics will be:
```
httpjson_mycollector_a,service='service01',server='http://my.service.com/_stats' value=0.5
httpjson_mycollector_b_d,service='service01',server='http://my.service.com/_stats' value=0.1
httpjson_mycollector_b_e,service='service01',server='http://my.service.com/_stats' value=5
```
# Example 2, Multiple Services:
2016-08-30 15:23:15 +00:00
There is also the option to collect JSON from multiple services, here is an example doing that.
2015-11-02 23:19:37 +00:00
```
2016-04-01 02:37:04 +00:00
[[inputs.httpjson]]
name = "mycollector1"
servers = [
"http://my.service1.com/_stats"
]
# HTTP method to use (case-sensitive)
method = "GET"
2015-10-17 11:38:50 +00:00
2016-04-01 02:37:04 +00:00
[[inputs.httpjson]]
name = "mycollector2"
servers = [
"http://service.net/json/stats"
]
# HTTP method to use (case-sensitive)
method = "POST"
2015-11-02 23:19:37 +00:00
```
The services respond with the following JSON:
mycollector1:
2015-10-17 11:38:50 +00:00
```json
{
"a": 0.5,
"b": {
"c": "some text",
"d": 0.1,
"e": 5
}
}
```
2015-11-02 23:19:37 +00:00
mycollector2:
```json
{
"load": 100,
"users": 1335
}
```
2015-10-17 11:38:50 +00:00
The collected metrics will be:
2015-11-02 23:19:37 +00:00
2015-10-17 11:38:50 +00:00
```
2015-11-02 23:19:37 +00:00
httpjson_mycollector1_a,server='http://my.service.com/_stats' value=0.5
httpjson_mycollector1_b_d,server='http://my.service.com/_stats' value=0.1
httpjson_mycollector1_b_e,server='http://my.service.com/_stats' value=5
httpjson_mycollector2_load,server='http://service.net/json/stats' value=100
httpjson_mycollector2_users,server='http://service.net/json/stats' value=1335
2015-10-17 11:38:50 +00:00
```
2016-11-15 18:02:55 +00:00
# Example 3, Multiple Metrics in Response:
The response JSON can be treated as an array of data points that are all parsed with the same configuration.
```
[[inputs.httpjson]]
name = "mycollector"
servers = [
"http://my.service.com/_stats"
]
# HTTP method to use (case-sensitive)
method = "GET"
tag_keys = ["service"]
```
which responds with the following JSON:
```json
[
{
"service": "service01",
"a": 0.5,
"b": {
"c": "some text",
"d": 0.1,
"e": 5
}
},
{
"service": "service02",
"a": 0.6,
"b": {
"c": "some text",
"d": 0.2,
"e": 6
}
}
]
```
The collected metrics will be:
```
httpjson_mycollector_a,service='service01',server='http://my.service.com/_stats' value=0.5
httpjson_mycollector_b_d,service='service01',server='http://my.service.com/_stats' value=0.1
httpjson_mycollector_b_e,service='service01',server='http://my.service.com/_stats' value=5
httpjson_mycollector_a,service='service02',server='http://my.service.com/_stats' value=0.6
httpjson_mycollector_b_d,service='service02',server='http://my.service.com/_stats' value=0.2
httpjson_mycollector_b_e,service='service02',server='http://my.service.com/_stats' value=6
```