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

[WIP] add initial support for acceptable status code ranges #143

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 36 additions & 1 deletion check/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion testdata/config.json
Original file line number Diff line number Diff line change
@@ -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"}
{"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"}