1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package server

import (
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"net/http"
	"rss_parrot/shared"
	"strings"
)

type metricsHandlerGroup struct {
	cfg             *shared.Config
	logger          shared.ILogger
	promHttpHandler http.Handler
}

func NewMetricsHandlerGroup(
	cfg *shared.Config,
	logger shared.ILogger,
) IHandlerGroup {
	res := metricsHandlerGroup{
		cfg:             cfg,
		logger:          logger,
		promHttpHandler: promhttp.Handler(),
	}
	return &res
}

func (hg *metricsHandlerGroup) Prefix() string {
	return "/"
}

func (hg *metricsHandlerGroup) GroupDefs() []handlerDef {
	return []handlerDef{
		{"GET", "/metrics", func(w http.ResponseWriter, r *http.Request) { hg.getMetrics(w, r) }},
	}
}

func (hg *metricsHandlerGroup) AuthMW() func(next http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return hg.authMW(next)
	}
}

func (hg *metricsHandlerGroup) authMW(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		authSecret := ""
		var authHeader = r.Header.Get(metricsAuthHeader)
		if strings.HasPrefix(authHeader, "Bearer ") {
			authSecret = strings.TrimPrefix(authHeader, "Bearer ")
		}
		if authSecret == "" || authSecret != hg.cfg.Secrets.MetricsAuth {
			hg.logger.Warnf("Metrics scrape request with missing or invalid Authorization header '%s' s", authSecret)
			writeErrorResponse(w, badAuthorization, http.StatusUnauthorized)
			return
		}
		next.ServeHTTP(w, r)
	})
}

func (hg *metricsHandlerGroup) getMetrics(w http.ResponseWriter, r *http.Request) {
	hg.logger.Infof("Handling metrics GET: %s", r.URL.Path)
	hg.promHttpHandler.ServeHTTP(w, r)
}