Skip to content

Commit

Permalink
Add ignore_status (#161)
Browse files Browse the repository at this point in the history
* Add ignore_status

Signed-off-by: Alexander Ryabov <[email protected]>

* Use valid_status_codes slice

Signed-off-by: Alexander Ryabov <[email protected]>

Co-authored-by: Alexander Ryabov <[email protected]>
  • Loading branch information
sepich and Alexander Ryabov authored Jul 11, 2022
1 parent a03b913 commit f8ddc2f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: [ <int>, ... | default = 2xx ]
13 changes: 12 additions & 1 deletion exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit f8ddc2f

Please sign in to comment.