Add ignore_status (#161)

* Add ignore_status

Signed-off-by: Alexander Ryabov <alexander.ryabov@jetbrains.com>

* Use valid_status_codes slice

Signed-off-by: Alexander Ryabov <alexander.ryabov@jetbrains.com>

Co-authored-by: Alexander Ryabov <alexander.ryabov@jetbrains.com>
This commit is contained in:
Alex R 2022-07-11 02:08:07 +02:00 committed by GitHub
parent a03b913a00
commit f8ddc2fa84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -57,6 +57,7 @@ type Module struct {
Metrics []Metric `yaml:"metrics"` Metrics []Metric `yaml:"metrics"`
HTTPClientConfig pconfig.HTTPClientConfig `yaml:"http_client_config,omitempty"` HTTPClientConfig pconfig.HTTPClientConfig `yaml:"http_client_config,omitempty"`
Body Body `yaml:"body,omitempty"` Body Body `yaml:"body,omitempty"`
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
} }
type Body struct { type Body struct {

View File

@ -43,3 +43,6 @@ modules:
# username: myuser # username: myuser
# #password: veryverysecret # #password: veryverysecret
# password_file: /tmp/mysecret.txt # password_file: /tmp/mysecret.txt
# Accepted status codes for this probe. Defaults to 2xx.
# valid_status_codes: [ <int>, ... | default = 2xx ]

View File

@ -179,7 +179,18 @@ func (f *JSONFetcher) FetchJSON(endpoint string) ([]byte, error) {
resp.Body.Close() resp.Body.Close()
}() }()
if resp.StatusCode/100 != 2 { if len(f.module.ValidStatusCodes) != 0 {
success := false
for _, code := range f.module.ValidStatusCodes {
if resp.StatusCode == code {
success = true
break
}
}
if !success {
return nil, errors.New(resp.Status)
}
} else if resp.StatusCode/100 != 2 {
return nil, errors.New(resp.Status) return nil, errors.New(resp.Status)
} }