mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
adds metric middlware to route register
This commit is contained in:
parent
6372e22180
commit
4bc6ecb241
@ -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
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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{
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user