From d5394a72458594085790f1a99d40d6f6aacec07d Mon Sep 17 00:00:00 2001 From: Povilas Vaitkus Date: Tue, 10 Dec 2024 16:41:06 +0200 Subject: [PATCH] Add custom health check endpoint Signed-off-by: Povilas Vaitkus --- README.md | 1 + stackdriver_exporter.go | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/README.md b/README.md index 0d04e05..abc181a 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ If you are still using the legacy [Access scopes][access-scopes], the `https://w | `web.systemd-socket` | No | | Use systemd socket activation listeners instead of port listeners (Linux only). | | `web.stackdriver-telemetry-path` | No | `/metrics` | Path under which to expose Stackdriver metrics. | | `web.telemetry-path` | No | `/metrics` | Path under which to expose Prometheus metrics | +| `web.health-check-path` | No | `/heath` | Path under which to expose health check | ### TLS and basic authentication diff --git a/stackdriver_exporter.go b/stackdriver_exporter.go index 4fc1c0a..859e4bf 100644 --- a/stackdriver_exporter.go +++ b/stackdriver_exporter.go @@ -55,6 +55,10 @@ var ( "web.stackdriver-telemetry-path", "Path under which to expose Stackdriver metrics.", ).Default("/metrics").String() + healthCheckPath = kingpin.Flag( + "web.health-check-path", "Path under which to expose health check.", + ).Default("/health").String() + projectID = kingpin.Flag( "google.project-id", "DEPRECATED - Comma seperated list of Google Project IDs. Use 'google.project-ids' instead.", ).String() @@ -275,6 +279,8 @@ func main() { kingpin.HelpFlag.Short('h') kingpin.Parse() + http.HandleFunc(*healthCheckPath, healthCheckHandler) + logger := promslog.New(promslogConfig) if *projectID != "" { logger.Warn("The google.project-id flag is deprecated and will be replaced by google.project-ids.") @@ -432,3 +438,8 @@ func parseMetricExtraFilters() []collectors.MetricFilter { } return extraFilters } + +func healthCheckHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK")) +}