-
Notifications
You must be signed in to change notification settings - Fork 7
/
promet_stats.go
66 lines (54 loc) · 1.94 KB
/
promet_stats.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
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
)
var (
commandCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "redis_traffic_stats_command_count",
Help: "The total number of redis commands",
}, []string{"command"})
commandTraffic = promauto.NewSummaryVec(prometheus.SummaryOpts{
Name: "redis_traffic_stats_command_traffic",
Help: "The total number of redis traffic bytes",
}, []string{"command"})
commandCountDetail = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "redis_traffic_stats_command_detail_count",
Help: "The total number of redis commands detail count",
}, []string{"command", "args"})
bigCommands = promauto.NewSummaryVec(prometheus.SummaryOpts{
Name: "redis_traffic_stats_big_commands",
Help: "The total number of redis traffic bytes",
}, []string{"command", "args"})
slowCommands = promauto.NewSummaryVec(prometheus.SummaryOpts{
Name: "redis_traffic_stats_slow_commands",
Help: "The total number of redis traffic nanosecond",
}, []string{"command", "args"})
)
func exportPrometheusMetrics(addr, username, password string) {
basicauth := func(f http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
user, pass, _ := r.BasicAuth()
if user != username {
http.Error(w, "Unauthorized.", 401)
log.Warn().Str("client_ip", r.RemoteAddr).Msg("unauthorized request")
return
}
if pass != password {
http.Error(w, "Unauthorized.", 401)
log.Warn().Str("client_ip", r.RemoteAddr).Msg("unauthorized request")
return
}
f.ServeHTTP(w, r)
}
}
mux := http.NewServeMux()
mux.Handle("/metrics", basicauth(promhttp.Handler()))
if err := http.ListenAndServe(addr, mux); err != nil {
panic(err)
}
}