Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ func main() {
http.Handle(*metricsPath, promhttp.Handler())
}

http.HandleFunc("/-/healthy", healthCheckHandler)
http.HandleFunc("/-/ready", healthCheckHandler)

if *metricsPath != "/" && *metricsPath != "" {
landingConfig := web.LandingConfig{
Name: "Stackdriver Exporter",
Expand All @@ -297,6 +300,14 @@ func main() {
Address: *metricsPath,
Text: "Metrics",
},
{
Address: "/-/healthy",
Text: "Healthy",
},
{
Address: "/-/ready",
Text: "Ready",
},
},
}
if *metricsPath != *stackdriverMetricsPath {
Expand All @@ -322,6 +333,11 @@ func main() {
}
}

func healthCheckHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "OK")
}

func collectorConfigFromFlags() *config.Config {
return &config.Config{
ProjectIDs: slices.Clone(*projectIDs),
Expand Down
31 changes: 31 additions & 0 deletions stackdriver_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package main

import (
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

Expand Down Expand Up @@ -46,3 +49,31 @@ func TestCollectorConfigFromFlagsDefaultsProjectsRefreshIntervalToDisabled(t *te
t.Fatalf("ProjectsRefreshInterval = %v, want default %v", cfg.ProjectsRefreshInterval, config.DefaultProjectsRefreshInterval)
}
}

func TestHealthCheckHandler(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/-/healthy", healthCheckHandler)
mux.HandleFunc("/-/ready", healthCheckHandler)

for _, path := range []string{"/-/healthy", "/-/ready"} {
t.Run(path, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, path, nil)
rrw := httptest.NewRecorder()
mux.ServeHTTP(rrw, req)

res := rrw.Result()
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want %d", res.StatusCode, http.StatusOK)
}
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
if got := string(body); got != "OK" {
t.Fatalf("body = %q, want %q", got, "OK")
}
})
}
}