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 content_length metric to http_response input plugin (#6037) #6261

Merged
merged 1 commit into from
Aug 16, 2019
Merged
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
3 changes: 2 additions & 1 deletion plugins/inputs/http_response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ This input plugin checks HTTP/HTTPS connections.
- result ([see below](#result--result_code))
- fields:
- response_time (float, seconds)
- content_length (int, response body length)
- response_string_match (int, 0 = mismatch / body read error, 1 = match)
- http_response_code (int, response status code)
- result_type (string, deprecated in 1.6: use `result` tag and `result_code` field)
Expand All @@ -85,5 +86,5 @@ This tag is used to expose network and plugin errors. HTTP errors are considered
### Example Output:

```
http_response,method=GET,server=http://www.github.com,status_code=200,result=success http_response_code=200i,response_time=6.223266528,result_type="success",result_code=0i 1459419354977857955
http_response,method=GET,result=success,server=http://github.com,status_code=200 content_length=87878i,http_response_code=200i,response_time=0.937655534,result_code=0i,result_type="success" 1565839598000000000
```
25 changes: 13 additions & 12 deletions plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,27 @@ func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string]

// This function closes the response body, as
// required by the net/http library
defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
defer resp.Body.Close()

// Set log the HTTP response code
tags["status_code"] = strconv.Itoa(resp.StatusCode)
fields["http_response_code"] = resp.StatusCode

// Check the response for a regex match.
if h.ResponseStringMatch != "" {

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("D! Failed to read body of HTTP Response : %s", err)
setResult("body_read_error", fields, tags)
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("D! Failed to read body of HTTP Response : %s", err)
setResult("body_read_error", fields, tags)
fields["content_length"] = len(bodyBytes)
if h.ResponseStringMatch != "" {
fields["response_string_match"] = 0
return fields, tags, nil
}
return fields, tags, nil
}

fields["content_length"] = len(bodyBytes)

// Check the response for a regex match.
if h.ResponseStringMatch != "" {
if h.compiledStringMatch.Match(bodyBytes) {
setResult("success", fields, tags)
fields["response_string_match"] = 1
Expand Down
86 changes: 82 additions & 4 deletions plugins/inputs/http_response/http_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestHeaders(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -201,6 +202,7 @@ func TestFields(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -262,6 +264,7 @@ func TestInterface(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -297,6 +300,7 @@ func TestRedirects(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -362,6 +366,7 @@ func TestMethod(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -391,6 +396,7 @@ func TestMethod(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags = map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -421,6 +427,7 @@ func TestMethod(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags = map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -456,6 +463,7 @@ func TestBody(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -520,6 +528,7 @@ func TestStringMatch(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -556,6 +565,7 @@ func TestStringMatchJson(t *testing.T) {
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -593,6 +603,7 @@ func TestStringMatchFail(t *testing.T) {
"result_type": "response_string_mismatch",
"result_code": 1,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -635,7 +646,7 @@ func TestTimeout(t *testing.T) {
"method": "GET",
"result": "timeout",
}
absentFields := []string{"http_response_code", "response_time", "response_string_match"}
absentFields := []string{"http_response_code", "response_time", "content_length", "response_string_match"}
absentTags := []string{"status_code"}
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, absentTags)
}
Expand All @@ -662,7 +673,7 @@ func TestPluginErrors(t *testing.T) {
err := h.Gather(&acc)
require.Error(t, err)

absentFields := []string{"http_response_code", "response_time", "response_string_match", "result_type", "result_code"}
absentFields := []string{"http_response_code", "response_time", "content_length", "response_string_match", "result_type", "result_code"}
absentTags := []string{"status_code", "result", "server", "method"}
checkOutput(t, &acc, nil, nil, absentFields, absentTags)

Expand All @@ -686,6 +697,7 @@ func TestPluginErrors(t *testing.T) {
"result_type": "body_read_error",
"result_code": 2,
"response_time": nil,
"content_length": nil,
}
expectedTags := map[string]interface{}{
"server": nil,
Expand Down Expand Up @@ -719,7 +731,7 @@ func TestNetworkErrors(t *testing.T) {
"method": "GET",
"result": "dns_error",
}
absentFields := []string{"http_response_code", "response_time", "response_string_match"}
absentFields := []string{"http_response_code", "response_time", "content_length", "response_string_match"}
absentTags := []string{"status_code"}
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, absentTags)

Expand All @@ -745,7 +757,73 @@ func TestNetworkErrors(t *testing.T) {
"method": "GET",
"result": "connection_failed",
}
absentFields = []string{"http_response_code", "response_time", "response_string_match"}
absentFields = []string{"http_response_code", "response_time", "content_length", "response_string_match"}
absentTags = []string{"status_code"}
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, absentTags)
}

func TestContentLength(t *testing.T) {
mux := setUpTestMux()
ts := httptest.NewServer(mux)
defer ts.Close()

h := &HTTPResponse{
URLs: []string{ts.URL + "/good"},
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
var acc testutil.Accumulator
err := h.Gather(&acc)
require.NoError(t, err)

expectedFields := map[string]interface{}{
"http_response_code": http.StatusOK,
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": len([]byte("hit the good page!")),
}
expectedTags := map[string]interface{}{
"server": nil,
"method": "GET",
"status_code": "200",
"result": "success",
}
absentFields := []string{"response_string_match"}
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)

h = &HTTPResponse{
URLs: []string{ts.URL + "/musthaveabody"},
Body: "{ 'test': 'data'}",
Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 20},
Headers: map[string]string{
"Content-Type": "application/json",
},
FollowRedirects: true,
}
acc = testutil.Accumulator{}
err = h.Gather(&acc)
require.NoError(t, err)

expectedFields = map[string]interface{}{
"http_response_code": http.StatusOK,
"result_type": "success",
"result_code": 0,
"response_time": nil,
"content_length": len([]byte("sent a body!")),
}
expectedTags = map[string]interface{}{
"server": nil,
"method": "GET",
"status_code": "200",
"result": "success",
}
absentFields = []string{"response_string_match"}
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
}