You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gotrace_id/gin_use.go

33 lines
705 B

package gotrace_id
import (
"fmt"
"github.com/gin-gonic/gin"
"go.dtapp.net/gostring"
)
// SetGinTraceId 设置跟踪编号 https://www.jianshu.com/p/2a1a74ad3c3a
func SetGinTraceId() gin.HandlerFunc {
return func(c *gin.Context) {
requestId := c.Request.Header.Get(TraceIdRequestKey)
if requestId == "" {
requestId = gostring.GetUuId()
}
c.Set(TraceIdGinKey, requestId)
c.Writer.Header().Set(TraceIdRequestKey, requestId)
c.Next()
}
}
// GetGinTraceId 通过gin中间件获取跟踪编号
func GetGinTraceId(c *gin.Context) string {
traceId := fmt.Sprintf("%s", c.MustGet(TraceIdGinKey))
if traceId == Nil {
return ""
}
if len(traceId) <= 0 {
return ""
}
return traceId
}