- update
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
73a2861132
commit
81d369d2aa
@ -1,23 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.dtapp.net/gostring"
|
||||
)
|
||||
|
||||
// CustomTraceIdContext 自定义设置跟踪编号上下文
|
||||
func CustomTraceIdContext() context.Context {
|
||||
return context.WithValue(context.Background(), "trace_id", gostring.GetUuId())
|
||||
}
|
||||
|
||||
// SetTraceIdContext 设置跟踪编号上下文
|
||||
func SetTraceIdContext(c *gin.Context) context.Context {
|
||||
return context.WithValue(context.Background(), "trace_id", GetTraceId(c))
|
||||
}
|
||||
|
||||
// GetTraceIdContext 通过上下文获取跟踪编号
|
||||
func GetTraceIdContext(ctx context.Context) string {
|
||||
return fmt.Sprintf("%s", ctx.Value("trace_id"))
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestContext(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
t.Log(ctx)
|
||||
|
||||
ctx = context.WithValue(ctx, "k1", "测试")
|
||||
|
||||
t.Log()
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.dtapp.net/gostring"
|
||||
)
|
||||
|
||||
// SetTraceId 设置跟踪编号 https://www.jianshu.com/p/2a1a74ad3c3a
|
||||
func SetTraceId() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
requestId := c.Request.Header.Get("X-Request-Id")
|
||||
if requestId == "" {
|
||||
requestId = gostring.GetUuId()
|
||||
}
|
||||
c.Set("trace_id", requestId)
|
||||
c.Writer.Header().Set("X-Request-Id", requestId)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// GetTraceId 通过gin中间件获取跟踪编号
|
||||
func GetTraceId(c *gin.Context) string {
|
||||
return fmt.Sprintf("%s", c.MustGet("trace_id"))
|
||||
}
|
@ -1 +0,0 @@
|
||||
package golog
|
Loading…
Reference in new issue