|
|
@ -8,22 +8,22 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// CustomTraceIdContext 自定义设置跟踪编号上下文
|
|
|
|
// CustomTraceIdContext 自定义设置跟踪编号上下文
|
|
|
|
func CustomTraceIdContext() context.Context {
|
|
|
|
func CustomTraceIdContext(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(context.Background(), "trace_id", gostring.GetUuId())
|
|
|
|
return context.WithValue(ctx, "trace_id", gostring.GetUuId())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetGinTraceIdContext 设置跟踪编号上下文
|
|
|
|
// SetGinTraceIdContext 设置跟踪编号上下文
|
|
|
|
func SetGinTraceIdContext(c *gin.Context) context.Context {
|
|
|
|
func SetGinTraceIdContext(ctx context.Context, c *gin.Context) context.Context {
|
|
|
|
return context.WithValue(context.Background(), "trace_id", GetGinTraceId(c))
|
|
|
|
return context.WithValue(ctx, "trace_id", GetGinTraceId(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetTraceIdContext 通过上下文获取跟踪编号
|
|
|
|
// GetTraceIdContext 通过上下文获取跟踪编号
|
|
|
|
func GetTraceIdContext(ctx context.Context) string {
|
|
|
|
func GetTraceIdContext(ctx context.Context) string {
|
|
|
|
traceId := fmt.Sprintf("%s", ctx.Value("trace_id"))
|
|
|
|
traceId := fmt.Sprintf("%s", ctx.Value("trace_id"))
|
|
|
|
if len(traceId) <= 0 {
|
|
|
|
if traceId == Nil {
|
|
|
|
return ""
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if traceId == "%!s(<nil>)" {
|
|
|
|
if len(traceId) <= 0 {
|
|
|
|
return ""
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return traceId
|
|
|
|
return traceId
|
|
|
|