提交 6c3a1bfa authored 作者: mooncake9527's avatar mooncake9527

record api cost

上级 7474de31
...@@ -18,6 +18,8 @@ const ( ...@@ -18,6 +18,8 @@ const (
GinContextKey = "ginContext" GinContextKey = "ginContext"
HeaderXTimestampKey = "Timestamp" HeaderXTimestampKey = "Timestamp"
KeyReqBody = "reqBody" KeyReqBody = "reqBody"
KeyAppName = "appName"
KeyApiStartTime = "apiStartTime"
KeyRspBody = "rspBody" KeyRspBody = "rspBody"
KeyClientIP = "clientIP" KeyClientIP = "clientIP"
KeyUID = "userId" KeyUID = "userId"
...@@ -245,7 +247,7 @@ func AddApiCost(c *gin.Context, appName string, start time.Time) { ...@@ -245,7 +247,7 @@ func AddApiCost(c *gin.Context, appName string, start time.Time) {
if cost == "" { if cost == "" {
cost = currentCost cost = currentCost
} else { } else {
cost = fmt.Sprintf("%s,%s", cost, currentCost) cost = fmt.Sprintf("%s,%s", currentCost, cost)
} }
c.Writer.Header().Set(KeyCost, cost) c.Writer.Header().Set(KeyCost, cost)
} }
......
...@@ -196,6 +196,7 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -196,6 +196,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
copyHttpReq.Headers = c.Request.Header copyHttpReq.Headers = c.Request.Header
copyHttpReq.Body = buf.String() copyHttpReq.Body = buf.String()
c.Set(ctxUtils.KeyReqBody, copyHttpReq) c.Set(ctxUtils.KeyReqBody, copyHttpReq)
c.Set(ctxUtils.KeyAppName, o.appName)
reqID := "" reqID := ""
if o.traceIDFrom == 1 { if o.traceIDFrom == 1 {
...@@ -223,8 +224,6 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -223,8 +224,6 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests // processing requests
c.Next() c.Next()
ctxUtils.AddApiCost(c, o.appName, start)
//print return message after processing //print return message after processing
fields = []zap.Field{ fields = []zap.Field{
zap.Int("code", c.Writer.Status()), zap.Int("code", c.Writer.Status()),
......
...@@ -2,13 +2,11 @@ package middleware ...@@ -2,13 +2,11 @@ package middleware
import ( import (
"context" "context"
"github.com/gin-gonic/gin"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils" ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"go.uber.org/zap"
"net/http" "net/http"
"strconv"
"time" "time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
) )
// RequestIDOption set the request id options. // RequestIDOption set the request id options.
...@@ -86,14 +84,15 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc { ...@@ -86,14 +84,15 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
c.Request.Header.Set(ctxUtil.HeaderXRequestIDKey, requestID) c.Request.Header.Set(ctxUtil.HeaderXRequestIDKey, requestID)
} }
st := time.Now()
// Expose it for use in the application // Expose it for use in the application
c.Set(ctxUtil.ContextTraceIDKey, requestID) c.Set(ctxUtil.ContextTraceIDKey, requestID)
c.Set(ctxUtil.KeyApiStartTime, st)
// Set X-Request-Id header // Set X-Request-Id header
c.Writer.Header().Set(ctxUtil.HeaderXRequestIDKey, requestID) c.Writer.Header().Set(ctxUtil.HeaderXRequestIDKey, requestID)
c.Next() c.Next()
c.Writer.Header().Set(ctxUtil.HeaderXTimestampKey, strconv.FormatInt(time.Now().Unix(), 10))
} }
} }
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity" "gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"net/http" "net/http"
"strconv"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
...@@ -145,6 +146,11 @@ func Out(c *gin.Context, err *errcode.Error, data ...interface{}) { ...@@ -145,6 +146,11 @@ func Out(c *gin.Context, err *errcode.Error, data ...interface{}) {
// status code flat 200, custom error codes in data.code // status code flat 200, custom error codes in data.code
func respJSONWith200(c *gin.Context, code int, msg string, data ...interface{}) { func respJSONWith200(c *gin.Context, code int, msg string, data ...interface{}) {
c.Writer.Header().Set(ctxUtils.HeaderXTimestampKey, strconv.FormatInt(time.Now().Unix(), 10))
appName := c.GetString(ctxUtils.KeyAppName)
stTime := c.GetTime(ctxUtils.KeyApiStartTime)
ctxUtils.AddApiCost(c, appName, stTime)
if len(data) > 0 { if len(data) > 0 {
writeJSON(c, http.StatusOK, newResp(c, code, msg, data[0])) writeJSON(c, http.StatusOK, newResp(c, code, msg, data[0]))
return return
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论