提交 5198625a authored 作者: mooncake9527's avatar mooncake9527

request cost

上级 fa27152e
...@@ -2,6 +2,7 @@ package middleware ...@@ -2,6 +2,7 @@ package middleware
import ( import (
"bytes" "bytes"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity" "gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io" "io"
"net/http" "net/http"
...@@ -46,6 +47,7 @@ func defaultOptions() *options { ...@@ -46,6 +47,7 @@ func defaultOptions() *options {
type options struct { type options struct {
maxLength int maxLength int
appName string
log *zap.Logger log *zap.Logger
ignoreRoutes map[string]struct{} ignoreRoutes map[string]struct{}
traceIDFrom int // 0: ignore, 1: from context, 2: from header traceIDFrom int // 0: ignore, 1: from context, 2: from header
...@@ -75,6 +77,11 @@ func WithLog(log *zap.Logger) Option { ...@@ -75,6 +77,11 @@ func WithLog(log *zap.Logger) Option {
} }
} }
} }
func WithAppName(appName string) Option {
return func(o *options) {
o.appName = appName
}
}
// WithIgnoreRoutes no logger content routes // WithIgnoreRoutes no logger content routes
func WithIgnoreRoutes(routes ...string) Option { func WithIgnoreRoutes(routes ...string) Option {
...@@ -191,14 +198,6 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -191,14 +198,6 @@ func Logging(opts ...Option) gin.HandlerFunc {
copyHttpReq.Body = buf.String() copyHttpReq.Body = buf.String()
c.Set(ctxUtil.KeyReqBody, copyHttpReq) c.Set(ctxUtil.KeyReqBody, copyHttpReq)
//if c.Request.Method == http.MethodGet && buf.Len() == 0 {
// paramMap, err := uriUtils.URLValuePairsToMap(c.Request.URL.RawQuery)
// if err == nil {
// val, _ := json.Marshal(paramMap)
// c.Set(ctxUtil.KeyReqBody, val)
// }
//}
reqID := "" reqID := ""
if o.traceIDFrom == 1 { if o.traceIDFrom == 1 {
if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist { if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist {
...@@ -225,7 +224,7 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -225,7 +224,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests // processing requests
c.Next() c.Next()
//c.Set(ctxUtil.KeyRspBody, newWriter.body.Bytes()) c.Writer.Header().Add(ctxUtil.KeyCost, fmt.Sprintf("%s-%d", o.appName, time.Since(start).Milliseconds()))
// print return message after processing // print return message after processing
fields = []zap.Field{ fields = []zap.Field{
...@@ -241,10 +240,6 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -241,10 +240,6 @@ func Logging(opts ...Option) gin.HandlerFunc {
if reqID != "" { if reqID != "" {
fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID)) fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID))
} }
//copyHttpRsp := CopyHttpRsp{}
//copyHttpRsp.Headers = c.Writer.Header()
//copyHttpRsp.Body = newWriter.body.Bytes()
//c.Set(ctxUtil.KeyRspBody, jsonUtils.ToJsonBytes(copyHttpRsp))
o.log.Info(">>>>>>>>>rsp", fields...) o.log.Info(">>>>>>>>>rsp", fields...)
} }
......
...@@ -2,7 +2,6 @@ package middleware ...@@ -2,7 +2,6 @@ package middleware
import ( import (
"context" "context"
"fmt"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils" ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"net/http" "net/http"
"strconv" "strconv"
...@@ -52,11 +51,6 @@ func WithContextRequestIDKey(key string) RequestIDOption { ...@@ -52,11 +51,6 @@ func WithContextRequestIDKey(key string) RequestIDOption {
o.contextRequestIDKey = key o.contextRequestIDKey = key
} }
} }
func WithAppName(key string) RequestIDOption {
return func(o *requestIDOptions) {
o.appName = key
}
}
// WithHeaderRequestIDKey set header request id key, minimum length of 4 // WithHeaderRequestIDKey set header request id key, minimum length of 4
func WithHeaderRequestIDKey(key string) RequestIDOption { func WithHeaderRequestIDKey(key string) RequestIDOption {
...@@ -84,9 +78,7 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc { ...@@ -84,9 +78,7 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
o.setRequestIDKey() o.setRequestIDKey()
return func(c *gin.Context) { return func(c *gin.Context) {
// Check for incoming header, use it if exists
requestID := c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey) requestID := c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
//costs := c.Request.Header.Values(ctxUtil.KeyCost)
// Create request id // Create request id
if requestID == "" { if requestID == "" {
...@@ -99,11 +91,9 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc { ...@@ -99,11 +91,9 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
// Set X-Request-Id header // Set X-Request-Id header
c.Writer.Header().Set(ctxUtil.HeaderXRequestIDKey, requestID) c.Writer.Header().Set(ctxUtil.HeaderXRequestIDKey, requestID)
startTime := time.Now()
c.Next() c.Next()
//costs = append(costs, fmt.Sprintf("%s-%d", o.appName, time.Since(startTime).Milliseconds()))
c.Writer.Header().Set(ctxUtil.HeaderXTimestampKey, strconv.FormatInt(time.Now().Unix(), 10)) c.Writer.Header().Set(ctxUtil.HeaderXTimestampKey, strconv.FormatInt(time.Now().Unix(), 10))
c.Writer.Header().Add(ctxUtil.KeyCost, fmt.Sprintf("%s-%d", o.appName, time.Since(startTime).Milliseconds()))
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论