adds metric middlware to route register

This commit is contained in:
bergquist 2017-09-13 15:18:52 +02:00 committed by Carl Bergquist
parent 6372e22180
commit 4bc6ecb241
5 changed files with 10 additions and 21 deletions

View File

@ -170,7 +170,6 @@ func (hs *HttpServer) newMacaron() *macaron.Macaron {
m.Use(hs.metricsEndpoint) m.Use(hs.metricsEndpoint)
m.Use(middleware.GetContextHandler()) m.Use(middleware.GetContextHandler())
m.Use(middleware.Sessioner(&setting.SessionOptions)) m.Use(middleware.Sessioner(&setting.SessionOptions))
m.Use(middleware.RequestMetrics())
m.Use(middleware.OrgRedirect()) m.Use(middleware.OrgRedirect())
// needs to be after context handler // needs to be after context handler

View File

@ -3,6 +3,7 @@ package api
import ( import (
"net/http" "net/http"
"github.com/grafana/grafana/pkg/middleware"
macaron "gopkg.in/macaron.v1" macaron "gopkg.in/macaron.v1"
) )
@ -68,13 +69,15 @@ func (rr *routeRegister) Register(router Router) *macaron.Router {
} }
func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handler) { func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handler) {
//inject metrics
//inject tracing //inject tracing
h := append(rr.subfixHandlers, handlers...)
h = append([]macaron.Handler{middleware.RequestMetrics(pattern)}, h...)
rr.routes = append(rr.routes, route{ rr.routes = append(rr.routes, route{
method: method, method: method,
pattern: rr.prefix + pattern, pattern: rr.prefix + pattern,
handlers: append(rr.subfixHandlers, handlers...), handlers: h,
}) })
} }

View File

@ -54,7 +54,6 @@ const (
) )
var metricCategoryPrefix []string = []string{"proxy_", "api_", "page_", "alerting_", "aws_", "db_", "stat_", "go_", "process_"} var metricCategoryPrefix []string = []string{"proxy_", "api_", "page_", "alerting_", "aws_", "db_", "stat_", "go_", "process_"}
var ignorePrefix []string = []string{"http_"}
// Config defines the Graphite bridge config. // Config defines the Graphite bridge config.
type Config struct { type Config struct {
@ -206,18 +205,6 @@ func (b *Bridge) writeMetrics(w io.Writer, mfs []*dto.MetricFamily, prefix strin
return err return err
} }
ignoreThisMetric := false
for _, v := range ignorePrefix {
if strings.HasPrefix(mf.GetName(), v) {
ignoreThisMetric = true
break
}
}
if ignoreThisMetric {
continue
}
buf := bufio.NewWriter(w) buf := bufio.NewWriter(w)
for _, s := range vec { for _, s := range vec {
if err := writePrefix(buf, prefix); err != nil { if err := writePrefix(buf, prefix); err != nil {

View File

@ -91,7 +91,7 @@ func init() {
Name: "http_request_total", Name: "http_request_total",
Help: "http request counter", Help: "http request counter",
}, },
[]string{"code", "method"}, []string{"handler", "statuscode", "method"},
) )
M_Http_Request_Summary = prometheus.NewSummaryVec( M_Http_Request_Summary = prometheus.NewSummaryVec(
@ -99,7 +99,7 @@ func init() {
Name: "http_request_duration", Name: "http_request_duration",
Help: "http request summary", Help: "http request summary",
}, },
[]string{"code", "method"}, []string{"handler", "statuscode", "method"},
) )
M_Api_User_SignUpStarted = prometheus.NewCounter(prometheus.CounterOpts{ M_Api_User_SignUpStarted = prometheus.NewCounter(prometheus.CounterOpts{

View File

@ -10,7 +10,7 @@ import (
"gopkg.in/macaron.v1" "gopkg.in/macaron.v1"
) )
func RequestMetrics() macaron.Handler { func RequestMetrics(handler string) macaron.Handler {
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) { return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
rw := res.(macaron.ResponseWriter) rw := res.(macaron.ResponseWriter)
now := time.Now() now := time.Now()
@ -20,8 +20,8 @@ func RequestMetrics() macaron.Handler {
code := sanitizeCode(status) code := sanitizeCode(status)
method := sanitizeMethod(req.Method) method := sanitizeMethod(req.Method)
metrics.M_Http_Request_Total.WithLabelValues(code, method).Inc() metrics.M_Http_Request_Total.WithLabelValues(handler, code, method).Inc()
metrics.M_Http_Request_Summary.WithLabelValues(code, method).Observe(time.Since(now).Seconds()) metrics.M_Http_Request_Summary.WithLabelValues(handler, code, method).Observe(time.Since(now).Seconds())
if strings.HasPrefix(req.RequestURI, "/api/datasources/proxy") { if strings.HasPrefix(req.RequestURI, "/api/datasources/proxy") {
countProxyRequests(status) countProxyRequests(status)