/
http.go
70 lines (57 loc) · 1.72 KB
/
http.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
package exporter
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func (e *Exporter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.mux.ServeHTTP(w, r)
}
func (e *Exporter) healthHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`ok`))
}
func (e *Exporter) indexHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Kvrocks Exporter ` + e.buildInfo.Version + `</title></head>
<body>
<h1>Kvrocks Exporter ` + e.buildInfo.Version + `</h1>
<p><a href='` + e.options.MetricsPath + `'>Metrics</a></p>
</body>
</html>
`))
}
func (e *Exporter) scrapeHandler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "'target' parameter must be specified", http.StatusBadRequest)
e.targetScrapeRequestErrors.Inc()
return
}
if !strings.Contains(target, "://") {
target = "redis://" + target
}
u, err := url.Parse(target)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid 'target' parameter, parse err: %ck ", err), http.StatusBadRequest)
e.targetScrapeRequestErrors.Inc()
return
}
// get rid of username/password info in "target" so users don't send them in plain text via http
u.User = nil
target = u.String()
opts := e.options
registry := prometheus.NewRegistry()
opts.Registry = registry
_, err = NewKvrocksExporter(target, opts)
if err != nil {
http.Error(w, "NewKvrocksExporter() err: err", http.StatusBadRequest)
e.targetScrapeRequestErrors.Inc()
return
}
promhttp.HandlerFor(
registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError},
).ServeHTTP(w, r)
}