浏览代码

adds metric middlware to route register

bergquist 8 年之前
父节点
当前提交
4bc6ecb241

+ 0 - 1
pkg/api/http_server.go

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

+ 5 - 2
pkg/api/route_register.go

@@ -3,6 +3,7 @@ package api
 import (
 	"net/http"
 
+	"github.com/grafana/grafana/pkg/middleware"
 	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) {
-	//inject metrics
 	//inject tracing
 
+	h := append(rr.subfixHandlers, handlers...)
+	h = append([]macaron.Handler{middleware.RequestMetrics(pattern)}, h...)
+
 	rr.routes = append(rr.routes, route{
 		method:   method,
 		pattern:  rr.prefix + pattern,
-		handlers: append(rr.subfixHandlers, handlers...),
+		handlers: h,
 	})
 }
 

+ 0 - 13
pkg/metrics/graphitebridge/graphite.go

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

+ 2 - 2
pkg/metrics/metrics.go

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

+ 3 - 3
pkg/middleware/request_metrics.go

@@ -10,7 +10,7 @@ import (
 	"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) {
 		rw := res.(macaron.ResponseWriter)
 		now := time.Now()
@@ -20,8 +20,8 @@ func RequestMetrics() macaron.Handler {
 
 		code := sanitizeCode(status)
 		method := sanitizeMethod(req.Method)
-		metrics.M_Http_Request_Total.WithLabelValues(code, method).Inc()
-		metrics.M_Http_Request_Summary.WithLabelValues(code, method).Observe(time.Since(now).Seconds())
+		metrics.M_Http_Request_Total.WithLabelValues(handler, code, method).Inc()
+		metrics.M_Http_Request_Summary.WithLabelValues(handler, code, method).Observe(time.Since(now).Seconds())
 
 		if strings.HasPrefix(req.RequestURI, "/api/datasources/proxy") {
 			countProxyRequests(status)