Skip to content

Commit

Permalink
Add response time to httpjson plugin
Browse files Browse the repository at this point in the history
closes #475
  • Loading branch information
titilambert authored and sparrc committed Jan 15, 2016
1 parent 7bfb429 commit c89c6dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
25 changes: 16 additions & 9 deletions plugins/inputs/httpjson/httpjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"strings"
"sync"
"time"

"github.com/influxdb/telegraf/internal"
"github.com/influxdb/telegraf/plugins/inputs"
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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{}
Expand All @@ -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
Expand All @@ -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() {
Expand Down
18 changes: 14 additions & 4 deletions plugins/inputs/httpjson/httpjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
const validJSON = `
{
"parent": {
"child": 3,
"child": 3.0,
"ignored_child": "hi"
},
"ignored_null": null,
Expand Down Expand Up @@ -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, 6, 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)
}
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit c89c6dd

Please sign in to comment.