grafana/pkg/middleware/request_metrics.go

66 lines
1.3 KiB
Go
Raw Normal View History

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") {
2016-09-12 06:29:31 -05:00
countProxyRequests(status)
} else if strings.HasPrefix(req.RequestURI, "/api/") {
countApiRequests(status)
} else {
2016-09-12 06:29:31 -05:00
countPageRequests(status)
}
}
}
2016-09-12 06:29:31 -05:00
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)
}
}