Commit e7b56c63 by bergquist

tech(metrics): move all request counters into one middleware

parent 583790e6
......@@ -4,7 +4,6 @@ import (
"encoding/json"
"net/http"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
......@@ -88,10 +87,8 @@ func ApiError(status int, message string, err error) *NormalResponse {
switch status {
case 404:
metrics.M_Api_Status_404.Inc(1)
data["message"] = "Not Found"
case 500:
metrics.M_Api_Status_500.Inc(1)
data["message"] = "Internal Server Error"
}
......
......@@ -53,6 +53,7 @@ func newMacaron() *macaron.Macaron {
m.Use(middleware.GetContextHandler())
m.Use(middleware.Sessioner(&setting.SessionOptions))
m.Use(middleware.RequestMetrics())
return m
}
......
......@@ -15,6 +15,7 @@ var (
M_Page_Status_404 Counter
M_Api_Status_500 Counter
M_Api_Status_404 Counter
M_Api_Status_200 Counter
M_Api_User_SignUpStarted Counter
M_Api_User_SignUpCompleted Counter
M_Api_User_SignUpInvite Counter
......@@ -57,6 +58,7 @@ func initMetricVars(settings *MetricSettings) {
M_Api_Status_500 = RegCounter("api.resp_status", "code", "500")
M_Api_Status_404 = RegCounter("api.resp_status", "code", "404")
M_Api_Status_200 = RegCounter("api.resp_status", "code", "200")
M_Api_User_SignUpStarted = RegCounter("api.user.signup_started")
M_Api_User_SignUpCompleted = RegCounter("api.user.signup_completed")
......
......@@ -208,15 +208,6 @@ func (ctx *Context) Handle(status int, title string, err error) {
}
}
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)
}
ctx.Data["Title"] = title
ctx.HTML(status, strconv.Itoa(status))
}
......@@ -243,10 +234,8 @@ func (ctx *Context) JsonApiErr(status int, message string, err error) {
switch status {
case 404:
metrics.M_Api_Status_404.Inc(1)
resp["message"] = "Not Found"
case 500:
metrics.M_Api_Status_500.Inc(1)
resp["message"] = "Internal Server Error"
}
......
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.URL.Path, "/api/") {
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)
}
} else {
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)
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment