-
Notifications
You must be signed in to change notification settings - Fork 6
/
frontend.go
76 lines (63 loc) · 1.94 KB
/
frontend.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
package frontend
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
_ "github.com/q3k/statusz"
)
// ProberRegistry is the interface to a prober registry
type ProberRegistry interface {
GetCollectors() []prometheus.Collector
}
// Config is a fronend config
type Config struct {
Version string
MetricsPath string
ListenAddress string
}
// Frontend represents an HTTP prometheus interface
type Frontend struct {
cfg *Config
proberReg ProberRegistry
}
// New creates a new HTTP frontend
func New(cfg *Config, proberReg ProberRegistry) *Frontend {
return &Frontend{
cfg: cfg,
proberReg: proberReg,
}
}
// Start starts the frontend
func (fe *Frontend) Start() {
log.Infof("Starting Matroschka Prober (Version: %s)\n", fe.cfg.Version)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Matroschka Prober (Version ` + fe.cfg.Version + `)</title></head>
<body>
<h1>Matroschka Prober</h1>
<p><a href="` + fe.cfg.MetricsPath + `">Metrics</a></p>
<h2>More information:</h2>
<p><a href="https://github.com/exaring/matroschka-prober">github.com/exaring/matroschka-prober</a></p>
</body>
</html>`))
})
http.HandleFunc(fe.cfg.MetricsPath, fe.handleMetricsRequest)
log.Infof("Listening for %s on %s\n", fe.cfg.MetricsPath, fe.cfg.ListenAddress)
log.Fatal(http.ListenAndServe(fe.cfg.ListenAddress, nil))
}
func (fe *Frontend) handleMetricsRequest(w http.ResponseWriter, r *http.Request) {
reg := prometheus.NewRegistry()
for _, p := range fe.proberReg.GetCollectors() {
reg.MustRegister(p)
}
promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: errLogger{log.New()},
ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, r)
}
type errLogger struct {
*log.Logger
}
func (l errLogger) Println(args ...interface{}) {
l.Errorln(args...)
}