3 Star 12 Fork 3

os-lee/easy-paas

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
trace.go 699 Bytes
一键复制 编辑 原始数据 按行查看 历史
lee 提交于 2024-07-21 22:19 +08:00 . 页面添加链路追踪功能
package logger
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
const (
TraceKey = "traceId"
ReqIn = "req_in"
ReqOut = "req_out"
)
type Trace struct {
TraceId string
}
func GetTraceFromHeader(c *gin.Context) *Trace {
if traceId := c.Request.Header.Get(TraceKey); traceId != "" {
return &Trace{TraceId: traceId}
}
trace := new(Trace)
trace.TraceId = fmt.Sprint(uuid.New())
return trace
}
func GetTraceFromContext(c context.Context) *Trace {
if value := c.Value(TraceKey); value != nil {
return value.(*Trace)
}
Log.Warnln("从请求context中获取trace为空!")
trace := new(Trace)
trace.TraceId = fmt.Sprint(uuid.New())
return trace
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/os-lee/easy-paas.git
git@gitee.com:os-lee/easy-paas.git
os-lee
easy-paas
easy-paas
6cf1638f64c0

搜索帮助