outposts: don't panic when listening for metrics fails

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-11-19 10:37:13 +01:00
parent 8d1fd48003
commit 2ac9f5426d
5 changed files with 14 additions and 10 deletions

View File

@ -24,14 +24,15 @@ var (
func RunServer() { func RunServer() {
m := mux.NewRouter() m := mux.NewRouter()
l := log.WithField("logger", "authentik.outpost.metrics")
m.HandleFunc("/akprox/ping", func(rw http.ResponseWriter, r *http.Request) { m.HandleFunc("/akprox/ping", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(204) rw.WriteHeader(204)
}) })
m.Path("/metrics").Handler(promhttp.Handler()) m.Path("/metrics").Handler(promhttp.Handler())
listen := "0.0.0.0:9300" 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) err := http.ListenAndServe(listen, m)
if err != nil { if err != nil {
panic(err) l.WithError(err).Warning("Failed to start metrics listener")
} }
} }

View File

@ -24,14 +24,15 @@ var (
func RunServer() { func RunServer() {
m := mux.NewRouter() m := mux.NewRouter()
l := log.WithField("logger", "authentik.outpost.metrics")
m.HandleFunc("/akprox/ping", func(rw http.ResponseWriter, r *http.Request) { m.HandleFunc("/akprox/ping", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(204) rw.WriteHeader(204)
}) })
m.Path("/metrics").Handler(promhttp.Handler()) m.Path("/metrics").Handler(promhttp.Handler())
listen := "0.0.0.0:9300" 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) err := http.ListenAndServe(listen, m)
if err != nil { if err != nil {
panic(err) l.WithError(err).Warning("Failed to start metrics listener")
} }
} }

View File

@ -21,6 +21,7 @@ var (
func RunMetricsServer() { func RunMetricsServer() {
m := mux.NewRouter() m := mux.NewRouter()
l := log.WithField("logger", "authentik.router.metrics")
m.Path("/metrics").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { m.Path("/metrics").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
promhttp.InstrumentMetricHandler( promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{ prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
@ -31,26 +32,27 @@ func RunMetricsServer() {
// Get upstream metrics // Get upstream metrics
re, err := http.NewRequest("GET", "http://localhost:8000/metrics/", nil) re, err := http.NewRequest("GET", "http://localhost:8000/metrics/", nil)
if err != nil { if err != nil {
log.WithError(err).Warning("failed to get upstream metrics") l.WithError(err).Warning("failed to get upstream metrics")
return return
} }
re.SetBasicAuth("monitor", config.G.SecretKey) re.SetBasicAuth("monitor", config.G.SecretKey)
res, err := http.DefaultClient.Do(re) res, err := http.DefaultClient.Do(re)
if err != nil { if err != nil {
log.WithError(err).Warning("failed to get upstream metrics") l.WithError(err).Warning("failed to get upstream metrics")
return return
} }
bm, err := ioutil.ReadAll(res.Body) bm, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
log.WithError(err).Warning("failed to get upstream metrics") l.WithError(err).Warning("failed to get upstream metrics")
return return
} }
_, err = rw.Write(bm) _, err = rw.Write(bm)
if err != nil { if err != nil {
log.WithError(err).Warning("failed to get upstream metrics") l.WithError(err).Warning("failed to get upstream metrics")
return return
} }
}) })
l.WithField("listen", config.G.Web.ListenMetrics).Info("Listening (metrics)")
err := http.ListenAndServe(config.G.Web.ListenMetrics, m) err := http.ListenAndServe(config.G.Web.ListenMetrics, m)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -26,7 +26,7 @@ func (ws *WebServer) listenTLS() {
ws.log.WithError(err).Fatalf("failed to listen") ws.log.WithError(err).Fatalf("failed to listen")
return 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)}} proxyListener := &proxyproto.Listener{Listener: tcpKeepAliveListener{ln.(*net.TCPListener)}}
defer proxyListener.Close() defer proxyListener.Close()

View File

@ -74,7 +74,7 @@ func (ws *WebServer) listenPlain() {
if err != nil { if err != nil {
ws.log.WithError(err).Fatalf("failed to listen") 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} proxyListener := &proxyproto.Listener{Listener: ln}
defer proxyListener.Close() defer proxyListener.Close()