From 97392b0bfc2fb2941165ee40cc01b9816422ccc8 Mon Sep 17 00:00:00 2001 From: Kevin Ard Date: Fri, 9 Oct 2020 17:20:28 -0400 Subject: [PATCH 1/2] add initial support for acceptable status code ranges --- check/http/http.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/check/http/http.go b/check/http/http.go index f9b6b5c..d1e5f77 100644 --- a/check/http/http.go +++ b/check/http/http.go @@ -15,6 +15,12 @@ import ( // Type should match the package name const Type = "http" +// UpStatusRange is a range of numeric response codes +type UpStatusRange struct { + Min int `json:"min,omitempty"` + Max int `json:"max,omitempty"` +} + // Checker implements a Checker for HTTP endpoints. type Checker struct { // Name is the name of the endpoint. @@ -27,6 +33,18 @@ type Checker struct { // a healthy endpoint. Default is http.StatusOK. UpStatus int `json:"up_status,omitempty"` + // UpStausRange allows specifying a range of acceptable + // status codes for a health endpoint + // eg: 200-399 to include acceptable redirect codes, + // 200-299 for only 2x series, + // or 400-599 to guarantee the asset is gone + // if one - but not both - value is provided, the other will + // use the default value of min: 0, max: 999 + // this allows simpler configs like + // - up_status_range.min = 400 (anything above 400 series will pass) + // - up.status_range.max = 399 (anything below 400 series will pass) + UpStatusRange UpStatusRange `json:"up_status_range,omitempty"` + // ThresholdRTT is the maximum round trip time to // allow for a healthy endpoint. If non-zero and a // request takes longer than ThresholdRTT, the @@ -174,7 +192,24 @@ func (c Checker) conclude(result types.Result) types.Result { // the configuration of c. It returns a non-nil error if down. // Note that it does not check for degraded response. func (c Checker) checkDown(resp *http.Response) error { - // Check status code + // Check status code range + if c.UpStatusRange.Min > 0 || c.UpStatusRange.Max > 0 { + useMin := 0 + if c.UpStatusRange.Min > useMin { + useMin = c.UpStatusRange.Min + } + + useMax := 999 + if c.UpStatusRange.Max > 0 && c.UpStatusRange.Max < useMax { + useMax = c.UpStatusRange.Max + } + + if resp.StatusCode >= useMin && resp.StatusCode <= useMax { + return nil + } + } + + // Check single status code var validStatus map[int]bool if c.UpStatus > 0 { // Explicit match against expected UpStatus From 6f280ebf14a1d8a03fc42a675c23068d45488304 Mon Sep 17 00:00:00 2001 From: Kevin Ard Date: Fri, 9 Oct 2020 17:44:09 -0400 Subject: [PATCH 2/2] update testdata/config.json with nil value for up_status_range [ci skip] --- testdata/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testdata/config.json b/testdata/config.json index b39bc1e..5a246f2 100644 --- a/testdata/config.json +++ b/testdata/config.json @@ -1 +1 @@ -{"storage":{"type":"s3","access_key_id":"AAAAAA6WVZYYANEAFL6Q","secret_access_key":"DbvNDdKHaN4n8n3qqqXwvUVqVQTcHVmNYtvcJfTd","region":"us-east-1","bucket":"test","check_expiry":604800000000000},"checkers":[{"type":"http","endpoint_name":"Example (HTTP)","endpoint_url":"http://www.example.com","attempts":5},{"type":"http","endpoint_name":"Example (HTTPS)","endpoint_url":"https://example.com","threshold_rtt":500000000,"attempts":5},{"type":"http","endpoint_name":"localhost","endpoint_url":"http://localhost:2015","threshold_rtt":1000000,"attempts":5}],"timestamp":"0001-01-01T00:00:00Z"} \ No newline at end of file +{"storage":{"type":"s3","access_key_id":"AAAAAA6WVZYYANEAFL6Q","secret_access_key":"DbvNDdKHaN4n8n3qqqXwvUVqVQTcHVmNYtvcJfTd","region":"us-east-1","bucket":"test","check_expiry":604800000000000},"checkers":[{"type":"http","endpoint_name":"Example (HTTP)","endpoint_url":"http://www.example.com","up_status_range":{},"attempts":5},{"type":"http","endpoint_name":"Example (HTTPS)","endpoint_url":"https://example.com","up_status_range":{},"threshold_rtt":500000000,"attempts":5},{"type":"http","endpoint_name":"localhost","endpoint_url":"http://localhost:2015","up_status_range":{},"threshold_rtt":1000000,"attempts":5}],"timestamp":"0001-01-01T00:00:00Z"} \ No newline at end of file