modelRT/middleware/trace.go

24 lines
686 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package middleware
import (
"modelRT/util"
"github.com/gin-gonic/gin"
)
// StartTrace define func of set trace info from request header
func StartTrace() gin.HandlerFunc {
return func(c *gin.Context) {
traceID := c.Request.Header.Get("traceid")
pSpanID := c.Request.Header.Get("spanid")
spanID := util.GenerateSpanID(c.Request.RemoteAddr)
if traceID == "" { // 如果traceId 为空证明是链路的发端把它设置成此次的spanId发端的spanId是root spanId
traceID = spanID // trace 标识整个请求的链路, span则标识链路中的不同服务
}
c.Set("traceid", traceID)
c.Set("spanid", spanID)
c.Set("pspanid", pSpanID)
c.Next()
}
}