Skip to content

Commit

Permalink
http_response: Measure data in case of timeout or dns error
Browse files Browse the repository at this point in the history
  • Loading branch information
froth committed May 24, 2017
1 parent 7155e90 commit a43a168
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions plugins/inputs/http_response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ This input plugin will test HTTP/HTTPS connections.
- http_response
- response_time (float, seconds)
- http_response_code (int) #The code received
- connection_failed (int) # 1 when no http connection could be established
- response_timeout (int) # 1 when response timeouts
- response_string_match (int) # 1 when response matches "response_string_match" configuration

### Tags:

Expand Down
18 changes: 13 additions & 5 deletions plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -130,15 +131,20 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
// Start Timer
start := time.Now()
resp, err := h.client.Do(request)

if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
fields["response_timeout"] = time.Since(start).Seconds()
}
fields["connection_failed"] = 1
if h.FollowRedirects {
return nil, err
return fields, nil
}
if urlError, ok := err.(*url.Error); ok &&
urlError.Err == ErrRedirectAttempted {
err = nil
} else {
return nil, err
return fields, nil
}
}
defer func() {
Expand All @@ -148,6 +154,7 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {

fields["response_time"] = time.Since(start).Seconds()
fields["http_response_code"] = resp.StatusCode
fields["connection_failed"] = 0

// Check the response for a regex match.
if h.ResponseStringMatch != "" {
Expand Down Expand Up @@ -215,10 +222,11 @@ func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
// Gather data
fields, err = h.httpGather()
if err != nil {
return err
acc.AddError(err)
} else {
// Add metrics
acc.AddFields("http_response", fields, tags)
}
// Add metrics
acc.AddFields("http_response", fields, tags)
return nil
}

Expand Down
21 changes: 18 additions & 3 deletions plugins/inputs/http_response/http_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func TestFields(t *testing.T) {
value, ok := acc.IntField("http_response", "http_response_code")
require.True(t, ok)
require.Equal(t, http.StatusOK, value)
value, ok = acc.IntField("http_response", "connection_failed")
require.True(t, ok)
require.Equal(t, 0, value)
}

func TestRedirects(t *testing.T) {
Expand Down Expand Up @@ -143,10 +146,15 @@ func TestRedirects(t *testing.T) {
}
acc = testutil.Accumulator{}
err = h.Gather(&acc)
require.Error(t, err)
require.NoError(t, err)

value, ok = acc.IntField("http_response", "http_response_code")
require.False(t, ok)
value, ok = acc.IntField("http_response", "response_string_match")
require.False(t, ok)
value, ok = acc.IntField("http_response", "connection_failed")
require.True(t, ok)
require.Equal(t, 1, value)
}

func TestMethod(t *testing.T) {
Expand Down Expand Up @@ -363,8 +371,15 @@ func TestTimeout(t *testing.T) {
}
var acc testutil.Accumulator
err := h.Gather(&acc)
require.Error(t, err)
require.NoError(t, err)

ok := acc.HasIntField("http_response", "http_response_code")
value, ok := acc.IntField("http_response", "http_response_code")
require.False(t, ok)
_, ok = acc.FloatField("http_response", "response_timeout")
require.True(t, ok)
value, ok = acc.IntField("http_response", "connection_failed")
require.True(t, ok)
require.Equal(t, 1, value)
_, ok = acc.FloatField("http_response", "response_time")
require.False(t, ok)
}

0 comments on commit a43a168

Please sign in to comment.