forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_metrics.go
65 lines (57 loc) · 1.26 KB
/
request_metrics.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
package middleware
import (
"net/http"
"strings"
"github.com/grafana/grafana/pkg/metrics"
"gopkg.in/macaron.v1"
)
func RequestMetrics() macaron.Handler {
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
rw := res.(macaron.ResponseWriter)
c.Next()
status := rw.Status()
if strings.HasPrefix(req.RequestURI, "/api/datasources/proxy") {
countProxyRequests(status)
} else if strings.HasPrefix(req.RequestURI, "/api/") {
countApiRequests(status)
} else {
countPageRequests(status)
}
}
}
func countApiRequests(status int) {
switch status {
case 200:
metrics.M_Api_Status_200.Inc(1)
case 404:
metrics.M_Api_Status_404.Inc(1)
case 500:
metrics.M_Api_Status_500.Inc(1)
default:
metrics.M_Api_Status_Unknown.Inc(1)
}
}
func countPageRequests(status int) {
switch status {
case 200:
metrics.M_Page_Status_200.Inc(1)
case 404:
metrics.M_Page_Status_404.Inc(1)
case 500:
metrics.M_Page_Status_500.Inc(1)
default:
metrics.M_Page_Status_Unknown.Inc(1)
}
}
func countProxyRequests(status int) {
switch status {
case 200:
metrics.M_Proxy_Status_200.Inc(1)
case 404:
metrics.M_Proxy_Status_404.Inc(1)
case 500:
metrics.M_Proxy_Status_500.Inc(1)
default:
metrics.M_Proxy_Status_Unknown.Inc(1)
}
}