options method buf fixed(record metrics update)

This commit is contained in:
bryanqiu 2022-12-22 09:53:14 +08:00
parent a098d141c2
commit 426a77f4cf
3 changed files with 45 additions and 8 deletions

26
api.go
View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"fmt"
"reflect" "reflect"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -12,7 +13,7 @@ import (
var ( var (
validate = validator.New() validate = validator.New()
requestLogidGetter = defaultLogidGetter requestLogidGetter = defaultLogidGetter
requestHandlerMapper = map[string]Handler{} requestHandlerMapper = map[string]gin.HandlerFunc{}
errCodeUnknownError = -10000 errCodeUnknownError = -10000
errCodeParameterError = -20000 errCodeParameterError = -20000
errCodeUnimplementApi = -20401 errCodeUnimplementApi = -20401
@ -56,7 +57,7 @@ func (e *Engine) POST(uri string, handler Handler) {
// handlerWrapper // handlerWrapper
func handlerWrapper(handler Handler) gin.HandlerFunc { func handlerWrapper(handler Handler) gin.HandlerFunc {
handlerFunc := handler.HandlerFunc() handlerFunc := handler.HandlerFunc()
return func(c *gin.Context) { wrappedHandlerFunc := func(c *gin.Context) {
// Case 1. get request handler // Case 1. get request handler
if c.Request == nil && c.Keys != nil { if c.Request == nil && c.Keys != nil {
c.Keys["handler"] = handler c.Keys["handler"] = handler
@ -78,6 +79,9 @@ func handlerWrapper(handler Handler) gin.HandlerFunc {
return return
} }
} }
hkey := fmt.Sprintf("%v", wrappedHandlerFunc)
requestHandlerMapper[hkey] = wrappedHandlerFunc
return wrappedHandlerFunc
} }
// handlerWrapper // handlerWrapper
@ -114,6 +118,9 @@ func middlewareMyApiEngine() gin.HandlerFunc {
// Step 3. do request // Step 3. do request
c.Next() c.Next()
// Step 4. recorde metrics
cc.recordMetrics()
} }
} }
@ -141,8 +148,17 @@ func CC(c *gin.Context) *Context {
} }
func ApiHandler(c *gin.Context) Handler { func ApiHandler(c *gin.Context) Handler {
newc := &gin.Context{Request: nil, Keys: map[string]any{}} h := c.Handler()
c.Handler()(newc) hkey := fmt.Sprintf("%v", h)
if _, ok := requestHandlerMapper[hkey]; !ok {
return nil
}
newc := &gin.Context{
Request: nil,
Keys: map[string]any{},
}
h(newc)
return newc.Keys["handler"].(Handler) return newc.Keys["handler"].(Handler)
} }
@ -157,7 +173,7 @@ func RecordMetrics(api, errcode, appid string, costms float64) {
summary.Observe(costms) summary.Observe(costms)
} }
func DumpHandler(c *Context) error { func DumpHandlerFunc(c *Context) error {
errmsg := "api not implemented" errmsg := "api not implemented"
errcode := errCodeUnimplementApi errcode := errCodeUnimplementApi
return c.RESULT_ERROR(errcode, errmsg) return c.RESULT_ERROR(errcode, errmsg)

View File

@ -150,8 +150,6 @@ func (c *Context) RESULT(output interface{}) error {
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b)) noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
log.NoticefWithDepth(calldepth, noticeStr) log.NoticefWithDepth(calldepth, noticeStr)
//////// metrics //////////////////////////////////
c.recordMetrics()
}(&output) }(&output)
if _, ok := t.FieldByName("ErrCode"); !ok { if _, ok := t.FieldByName("ErrCode"); !ok {
@ -220,8 +218,11 @@ func (c *Context) recordMetrics() {
metricApiSummary == nil { metricApiSummary == nil {
return return
} }
var h = c.ApiHandler()
if h == nil {
h = unimplementHandler{c.Context}
}
var ( var (
h = c.ApiHandler()
api = h.ApiName() api = h.ApiName()
appid = c.AppId appid = c.AppId
costus = time.Now().Sub(c.starttime).Microseconds() costus = time.Now().Sub(c.starttime).Microseconds()

View File

@ -1,5 +1,12 @@
package api package api
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
)
// /////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////
// IRouter & IRoutes in gin, we redefine it simple // IRouter & IRoutes in gin, we redefine it simple
// /////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////
@ -42,3 +49,16 @@ type HandlerFunc func(*Context) error
type IRoutes interface { type IRoutes interface {
POST(uri string, handler Handler) POST(uri string, handler Handler)
} }
type unimplementHandler struct {
c *gin.Context
}
func (u unimplementHandler) ApiName() string {
req := u.c.Request
return strings.ToLower(fmt.Sprintf("[%s]%s", req.Method, req.RequestURI))
}
func (u unimplementHandler) HandlerFunc() HandlerFunc {
return DumpHandlerFunc
}