添加一个设置trace_id的中间件

This commit is contained in:
Yun
2024-04-19 09:16:07 +08:00
parent 4f1a91faad
commit 30687b6be6
3 changed files with 20 additions and 1 deletions
+4 -1
View File
@@ -2,7 +2,10 @@ module github.com/yuninks/loggerx
go 1.20
require github.com/gin-gonic/gin v1.9.1
require (
github.com/gin-gonic/gin v1.9.1
github.com/satori/go.uuid v1.2.0
)
require (
github.com/bytedance/sonic v1.9.1 // indirect
+2
View File
@@ -44,6 +44,8 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+14
View File
@@ -0,0 +1,14 @@
package loggerx
import (
"context"
uuid "github.com/satori/go.uuid"
)
// 可以被引入的中间件
// 自动设置请求ID
func SetTradeId(ctx context.Context) context.Context {
return context.WithValue(ctx, "trade_id", uuid.NewV4().String())
}