Skip to content

Commit

Permalink
add health endpoint (#330)
Browse files Browse the repository at this point in the history
* remove spurious v from html title of "/"

* add health endpoint
  • Loading branch information
oliver006 authored Nov 14, 2019
1 parent 175a69f commit e671c9b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 4 additions & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,12 @@ func NewRedisExporter(redisURI string, opts ExporterOptions) (*Exporter, error)
}

e.mux.HandleFunc("/scrape", e.ScrapeHandler)
e.mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`ok`))
})
e.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Redis Exporter v` + BuildVersion + `</title></head>
<head><title>Redis Exporter ` + BuildVersion + `</title></head>
<body>
<h1>Redis Exporter ` + BuildVersion + `</h1>
<p><a href='` + opts.MetricsPath + `'>Metrics</a></p>
Expand Down
27 changes: 21 additions & 6 deletions exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ func TestIncludeSystemMemoryMetric(t *testing.T) {
}
}

func TestHTTPEndpoints(t *testing.T) {
func TestHTTPScrapeMetricsEndpoints(t *testing.T) {
setupDBKeys(t, os.Getenv("TEST_REDIS_URI"))
defer deleteKeysFromDB(t, os.Getenv("TEST_REDIS_URI"))
setupDBKeys(t, os.Getenv("TEST_PWD_REDIS_URI"))
Expand Down Expand Up @@ -1339,7 +1339,7 @@ func TestCheckKeys(t *testing.T) {
}
}

func TestHTTPIndexPage(t *testing.T) {
func TestHTTPHTMLPages(t *testing.T) {
if os.Getenv("TEST_PWD_REDIS_URI") == "" {
t.Skipf("TEST_PWD_REDIS_URI not set - skipping")
}
Expand All @@ -1348,10 +1348,25 @@ func TestHTTPIndexPage(t *testing.T) {
ts := httptest.NewServer(e)
defer ts.Close()

want := `<head><title>Redis Exporter v`
body := downloadURL(t, ts.URL+"/")
if !strings.Contains(body, want) {
t.Errorf(`error, expected string "%s" in body, got body: \n\n%s`, want, body)
for _, tst := range []struct {
path string
want string
}{
{
path: "/",
want: `<head><title>Redis Exporter `,
},
{
path: "/health",
want: `ok`,
},
} {
t.Run(fmt.Sprintf("path: %s", tst.path), func(t *testing.T) {
body := downloadURL(t, ts.URL+tst.path)
if !strings.Contains(body, tst.want) {
t.Fatalf(`error, expected string "%s" in body, got body: \n\n%s`, tst.want, body)
}
})
}
}

Expand Down

0 comments on commit e671c9b

Please sign in to comment.