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

request cost

上级 fa27152e
......@@ -2,6 +2,7 @@ package middleware
import (
"bytes"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io"
"net/http"
......@@ -46,6 +47,7 @@ func defaultOptions() *options {
type options struct {
maxLength int
appName string
log *zap.Logger
ignoreRoutes map[string]struct{}
traceIDFrom int // 0: ignore, 1: from context, 2: from header
......@@ -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
func WithIgnoreRoutes(routes ...string) Option {
......@@ -191,14 +198,6 @@ func Logging(opts ...Option) gin.HandlerFunc {
copyHttpReq.Body = buf.String()
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 := ""
if o.traceIDFrom == 1 {
if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist {
......@@ -225,7 +224,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests
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
fields = []zap.Field{
......@@ -241,10 +240,6 @@ func Logging(opts ...Option) gin.HandlerFunc {
if 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...)
}
......
......@@ -2,7 +2,6 @@ package middleware
import (
"context"
"fmt"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"net/http"
"strconv"
......@@ -52,11 +51,6 @@ func WithContextRequestIDKey(key string) RequestIDOption {
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
func WithHeaderRequestIDKey(key string) RequestIDOption {
......@@ -84,9 +78,7 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
o.setRequestIDKey()
return func(c *gin.Context) {
// Check for incoming header, use it if exists
requestID := c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
//costs := c.Request.Header.Values(ctxUtil.KeyCost)
// Create request id
if requestID == "" {
......@@ -99,11 +91,9 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
// Set X-Request-Id header
c.Writer.Header().Set(ctxUtil.HeaderXRequestIDKey, requestID)
startTime := time.Now()
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().Add(ctxUtil.KeyCost, fmt.Sprintf("%s-%d", o.appName, time.Since(startTime).Milliseconds()))
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论