Add prometheous metrics for each api handler (#12254)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
05fae599b8
Коммит
e236eb74fa
@@ -18,6 +18,7 @@ func (api *API) ApiHandler(h func(*Context, http.ResponseWriter, *http.Request))
|
|||||||
handler := &web.Handler{
|
handler := &web.Handler{
|
||||||
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: web.GetHandlerName(h),
|
||||||
RequireSession: false,
|
RequireSession: false,
|
||||||
TrustRequester: false,
|
TrustRequester: false,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
@@ -35,6 +36,7 @@ func (api *API) ApiSessionRequired(h func(*Context, http.ResponseWriter, *http.R
|
|||||||
handler := &web.Handler{
|
handler := &web.Handler{
|
||||||
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: web.GetHandlerName(h),
|
||||||
RequireSession: true,
|
RequireSession: true,
|
||||||
TrustRequester: false,
|
TrustRequester: false,
|
||||||
RequireMfa: true,
|
RequireMfa: true,
|
||||||
@@ -54,6 +56,7 @@ func (api *API) ApiSessionRequiredMfa(h func(*Context, http.ResponseWriter, *htt
|
|||||||
handler := &web.Handler{
|
handler := &web.Handler{
|
||||||
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: web.GetHandlerName(h),
|
||||||
RequireSession: true,
|
RequireSession: true,
|
||||||
TrustRequester: false,
|
TrustRequester: false,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
@@ -73,6 +76,7 @@ func (api *API) ApiHandlerTrustRequester(h func(*Context, http.ResponseWriter, *
|
|||||||
handler := &web.Handler{
|
handler := &web.Handler{
|
||||||
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: web.GetHandlerName(h),
|
||||||
RequireSession: false,
|
RequireSession: false,
|
||||||
TrustRequester: true,
|
TrustRequester: true,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
@@ -91,6 +95,7 @@ func (api *API) ApiSessionRequiredTrustRequester(h func(*Context, http.ResponseW
|
|||||||
handler := &web.Handler{
|
handler := &web.Handler{
|
||||||
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
GetGlobalAppOptions: api.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: web.GetHandlerName(h),
|
||||||
RequireSession: true,
|
RequireSession: true,
|
||||||
TrustRequester: true,
|
TrustRequester: true,
|
||||||
RequireMfa: true,
|
RequireMfa: true,
|
||||||
|
|||||||
@@ -44,4 +44,5 @@ type MetricsInterface interface {
|
|||||||
IncrementPostsSearchCounter()
|
IncrementPostsSearchCounter()
|
||||||
ObservePostsSearchDuration(elapsed float64)
|
ObservePostsSearchDuration(elapsed float64)
|
||||||
ObserveStoreMethodDuration(method string, success string, elapsed float64)
|
ObserveStoreMethodDuration(method string, success string, elapsed float64)
|
||||||
|
ObserveApiEndpointDuration(endpoint string, elapsed float64)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ package web
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
@@ -16,10 +19,20 @@ import (
|
|||||||
"github.com/mattermost/mattermost-server/utils"
|
"github.com/mattermost/mattermost-server/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetHandlerName(h func(*Context, http.ResponseWriter, *http.Request)) string {
|
||||||
|
handlerName := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
||||||
|
pos := strings.LastIndex(handlerName, ".")
|
||||||
|
if pos != -1 && len(handlerName) > pos {
|
||||||
|
handlerName = handlerName[pos+1:]
|
||||||
|
}
|
||||||
|
return handlerName
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Web) NewHandler(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
|
func (w *Web) NewHandler(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
|
||||||
return &Handler{
|
return &Handler{
|
||||||
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: GetHandlerName(h),
|
||||||
RequireSession: false,
|
RequireSession: false,
|
||||||
TrustRequester: false,
|
TrustRequester: false,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
@@ -35,6 +48,7 @@ func (w *Web) NewStaticHandler(h func(*Context, http.ResponseWriter, *http.Reque
|
|||||||
return &Handler{
|
return &Handler{
|
||||||
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: GetHandlerName(h),
|
||||||
RequireSession: false,
|
RequireSession: false,
|
||||||
TrustRequester: false,
|
TrustRequester: false,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
@@ -47,6 +61,7 @@ func (w *Web) NewStaticHandler(h func(*Context, http.ResponseWriter, *http.Reque
|
|||||||
type Handler struct {
|
type Handler struct {
|
||||||
GetGlobalAppOptions app.AppOptionCreator
|
GetGlobalAppOptions app.AppOptionCreator
|
||||||
HandleFunc func(*Context, http.ResponseWriter, *http.Request)
|
HandleFunc func(*Context, http.ResponseWriter, *http.Request)
|
||||||
|
HandlerName string
|
||||||
RequireSession bool
|
RequireSession bool
|
||||||
TrustRequester bool
|
TrustRequester bool
|
||||||
RequireMfa bool
|
RequireMfa bool
|
||||||
@@ -192,6 +207,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if r.URL.Path != model.API_URL_SUFFIX+"/websocket" {
|
if r.URL.Path != model.API_URL_SUFFIX+"/websocket" {
|
||||||
elapsed := float64(time.Since(now)) / float64(time.Second)
|
elapsed := float64(time.Since(now)) / float64(time.Second)
|
||||||
c.App.Metrics.ObserveHttpRequestDuration(elapsed)
|
c.App.Metrics.ObserveHttpRequestDuration(elapsed)
|
||||||
|
c.App.Metrics.ObserveApiEndpointDuration(h.HandlerName, elapsed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -249,6 +265,7 @@ func (w *Web) ApiHandler(h func(*Context, http.ResponseWriter, *http.Request)) h
|
|||||||
handler := &Handler{
|
handler := &Handler{
|
||||||
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: GetHandlerName(h),
|
||||||
RequireSession: false,
|
RequireSession: false,
|
||||||
TrustRequester: false,
|
TrustRequester: false,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
@@ -267,6 +284,7 @@ func (w *Web) ApiHandlerTrustRequester(h func(*Context, http.ResponseWriter, *ht
|
|||||||
handler := &Handler{
|
handler := &Handler{
|
||||||
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: GetHandlerName(h),
|
||||||
RequireSession: false,
|
RequireSession: false,
|
||||||
TrustRequester: true,
|
TrustRequester: true,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
@@ -284,6 +302,7 @@ func (w *Web) ApiSessionRequired(h func(*Context, http.ResponseWriter, *http.Req
|
|||||||
handler := &Handler{
|
handler := &Handler{
|
||||||
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: GetHandlerName(h),
|
||||||
RequireSession: true,
|
RequireSession: true,
|
||||||
TrustRequester: false,
|
TrustRequester: false,
|
||||||
RequireMfa: true,
|
RequireMfa: true,
|
||||||
@@ -302,6 +321,7 @@ func (w *Web) apiHandlerTrustRequester(h func(*Context, http.ResponseWriter, *ht
|
|||||||
handler := &Handler{
|
handler := &Handler{
|
||||||
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
GetGlobalAppOptions: w.GetGlobalAppOptions,
|
||||||
HandleFunc: h,
|
HandleFunc: h,
|
||||||
|
HandlerName: GetHandlerName(h),
|
||||||
RequireSession: false,
|
RequireSession: false,
|
||||||
TrustRequester: true,
|
TrustRequester: true,
|
||||||
RequireMfa: false,
|
RequireMfa: false,
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user