-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_simple.go
43 lines (38 loc) · 1.01 KB
/
http_simple.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
package thetis
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
type httpMetric struct {
collector *prometheus.CounterVec
}
//GetCollector reports the status,route and method
//of every request
func (h *httpMetric) GetCollector() prometheus.Collector {
responses := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_status",
Help: "HTTP Status per request",
}, []string{"code", "route", "method"},
)
h.collector = responses
return responses
}
//CreateHandler returns a handler that will be called
//every request, and it is responsible of writing
//the desired metrics to the handler
func (h *httpMetric) CreateHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
uri := r.RequestURI
//ignoring the metrics route by default
if uri == "/metrics" {
return
}
method := r.Method
status := "0"
if sw, ok := w.(*statusWriter); ok {
status = fmt.Sprint(sw.status)
}
h.collector.WithLabelValues(status, uri, method).Inc()
}
}