Skip to content

Commit

Permalink
Add headers parameter for HTTP checker
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Lehmann <[email protected]>
  • Loading branch information
aaronlehmann committed Aug 20, 2015
1 parent e8f088f commit b67aab2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
2 changes: 2 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
21 changes: 20 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1400,7 +1402,9 @@ The URL to which events should be published.
yes
</td>
<td>
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.
</td>
</tr>
<tr>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1766,6 +1772,19 @@ health check will fail.
<td>
The URI to check.
</td>
</tr>
<tr>
<td>
<code>headers</code>
</td>
<td>
no
</td>
<td>
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.
</td>
</tr>
<tr>
<td>
Expand Down
13 changes: 11 additions & 2 deletions health/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions health/checks/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion registry/handlers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b67aab2

Please sign in to comment.