From b67aab2f604a2c1531602ce694a129f74c0dd843 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Wed, 19 Aug 2015 18:23:58 -0700 Subject: [PATCH] Add headers parameter for HTTP checker Signed-off-by: Aaron Lehmann --- configuration/configuration.go | 2 ++ docs/configuration.md | 21 ++++++++++++++++++++- health/checks/checks.go | 13 +++++++++++-- health/checks/checks_test.go | 4 ++-- registry/handlers/app.go | 2 +- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/configuration/configuration.go b/configuration/configuration.go index b9685741..1b3519d5 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -202,6 +202,8 @@ type HTTPChecker struct { Interval time.Duration `yaml:"interval,omitempty"` // URI is the HTTP URI to check URI string `yaml:"uri,omitempty"` + // Headers lists static headers that should be added to all requests + Headers http.Header `yaml:"headers"` // Threshold is the number of times a check must fail to trigger an // unhealthy state Threshold int `yaml:"threshold,omitempty"` diff --git a/docs/configuration.md b/docs/configuration.md index 3e4bacc8..3563ef51 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -205,6 +205,8 @@ information about each option that appears later in this page. interval: 10s http: - uri: http://server.to.check/must/return/200 + headers: + Authorization: [Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==] statuscode: 200 timeout: 3s interval: 10s @@ -1400,7 +1402,9 @@ The URL to which events should be published. yes - Static headers to add to each request. + Static headers to add to each request. Each header's name should be a key + underneath headers, and each value is a list of payloads for that + header name. Note that values must always be lists. @@ -1619,6 +1623,8 @@ Configure the behavior of the Redis connection pool. interval: 10s http: - uri: http://server.to.check/must/return/200 + headers: + Authorization: [Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==] statuscode: 200 timeout: 3s interval: 10s @@ -1766,6 +1772,19 @@ health check will fail. The URI to check. + + + + headers + + + no + + + Static headers to add to each request. Each header's name should be a key + underneath headers, and each value is a list of payloads for that + header name. Note that values must always be lists. + diff --git a/health/checks/checks.go b/health/checks/checks.go index 86e914b1..e3c3b08d 100644 --- a/health/checks/checks.go +++ b/health/checks/checks.go @@ -24,12 +24,21 @@ func FileChecker(f string) health.Checker { // HTTPChecker does a HEAD request and verifies that the HTTP status code // returned matches statusCode. -func HTTPChecker(r string, statusCode int, timeout time.Duration) health.Checker { +func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.Header) health.Checker { return health.CheckFunc(func() error { client := http.Client{ Timeout: timeout, } - response, err := client.Head(r) + req, err := http.NewRequest("HEAD", r, nil) + if err != nil { + return errors.New("error creating request: " + r) + } + for headerName, headerValues := range headers { + for _, headerValue := range headerValues { + req.Header.Add(headerName, headerValue) + } + } + response, err := client.Do(req) if err != nil { return errors.New("error while checking: " + r) } diff --git a/health/checks/checks_test.go b/health/checks/checks_test.go index 8ba24d33..6b6dd14f 100644 --- a/health/checks/checks_test.go +++ b/health/checks/checks_test.go @@ -15,11 +15,11 @@ func TestFileChecker(t *testing.T) { } func TestHTTPChecker(t *testing.T) { - if err := HTTPChecker("https://www.google.cybertron", 200, 0).Check(); err == nil { + if err := HTTPChecker("https://www.google.cybertron", 200, 0, nil).Check(); err == nil { t.Errorf("Google on Cybertron was expected as not exists") } - if err := HTTPChecker("https://www.google.pt", 200, 0).Check(); err != nil { + if err := HTTPChecker("https://www.google.pt", 200, 0, nil).Check(); err != nil { t.Errorf("Google at Portugal was expected as exists, error:%v", err) } } diff --git a/registry/handlers/app.go b/registry/handlers/app.go index 24f43f37..b1e46b02 100644 --- a/registry/handlers/app.go +++ b/registry/handlers/app.go @@ -281,7 +281,7 @@ func (app *App) RegisterHealthChecks(healthRegistries ...*health.Registry) { statusCode = 200 } - checker := checks.HTTPChecker(httpChecker.URI, statusCode, httpChecker.Timeout) + checker := checks.HTTPChecker(httpChecker.URI, statusCode, httpChecker.Timeout, httpChecker.Headers) if httpChecker.Threshold != 0 { ctxu.GetLogger(app).Infof("configuring HTTP health check uri=%s, interval=%d, threshold=%d", httpChecker.URI, interval/time.Second, httpChecker.Threshold)