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 16, 2017
1 parent 1512d76 commit 82fc8c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 8 additions & 2 deletions plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,21 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
// Start Timer
start := time.Now()
resp, err := h.client.Do(request)
fields["response_time"] = time.Since(start).Seconds()

if err != nil {
fields["http_response_code"] = -1
fields["response_string_match"] = 0
if h.FollowRedirects {
return nil, err
log.Printf("E! Failed to open HTTP connection: %s", err)
return fields, nil
}
if urlError, ok := err.(*url.Error); ok &&
urlError.Err == ErrRedirectAttempted {
err = nil
} else {
return nil, err
log.Printf("E! Failed to open HTTP connection: %s", err)
return fields, nil
}
}
defer func() {
Expand Down
12 changes: 9 additions & 3 deletions plugins/inputs/http_response/http_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,14 @@ 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")
require.False(t, ok)
value, ok := acc.IntField("http_response", "http_response_code")
require.True(t, ok)
require.Equal(t, -1, value)
value, ok = acc.IntField("http_response", "response_string_match")
require.True(t, ok)
require.Equal(t, 0, value)
_, ok = acc.FloatField("http_response", "response_time")
require.True(t, ok)
}

0 comments on commit 82fc8c8

Please sign in to comment.