-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
86 lines (75 loc) · 2.35 KB
/
routes.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
package main
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "kv_http_duration_seconds",
Help: "Duration of HTTP requests.",
}, []string{"path", "method", "code"})
keys = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "total_keys_kv",
Help: "total keys in kv store",
},
[]string{"kv"},
)
totalRequests = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of get requests.",
},
[]string{"path"},
)
responseStatus = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "response_status",
Help: "Status of HTTP response",
},
[]string{"status", "path"},
)
)
func (app *application) routes() http.Handler {
router := mux.NewRouter()
router.Use(app.prometheusMiddleware)
router.HandleFunc("/healthcheck", app.healthcheckHandler).Methods("GET")
router.HandleFunc("/get/{key}", app.getKey).Methods("GET")
router.HandleFunc("/set", app.setKey).Methods("POST")
router.HandleFunc("/search", app.search).Methods("GET")
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
router.HandleFunc("/metrics", promhttp.Handler().ServeHTTP)
return router
}
type responseWriter struct {
http.ResponseWriter
statusCode int
}
func (rw *responseWriter) WriteHeader(code int) {
rw.statusCode = code
rw.ResponseWriter.WriteHeader(code)
}
func NewResponseWriter(w http.ResponseWriter) *responseWriter {
return &responseWriter{w, http.StatusOK}
}
func (app *application) prometheusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
rw := NewResponseWriter(w)
next.ServeHTTP(rw, r)
statusCode := rw.statusCode
responseStatus.WithLabelValues(strconv.Itoa(statusCode), path).Inc()
// TODO: best place to put this?
totalRequests.WithLabelValues(path).Inc()
keys.WithLabelValues("redis").Set(float64(app.db.TotalKeys()))
timer := prometheus.NewTimer(httpDuration.WithLabelValues(path, r.Method, strconv.Itoa(statusCode)))
timer.ObserveDuration()
})
}