2021-09-09 13:52:24 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2023-09-02 15:58:37 +00:00
|
|
|
"fmt"
|
2022-08-08 19:00:45 +00:00
|
|
|
"io"
|
2021-09-09 13:52:24 +00:00
|
|
|
"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"
|
2022-06-10 20:19:48 +00:00
|
|
|
"goauthentik.io/internal/utils/sentry"
|
2021-09-09 13:52:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Requests = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
2023-07-27 16:51:08 +00:00
|
|
|
Name: "authentik_main_request_duration_seconds",
|
|
|
|
Help: "API request latencies in seconds",
|
|
|
|
}, []string{"dest"})
|
|
|
|
|
|
|
|
// NOTE: the following metric is kept for compatibility purpose
|
|
|
|
RequestsLegacy = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
2021-09-09 13:52:24 +00:00
|
|
|
Name: "authentik_main_requests",
|
|
|
|
Help: "The total number of configured providers",
|
|
|
|
}, []string{"dest"})
|
|
|
|
)
|
|
|
|
|
2023-09-02 15:58:37 +00:00
|
|
|
func (ws *WebServer) runMetricsServer() {
|
2021-09-09 13:52:24 +00:00
|
|
|
m := mux.NewRouter()
|
2021-11-19 09:37:13 +00:00
|
|
|
l := log.WithField("logger", "authentik.router.metrics")
|
2022-06-10 20:19:48 +00:00
|
|
|
m.Use(sentry.SentryNoSampleMiddleware)
|
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
|
2023-09-02 15:58:37 +00:00
|
|
|
re, err := http.NewRequest("GET", fmt.Sprintf("%s/-/metrics/", ws.ul.String()), nil)
|
2021-09-09 13:52:24 +00:00
|
|
|
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
|
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
re.SetBasicAuth("monitor", config.Get().SecretKey)
|
2023-09-02 15:58:37 +00:00
|
|
|
res, err := ws.upstreamHttpClient().Do(re)
|
2021-09-09 13:52:24 +00:00
|
|
|
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
|
|
|
|
}
|
2022-08-08 19:00:45 +00:00
|
|
|
_, err = io.Copy(rw, res.Body)
|
2021-09-09 13:52:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
2022-08-03 19:33:27 +00:00
|
|
|
l.WithField("listen", config.Get().Listen.Metrics).Info("Starting Metrics server")
|
|
|
|
err := http.ListenAndServe(config.Get().Listen.Metrics, m)
|
2021-09-09 13:52:24 +00:00
|
|
|
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
|
|
|
}
|
2022-08-03 19:33:27 +00:00
|
|
|
l.WithField("listen", config.Get().Listen.Metrics).Info("Stopping Metrics server")
|
2021-09-09 13:52:24 +00:00
|
|
|
}
|