2021-09-09 13:52:24 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"goauthentik.io/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Requests = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
Name: "authentik_main_requests",
|
|
|
|
Help: "The total number of configured providers",
|
|
|
|
}, []string{"dest"})
|
|
|
|
)
|
|
|
|
|
|
|
|
func RunMetricsServer() {
|
|
|
|
m := mux.NewRouter()
|
2021-11-19 09:37:13 +00:00
|
|
|
l := log.WithField("logger", "authentik.router.metrics")
|
2021-09-09 13:52:24 +00:00
|
|
|
m.Path("/metrics").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2021-09-16 10:09:12 +00:00
|
|
|
promhttp.InstrumentMetricHandler(
|
2021-09-09 13:52:24 +00:00
|
|
|
prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
|
|
|
|
DisableCompression: true,
|
|
|
|
}),
|
|
|
|
).ServeHTTP(rw, r)
|
|
|
|
|
|
|
|
// Get upstream metrics
|
|
|
|
re, err := http.NewRequest("GET", "http://localhost:8000/metrics/", nil)
|
|
|
|
if err != nil {
|
2021-11-19 09:37:13 +00:00
|
|
|
l.WithError(err).Warning("failed to get upstream metrics")
|
2021-09-09 13:52:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
re.SetBasicAuth("monitor", config.G.SecretKey)
|
|
|
|
res, err := http.DefaultClient.Do(re)
|
|
|
|
if err != nil {
|
2021-11-19 09:37:13 +00:00
|
|
|
l.WithError(err).Warning("failed to get upstream metrics")
|
2021-09-09 13:52:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
bm, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
2021-11-19 09:37:13 +00:00
|
|
|
l.WithError(err).Warning("failed to get upstream metrics")
|
2021-09-09 13:52:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = rw.Write(bm)
|
|
|
|
if err != nil {
|
2021-11-19 09:37:13 +00:00
|
|
|
l.WithError(err).Warning("failed to get upstream metrics")
|
2021-09-09 13:52:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2021-12-22 09:33:21 +00:00
|
|
|
l.WithField("listen", config.G.Web.ListenMetrics).Info("Starting Metrics server")
|
2021-09-09 13:52:24 +00:00
|
|
|
err := http.ListenAndServe(config.G.Web.ListenMetrics, m)
|
|
|
|
if err != nil {
|
2021-12-22 09:33:21 +00:00
|
|
|
l.WithError(err).Warning("Failed to start metrics server")
|
2021-09-09 13:52:24 +00:00
|
|
|
}
|
2021-12-22 09:33:21 +00:00
|
|
|
l.WithField("listen", config.G.Web.ListenMetrics).Info("Stopping Metrics server")
|
2021-09-09 13:52:24 +00:00
|
|
|
}
|