From b44644b6bf802fc7b497165320621e1dfdb067a0 Mon Sep 17 00:00:00 2001 From: Thibault Cohen Date: Thu, 7 Jan 2016 20:49:14 -0500 Subject: [PATCH] Add response time to httpjson plugin closes #475 --- CHANGELOG.md | 1 + plugins/inputs/httpjson/httpjson.go | 25 +++++++++++++++--------- plugins/inputs/httpjson/httpjson_test.go | 18 +++++++++++++---- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d93b38133..be5194726 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - [#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! +- [#475](https://github.com/influxdata/telegraf/pull/475): Add response time to httpjson plugin. Thanks @titilambert! ### Bugfixes - [#506](https://github.com/influxdb/telegraf/pull/506): Ping input doesn't return response time metric when timeout. Thanks @titilambert! diff --git a/plugins/inputs/httpjson/httpjson.go b/plugins/inputs/httpjson/httpjson.go index e31085e3a..5763fd6fa 100644 --- a/plugins/inputs/httpjson/httpjson.go +++ b/plugins/inputs/httpjson/httpjson.go @@ -9,6 +9,7 @@ import ( "net/url" "strings" "sync" + "time" "github.com/influxdb/telegraf/internal" "github.com/influxdb/telegraf/plugins/inputs" @@ -119,7 +120,8 @@ func (h *HttpJson) gatherServer( acc inputs.Accumulator, serverURL string, ) error { - resp, err := h.sendRequest(serverURL) + resp, responseTime, err := h.sendRequest(serverURL) + if err != nil { return err } @@ -141,6 +143,9 @@ func (h *HttpJson) gatherServer( delete(jsonOut, tag) } + if responseTime >= 0 { + jsonOut["response_time"] = responseTime + } f := internal.JSONFlattener{} err = f.FlattenJSON("", jsonOut) if err != nil { @@ -164,11 +169,11 @@ func (h *HttpJson) gatherServer( // Returns: // string: body of the response // 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 requestURL, err := url.Parse(serverURL) if err != nil { - return "", fmt.Errorf("Invalid server URL \"%s\"", serverURL) + return "", -1, fmt.Errorf("Invalid server URL \"%s\"", serverURL) } params := url.Values{} @@ -180,19 +185,21 @@ func (h *HttpJson) sendRequest(serverURL string) (string, error) { // Create + send request req, err := http.NewRequest(h.Method, requestURL.String(), nil) if err != nil { - return "", err + return "", -1, err } + start := time.Now() resp, err := h.client.MakeRequest(req) if err != nil { - return "", err + return "", -1, err } - defer resp.Body.Close() defer resp.Body.Close() + responseTime := time.Since(start).Seconds() + body, err := ioutil.ReadAll(resp.Body) if err != nil { - return string(body), err + return string(body), responseTime, err } // Process response @@ -203,10 +210,10 @@ func (h *HttpJson) sendRequest(serverURL string) (string, error) { http.StatusText(resp.StatusCode), 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() { diff --git a/plugins/inputs/httpjson/httpjson_test.go b/plugins/inputs/httpjson/httpjson_test.go index dbc818344..3f14290ff 100644 --- a/plugins/inputs/httpjson/httpjson_test.go +++ b/plugins/inputs/httpjson/httpjson_test.go @@ -14,7 +14,7 @@ import ( const validJSON = ` { "parent": { - "child": 3, + "child": 3.0, "ignored_child": "hi" }, "ignored_null": null, @@ -126,10 +126,16 @@ func TestHttpJson200(t *testing.T) { var acc testutil.Accumulator err := service.Gather(&acc) 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 { tags := map[string]string{"server": srv} mname := "httpjson_" + service.Name + expectedFields["response_time"] = 1.0 acc.AssertContainsTaggedFields(t, mname, expectedFields, tags) } } @@ -188,11 +194,15 @@ func TestHttpJson200Tags(t *testing.T) { if service.Name == "other_webapp" { var acc testutil.Accumulator err := service.Gather(&acc) + // Set responsetime + for _, p := range acc.Points { + p.Fields["response_time"] = 1.0 + } require.NoError(t, err) - assert.Equal(t, 2, acc.NFields()) + assert.Equal(t, 4, acc.NFields()) for _, srv := range service.Servers { 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 acc.AssertContainsTaggedFields(t, mname, fields, tags) }