parent
7bfb42946e
commit
b44644b6bf
|
@ -3,6 +3,7 @@
|
||||||
### Features
|
### Features
|
||||||
- [#509](https://github.com/influxdb/telegraf/pull/509): Flatten JSON arrays with indices. Thanks @psilva261!
|
- [#509](https://github.com/influxdb/telegraf/pull/509): Flatten JSON arrays with indices. Thanks @psilva261!
|
||||||
- [#512](https://github.com/influxdata/telegraf/pull/512): Python 3 build script, add lsof dep to package. Thanks @Ormod!
|
- [#512](https://github.com/influxdata/telegraf/pull/512): Python 3 build script, add lsof dep to package. Thanks @Ormod!
|
||||||
|
- [#475](https://github.com/influxdata/telegraf/pull/475): Add response time to httpjson plugin. Thanks @titilambert!
|
||||||
|
|
||||||
### Bugfixes
|
### Bugfixes
|
||||||
- [#506](https://github.com/influxdb/telegraf/pull/506): Ping input doesn't return response time metric when timeout. Thanks @titilambert!
|
- [#506](https://github.com/influxdb/telegraf/pull/506): Ping input doesn't return response time metric when timeout. Thanks @titilambert!
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/influxdb/telegraf/internal"
|
"github.com/influxdb/telegraf/internal"
|
||||||
"github.com/influxdb/telegraf/plugins/inputs"
|
"github.com/influxdb/telegraf/plugins/inputs"
|
||||||
|
@ -119,7 +120,8 @@ func (h *HttpJson) gatherServer(
|
||||||
acc inputs.Accumulator,
|
acc inputs.Accumulator,
|
||||||
serverURL string,
|
serverURL string,
|
||||||
) error {
|
) error {
|
||||||
resp, err := h.sendRequest(serverURL)
|
resp, responseTime, err := h.sendRequest(serverURL)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -141,6 +143,9 @@ func (h *HttpJson) gatherServer(
|
||||||
delete(jsonOut, tag)
|
delete(jsonOut, tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if responseTime >= 0 {
|
||||||
|
jsonOut["response_time"] = responseTime
|
||||||
|
}
|
||||||
f := internal.JSONFlattener{}
|
f := internal.JSONFlattener{}
|
||||||
err = f.FlattenJSON("", jsonOut)
|
err = f.FlattenJSON("", jsonOut)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -164,11 +169,11 @@ func (h *HttpJson) gatherServer(
|
||||||
// Returns:
|
// Returns:
|
||||||
// string: body of the response
|
// string: body of the response
|
||||||
// error : Any error that may have occurred
|
// error : Any error that may have occurred
|
||||||
func (h *HttpJson) sendRequest(serverURL string) (string, error) {
|
func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
|
||||||
// Prepare URL
|
// Prepare URL
|
||||||
requestURL, err := url.Parse(serverURL)
|
requestURL, err := url.Parse(serverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Invalid server URL \"%s\"", serverURL)
|
return "", -1, fmt.Errorf("Invalid server URL \"%s\"", serverURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
|
@ -180,19 +185,21 @@ func (h *HttpJson) sendRequest(serverURL string) (string, error) {
|
||||||
// Create + send request
|
// Create + send request
|
||||||
req, err := http.NewRequest(h.Method, requestURL.String(), nil)
|
req, err := http.NewRequest(h.Method, requestURL.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
resp, err := h.client.MakeRequest(req)
|
resp, err := h.client.MakeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", -1, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
responseTime := time.Since(start).Seconds()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return string(body), err
|
return string(body), responseTime, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process response
|
// Process response
|
||||||
|
@ -203,10 +210,10 @@ func (h *HttpJson) sendRequest(serverURL string) (string, error) {
|
||||||
http.StatusText(resp.StatusCode),
|
http.StatusText(resp.StatusCode),
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
http.StatusText(http.StatusOK))
|
http.StatusText(http.StatusOK))
|
||||||
return string(body), err
|
return string(body), responseTime, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(body), err
|
return string(body), responseTime, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
const validJSON = `
|
const validJSON = `
|
||||||
{
|
{
|
||||||
"parent": {
|
"parent": {
|
||||||
"child": 3,
|
"child": 3.0,
|
||||||
"ignored_child": "hi"
|
"ignored_child": "hi"
|
||||||
},
|
},
|
||||||
"ignored_null": null,
|
"ignored_null": null,
|
||||||
|
@ -126,10 +126,16 @@ func TestHttpJson200(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := service.Gather(&acc)
|
err := service.Gather(&acc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 10, acc.NFields())
|
assert.Equal(t, 12, acc.NFields())
|
||||||
|
// Set responsetime
|
||||||
|
for _, p := range acc.Points {
|
||||||
|
p.Fields["response_time"] = 1.0
|
||||||
|
}
|
||||||
|
|
||||||
for _, srv := range service.Servers {
|
for _, srv := range service.Servers {
|
||||||
tags := map[string]string{"server": srv}
|
tags := map[string]string{"server": srv}
|
||||||
mname := "httpjson_" + service.Name
|
mname := "httpjson_" + service.Name
|
||||||
|
expectedFields["response_time"] = 1.0
|
||||||
acc.AssertContainsTaggedFields(t, mname, expectedFields, tags)
|
acc.AssertContainsTaggedFields(t, mname, expectedFields, tags)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,11 +194,15 @@ func TestHttpJson200Tags(t *testing.T) {
|
||||||
if service.Name == "other_webapp" {
|
if service.Name == "other_webapp" {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := service.Gather(&acc)
|
err := service.Gather(&acc)
|
||||||
|
// Set responsetime
|
||||||
|
for _, p := range acc.Points {
|
||||||
|
p.Fields["response_time"] = 1.0
|
||||||
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 2, acc.NFields())
|
assert.Equal(t, 4, acc.NFields())
|
||||||
for _, srv := range service.Servers {
|
for _, srv := range service.Servers {
|
||||||
tags := map[string]string{"server": srv, "role": "master", "build": "123"}
|
tags := map[string]string{"server": srv, "role": "master", "build": "123"}
|
||||||
fields := map[string]interface{}{"value": float64(15)}
|
fields := map[string]interface{}{"value": float64(15), "response_time": float64(1)}
|
||||||
mname := "httpjson_" + service.Name
|
mname := "httpjson_" + service.Name
|
||||||
acc.AssertContainsTaggedFields(t, mname, fields, tags)
|
acc.AssertContainsTaggedFields(t, mname, fields, tags)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue