From f8ddc2fa84350116fa0bec21dce49aa99e06ccac Mon Sep 17 00:00:00 2001 From: Alex R Date: Mon, 11 Jul 2022 02:08:07 +0200 Subject: [PATCH] Add ignore_status (#161) * Add ignore_status Signed-off-by: Alexander Ryabov * Use valid_status_codes slice Signed-off-by: Alexander Ryabov Co-authored-by: Alexander Ryabov --- config/config.go | 1 + examples/config.yml | 3 +++ exporter/util.go | 13 ++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index c98012b3..319db9cf 100644 --- a/config/config.go +++ b/config/config.go @@ -57,6 +57,7 @@ type Module struct { Metrics []Metric `yaml:"metrics"` HTTPClientConfig pconfig.HTTPClientConfig `yaml:"http_client_config,omitempty"` Body Body `yaml:"body,omitempty"` + ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"` } type Body struct { diff --git a/examples/config.yml b/examples/config.yml index d81785ae..5db61ea7 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -43,3 +43,6 @@ modules: # username: myuser # #password: veryverysecret # password_file: /tmp/mysecret.txt + + # Accepted status codes for this probe. Defaults to 2xx. + # valid_status_codes: [ , ... | default = 2xx ] \ No newline at end of file diff --git a/exporter/util.go b/exporter/util.go index 0bbad4c6..1a5d8cd5 100644 --- a/exporter/util.go +++ b/exporter/util.go @@ -179,7 +179,18 @@ func (f *JSONFetcher) FetchJSON(endpoint string) ([]byte, error) { 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) }