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 : Add in support for looking for substring in response #2204

Merged
merged 6 commits into from
Feb 1, 2017

Conversation

lrsmith
Copy link
Contributor

@lrsmith lrsmith commented Dec 28, 2016

When hitting a HTTP health_check it will return a valid HTTP status but will have an "healthy" or "un-healthy" message in the response. I've extended http_response to allow to specify a substring to search for in the HTTP response and return a metric of 1 if found, 0 if not. This would allow the metric to be passed or be pulled by monitoring.

Required for all PRs:

  • CHANGELOG.md updated (we recommend not updating this until the PR has been approved by a maintainer)
  • Sign CLA (if not already signed)
  • README.md updated (if adding a new plugin)

@lrsmith lrsmith changed the title Add in support for looking for substring in response http_response : Add in support for looking for substring in response Dec 28, 2016
@sparrc
Copy link
Contributor

sparrc commented Jan 9, 2017

this looks good but I think it should support regex as well


// Check the response for a regex match
if h.ResponseStringMatch != "" {
regex, compile_err := regexp.Compile(h.ResponseStringMatch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change compile_err to err

if h.ResponseStringMatch != "" {
regex, compile_err := regexp.Compile(h.ResponseStringMatch)
if compile_err != nil {
fields["response_string_match"] = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print an Error log message here to indicate that the regex compile failed

fields["response_string_match"] = 0
}

bodyBytes, _ := ioutil.ReadAll(resp.Body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't move on and read the response body if the regex compile failed

@@ -23,6 +23,11 @@ This input plugin will test HTTP/HTTPS connections.
# {'fake':'data'}
# '''

## Optional : Look for substring in body of the response
# response_string_match = "\"service_status\": \"up\""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than the or, you should put a list of examples above with two ## in front of it

@lrsmith
Copy link
Contributor Author

lrsmith commented Jan 11, 2017

Looks like a missing docker image for testing

docker run --name aerospike -p "3000:3000" -d aerospike/aerospike-server:3.9.0
Unable to find image 'aerospike/aerospike-server:3.9.0' locally
3.9.0: Pulling from aerospike/aerospike-server

@sparrc sparrc added this to the 1.3.0 milestone Jan 11, 2017
Copy link
Contributor

@sparrc sparrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

@@ -54,6 +58,11 @@ var sampleConfig = `
# {'fake':'data'}
# '''

## Optional : Look for substring in body of the response. Can use regex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the sample config here to match the README

fields["response_string_match"] = 0
} else {

bodyBytes, _ := ioutil.ReadAll(resp.Body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't ignore the error here. If there is an error, log it and continue.

} else {

bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not convert the response body to a string, you can use regex.Match below (instead of regex.MatchString)

@@ -54,6 +58,11 @@ var sampleConfig = `
# {'fake':'data'}
# '''

## Optional substring or regex match in body of the response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 spaces instead of a tab here


// Check the response for a regex match
if h.ResponseStringMatch != "" {
regex, err := regexp.Compile(h.ResponseStringMatch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still need to change this to only compile the regex once

@sparrc
Copy link
Contributor

sparrc commented Jan 27, 2017

looking good, just a few more changes

@sparrc sparrc merged commit 9003efc into influxdata:master Feb 1, 2017
mlindes pushed a commit to Comcast/telegraf that referenced this pull request Feb 6, 2017
…nfluxdata#2204)

* Add in support for looking for substring in response

* Add note to CHANGELOG.md

* Switch from substring match to regex match

* Requested code changes

* Make requested changes and refactor to avoid nested if-else.

* Convert tabs to space and compile regex once
maxunt pushed a commit that referenced this pull request Jun 26, 2018
…2204)

* Add in support for looking for substring in response

* Add note to CHANGELOG.md

* Switch from substring match to regex match

* Requested code changes

* Make requested changes and refactor to avoid nested if-else.

* Convert tabs to space and compile regex once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants