From 2ac9f5426df733af64cb7308b825ad1fe8155959 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 19 Nov 2021 10:37:13 +0100 Subject: [PATCH] outposts: don't panic when listening for metrics fails Signed-off-by: Jens Langhammer --- internal/outpost/ldap/metrics/metrics.go | 5 +++-- internal/outpost/proxyv2/metrics/metrics.go | 5 +++-- internal/web/metrics.go | 10 ++++++---- internal/web/ssl.go | 2 +- internal/web/web.go | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/outpost/ldap/metrics/metrics.go b/internal/outpost/ldap/metrics/metrics.go index 8b1a12082..fdd1a1825 100644 --- a/internal/outpost/ldap/metrics/metrics.go +++ b/internal/outpost/ldap/metrics/metrics.go @@ -24,14 +24,15 @@ var ( func RunServer() { m := mux.NewRouter() + l := log.WithField("logger", "authentik.outpost.metrics") m.HandleFunc("/akprox/ping", func(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(204) }) m.Path("/metrics").Handler(promhttp.Handler()) listen := "0.0.0.0:9300" - log.WithField("logger", "authentik.outpost.metrics").WithField("listen", listen).Info("Starting Metrics server") + l.WithField("listen", listen).Info("Starting Metrics server") err := http.ListenAndServe(listen, m) if err != nil { - panic(err) + l.WithError(err).Warning("Failed to start metrics listener") } } diff --git a/internal/outpost/proxyv2/metrics/metrics.go b/internal/outpost/proxyv2/metrics/metrics.go index 93e088873..fb9f18b97 100644 --- a/internal/outpost/proxyv2/metrics/metrics.go +++ b/internal/outpost/proxyv2/metrics/metrics.go @@ -24,14 +24,15 @@ var ( func RunServer() { m := mux.NewRouter() + l := log.WithField("logger", "authentik.outpost.metrics") m.HandleFunc("/akprox/ping", func(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(204) }) m.Path("/metrics").Handler(promhttp.Handler()) listen := "0.0.0.0:9300" - log.WithField("logger", "authentik.outpost.metrics").WithField("listen", listen).Info("Starting Metrics server") + l.WithField("listen", listen).Info("Starting Metrics server") err := http.ListenAndServe(listen, m) if err != nil { - panic(err) + l.WithError(err).Warning("Failed to start metrics listener") } } diff --git a/internal/web/metrics.go b/internal/web/metrics.go index 29489c55e..2b584bdf6 100644 --- a/internal/web/metrics.go +++ b/internal/web/metrics.go @@ -21,6 +21,7 @@ var ( func RunMetricsServer() { m := mux.NewRouter() + l := log.WithField("logger", "authentik.router.metrics") m.Path("/metrics").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { promhttp.InstrumentMetricHandler( prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{ @@ -31,26 +32,27 @@ func RunMetricsServer() { // Get upstream metrics re, err := http.NewRequest("GET", "http://localhost:8000/metrics/", nil) if err != nil { - log.WithError(err).Warning("failed to get upstream metrics") + l.WithError(err).Warning("failed to get upstream metrics") return } re.SetBasicAuth("monitor", config.G.SecretKey) res, err := http.DefaultClient.Do(re) if err != nil { - log.WithError(err).Warning("failed to get upstream metrics") + l.WithError(err).Warning("failed to get upstream metrics") return } bm, err := ioutil.ReadAll(res.Body) if err != nil { - log.WithError(err).Warning("failed to get upstream metrics") + l.WithError(err).Warning("failed to get upstream metrics") return } _, err = rw.Write(bm) if err != nil { - log.WithError(err).Warning("failed to get upstream metrics") + l.WithError(err).Warning("failed to get upstream metrics") return } }) + l.WithField("listen", config.G.Web.ListenMetrics).Info("Listening (metrics)") err := http.ListenAndServe(config.G.Web.ListenMetrics, m) if err != nil { panic(err) diff --git a/internal/web/ssl.go b/internal/web/ssl.go index c847b5467..bf6fc4bf4 100644 --- a/internal/web/ssl.go +++ b/internal/web/ssl.go @@ -26,7 +26,7 @@ func (ws *WebServer) listenTLS() { ws.log.WithError(err).Fatalf("failed to listen") return } - ws.log.WithField("addr", config.G.Web.ListenTLS).Info("Listening (TLS)") + ws.log.WithField("listen", config.G.Web.ListenTLS).Info("Listening (TLS)") proxyListener := &proxyproto.Listener{Listener: tcpKeepAliveListener{ln.(*net.TCPListener)}} defer proxyListener.Close() diff --git a/internal/web/web.go b/internal/web/web.go index 7f6a2846b..4096f91dc 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -74,7 +74,7 @@ func (ws *WebServer) listenPlain() { if err != nil { ws.log.WithError(err).Fatalf("failed to listen") } - ws.log.WithField("addr", config.G.Web.Listen).Info("Listening") + ws.log.WithField("listen", config.G.Web.Listen).Info("Listening") proxyListener := &proxyproto.Listener{Listener: ln} defer proxyListener.Close()