forked from oliver006/redis_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
90 lines (72 loc) · 2.15 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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>Redis Exporter ` + e.buildInfo.Version + `</title></head>
<body>
<h1>Redis 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
if ck := r.URL.Query().Get("check-keys"); ck != "" {
opts.CheckKeys = ck
}
if csk := r.URL.Query().Get("check-single-keys"); csk != "" {
opts.CheckSingleKeys = csk
}
if cs := r.URL.Query().Get("check-streams"); cs != "" {
opts.CheckStreams = cs
}
if css := r.URL.Query().Get("check-single-streams"); css != "" {
opts.CheckSingleStreams = css
}
if cntk := r.URL.Query().Get("count-keys"); cntk != "" {
opts.CountKeys = cntk
}
registry := prometheus.NewRegistry()
opts.Registry = registry
_, err = NewRedisExporter(target, opts)
if err != nil {
http.Error(w, "NewRedisExporter() err: err", http.StatusBadRequest)
e.targetScrapeRequestErrors.Inc()
return
}
promhttp.HandlerFor(
registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError},
).ServeHTTP(w, r)
}