From 0ed525e0322e6d9433531cc382a58d4c6e684471 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sat, 16 Nov 2019 15:48:37 -0500 Subject: [PATCH] Expose metrics on /metrics --- main.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 83117127..f4c381ae 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,12 @@ package main import ( "encoding/json" - "log" - "net/http" - + "github.com/TwinProduction/gatus/config" "github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/watchdog" + "github.com/prometheus/client_golang/prometheus/promhttp" + "log" + "net/http" ) func main() { @@ -14,17 +15,20 @@ func main() { http.HandleFunc("/api/v1/results", serviceResultsHandler) http.HandleFunc("/health", healthHandler) http.Handle("/", http.FileServer(http.Dir("./static"))) + if config.Get().Metrics { + http.Handle("/metrics", promhttp.Handler()) + } log.Println("[main][main] Listening on port 8080") log.Fatal(http.ListenAndServe(":8080", nil)) } -func serviceResultsHandler(writer http.ResponseWriter, request *http.Request) { +func serviceResultsHandler(writer http.ResponseWriter, _ *http.Request) { serviceResults := watchdog.GetServiceResults() writer.WriteHeader(http.StatusOK) _, _ = writer.Write(structToJsonBytes(serviceResults)) } -func healthHandler(writer http.ResponseWriter, request *http.Request) { +func healthHandler(writer http.ResponseWriter, _ *http.Request) { writer.WriteHeader(http.StatusOK) _, _ = writer.Write(structToJsonBytes(&core.HealthStatus{Status: "UP"})) }