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

Add response time metric to httpjson plugin #475

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## v0.3.0 [unreleased]
## v0.10.0 [unreleased]

### Release Notes
- Linux packages have been taken out of `opt`, the binary is now in `/usr/bin`
Expand All @@ -23,7 +23,7 @@ aggregated).

### Packaging change note:

RHEL/CentOS users upgrading from 0.2 to 0.3 will probably have their
RHEL/CentOS users upgrading from 0.2.x to 0.10.0 will probably have their
configurations overwritten by the upgrade. There is a backup stored at
/etc/telegraf/telegraf.conf.$(date +%s).backup.

Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ new plugins.

## Installation:

NOTE: Telegraf 0.3.x is **not** backwards-compatible with previous versions of
NOTE: Telegraf 0.10.x is **not** backwards-compatible with previous versions of
telegraf, both in the database layout and the configuration file. 0.2.x will
continue to be supported, see below for download links.

TODO: link to blog post about 0.3.x changes.
TODO: link to blog post about 0.10.x changes.

### Linux deb and rpm packages:

Latest:
* http://get.influxdb.org/telegraf/telegraf_0.3.0_amd64.deb
* http://get.influxdb.org/telegraf/telegraf-0.3.0-1.x86_64.rpm
* http://get.influxdb.org/telegraf/telegraf_0.10.0_amd64.deb
* http://get.influxdb.org/telegraf/telegraf-0.10.0-1.x86_64.rpm

0.2.x:
* http://get.influxdb.org/telegraf/telegraf_0.2.4_amd64.deb
Expand All @@ -45,9 +45,9 @@ controlled via `systemctl [action] telegraf`
### Linux binaries:

Latest:
* http://get.influxdb.org/telegraf/telegraf_linux_amd64_0.3.0.tar.gz
* http://get.influxdb.org/telegraf/telegraf_linux_386_0.3.0.tar.gz
* http://get.influxdb.org/telegraf/telegraf_linux_arm_0.3.0.tar.gz
* http://get.influxdb.org/telegraf/telegraf_linux_amd64_0.10.0.tar.gz
* http://get.influxdb.org/telegraf/telegraf_linux_386_0.10.0.tar.gz
* http://get.influxdb.org/telegraf/telegraf_linux_arm_0.10.0.tar.gz

0.2.x:
* http://get.influxdb.org/telegraf/telegraf_linux_amd64_0.2.4.tar.gz
Expand Down
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 @@ -123,10 +123,16 @@ func TestHttpJson200(t *testing.T) {
var acc testutil.Accumulator
err := service.Gather(&acc)
require.NoError(t, err)
assert.Equal(t, 4, 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 @@ -185,11 +191,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