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

Http response fixes #3814

Merged
merged 25 commits into from
Mar 8, 2018
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cf87092
Http_response input: Reorder, simplify and comment error handling code
Feb 18, 2018
a0f52b0
http_response input: fix typo in comment
Feb 18, 2018
0f23ab0
http_response input: Add options to log the response time even in the…
Feb 18, 2018
a9c8e72
http_response input: Add tags containing the http code and a field wi…
Feb 21, 2018
be3520d
http_response input: Remove feature to add a fixed latency in case of…
Feb 21, 2018
9c44462
http_response input: Fix confusing error handling when the body regex…
Feb 21, 2018
6436ff0
http_response input: Remove flags
Feb 21, 2018
de49945
http_response input: Factor out network error processing
Feb 21, 2018
319b66f
http_response input: Add handling error types other than timeout
Feb 21, 2018
a3722e7
http_response input: Update documentation
Feb 22, 2018
7627aa9
http_response: Fix broken timeout error handling
Feb 22, 2018
81722d8
http_response input: Add testing helper functions to eliminate repeat…
Feb 24, 2018
02dd69c
http_response input: Fix a couple of bugs in the helper functions
Feb 24, 2018
b80d0ff
http_response input: Reformat maps that ran past column 80
Feb 24, 2018
8ab127c
http_response input: Make test more rigorous, including exactly what …
Feb 25, 2018
17c8929
http_response input: Add tests for network errors and the plugin's in…
Feb 25, 2018
fb2f83b
http_response input: Remove UnknownNetwork and Address network error …
Feb 25, 2018
d6e1ecc
http_response input: Update README
Feb 25, 2018
817fb66
http_response input: Run make fmt
Feb 25, 2018
1751563
http_response input:
Mar 6, 2018
4c1e52c
http_response input:
Mar 6, 2018
69be2a8
http_response input:
Mar 6, 2018
cc3a0cd
http_response input:
Mar 6, 2018
7029079
http_response input:
Mar 6, 2018
927ec73
http_response input:
Mar 6, 2018
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
Prev Previous commit
Next Next commit
http_response input: Add options to log the response time even in the…
… event of a network error
  • Loading branch information
Germán Jaber committed Feb 18, 2018
commit 0f23ab0e4c787465e08bed04d975f2acafdd6dae
35 changes: 25 additions & 10 deletions plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import (

// HTTPResponse struct
type HTTPResponse struct {
Address string
Body string
Method string
ResponseTimeout internal.Duration
Headers map[string]string
FollowRedirects bool
ResponseStringMatch string
Address string
Body string
Method string
ResponseTimeout internal.Duration
OverwriteNetworkErrorLatency bool
FixedNetworkErrorLatency float64
LogNetworkErrorDuration bool
Headers map[string]string
FollowRedirects bool
ResponseStringMatch string

// Path to CA file
SSLCA string `toml:"ssl_ca"`
Expand Down Expand Up @@ -137,10 +140,19 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
// Start Timer
start := time.Now()
resp, err := h.client.Do(request)
response_time := time.Since(start).Seconds()

// If an error in returned, it means we are dealing with a network error
// HTTP error codes do not generate errors in the net/http library
if err != nil {
// Set response_time as this is a network error
if h.LogNetworkErrorDuration {
if h.OverwriteNetworkErrorLatency {
response_time = h.FixedNetworkErrorLatency
}
fields["response_time"] = response_time
}

// Timeouts have their special type
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
fields["result_type"] = "timeout"
Expand All @@ -153,21 +165,24 @@ func (h *HTTPResponse) httpGather() (map[string]interface{}, error) {
// If the error is a redirect we continue processing and log
// the HTTP response code
urlError, isUrlError := err.(*url.Error)
if ! h.FollowRedirects && isUrlError
&& urlError.Err == ErrRedirectAttempted {
if !h.FollowRedirects && isUrlError && urlError.Err == ErrRedirectAttempted {
err = nil
} else {
// If the error isn't a timeout or a redirect stop
// processing the request
return fields, nil
}
}

if _, ok := fields["response_time"]; !ok {
fields["response_time"] = response_time
}

defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()

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

// Check the response for a regex match.
Expand Down