Add timeout customization

This commit is contained in:
Faiz Shukri
2016-06-16 13:38:51 +08:00
parent 20e4dd1414
commit 3f4b9ced77
4 changed files with 11 additions and 3 deletions

View File

@@ -53,7 +53,7 @@ func main() {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for _, mon := range cfg.Monitors { for _, mon := range cfg.Monitors {
cfg.Logger.Printf(" Starting %s: %d seconds check interval\n - %v %s", mon.Name, mon.CheckInterval, mon.Method, mon.URL) cfg.Logger.Printf(" Starting %s: %d seconds check interval\n - %v %s (%d second/s timeout)", mon.Name, mon.CheckInterval, mon.Method, mon.URL, mon.HttpTimeout)
// print features // print features
if mon.ExpectedStatusCode > 0 { if mon.ExpectedStatusCode > 0 {

View File

@@ -9,6 +9,7 @@
"threshold": 80, "threshold": 80,
"component_id": 1, "component_id": 1,
"interval": 10, "interval": 10,
"timeout": 5,
"expected_status_code": 200, "expected_status_code": 200,
"strict_tls": true "strict_tls": true
} }

View File

@@ -13,8 +13,8 @@ import (
"time" "time"
) )
const HttpTimeout = time.Duration(time.Second)
const DefaultInterval = 60 const DefaultInterval = 60
const DefaultTimeout = 1
const DefaultTimeFormat = "15:04:05 Jan 2 MST" const DefaultTimeFormat = "15:04:05 Jan 2 MST"
// Monitor data model // Monitor data model
@@ -24,6 +24,7 @@ type Monitor struct {
Method string `json:"method"` Method string `json:"method"`
StrictTLS bool `json:"strict_tls"` StrictTLS bool `json:"strict_tls"`
CheckInterval time.Duration `json:"interval"` CheckInterval time.Duration `json:"interval"`
HttpTimeout time.Duration `json:"timeout"`
MetricID int `json:"metric_id"` MetricID int `json:"metric_id"`
ComponentID int `json:"component_id"` ComponentID int `json:"component_id"`
@@ -100,7 +101,7 @@ func (monitor *Monitor) Tick() {
func (monitor *Monitor) doRequest() bool { func (monitor *Monitor) doRequest() bool {
client := &http.Client{ client := &http.Client{
Timeout: HttpTimeout, Timeout: time.Duration(monitor.HttpTimeout * time.Second),
} }
if monitor.StrictTLS == false { if monitor.StrictTLS == false {
client.Transport = &http.Transport{ client.Transport = &http.Transport{
@@ -213,6 +214,10 @@ func (monitor *Monitor) ValidateConfiguration() error {
monitor.CheckInterval = DefaultInterval monitor.CheckInterval = DefaultInterval
} }
if monitor.HttpTimeout < 1 {
monitor.HttpTimeout = DefaultTimeout
}
monitor.Method = strings.ToUpper(monitor.Method) monitor.Method = strings.ToUpper(monitor.Method)
switch monitor.Method { switch monitor.Method {
case "GET", "POST", "DELETE", "OPTIONS", "HEAD": case "GET", "POST", "DELETE", "OPTIONS", "HEAD":

View File

@@ -32,6 +32,8 @@ Configuration
"strict_tls": true, "strict_tls": true,
// seconds between checks // seconds between checks
"interval": 10, "interval": 10,
// seconds for http timeout
"timeout": 5,
// post lag to cachet metric (graph) // post lag to cachet metric (graph)
// note either metric ID or component ID are required // note either metric ID or component ID are required
"metric_id": <metric id>, "metric_id": <metric id>,