From a8d0809d5dbcf976da781a0ddef2d56614be850f Mon Sep 17 00:00:00 2001 From: hujiebin Date: Wed, 1 Mar 2023 11:09:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=A1=A5=E4=B8=8AtraceId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- route/middleHandle.go | 2 +- route/utils.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/route/middleHandle.go b/route/middleHandle.go index ace0016..7f4ec55 100755 --- a/route/middleHandle.go +++ b/route/middleHandle.go @@ -90,7 +90,7 @@ func LoggerHandle(c *gin.Context) { start := time.Now() clientIp := c.ClientIP() method := c.Request.Method - traceId := genTraceId() + traceId := genTraceId(c) reqUri := c.Request.RequestURI c.Set(mycontext.TRACEID, traceId) // diff --git a/route/utils.go b/route/utils.go index 2a736b4..a6544bd 100755 --- a/route/utils.go +++ b/route/utils.go @@ -1,11 +1,16 @@ package route import ( + "git.hilo.cn/hilo-common/mycontext" + "github.com/gin-gonic/gin" uuid "github.com/satori/go.uuid" "strings" ) -func genTraceId() string { +func genTraceId(c *gin.Context) string { + if trace := c.GetHeader(mycontext.TRACEID); len(trace) > 0 { + return trace + } traceId := strings.Replace(uuid.NewV4().String(), "-", "", -1) traceId = traceId[:len(traceId)/2] return traceId -- 2.22.0