ctx_trace.go 580 B

123456789101112131415161718
  1. package global
  2. import (
  3. "context"
  4. uuid "github.com/satori/go.uuid"
  5. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/consts"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/log"
  7. )
  8. func MakeTraceCtx(ctx context.Context, keysAndValues ...interface{}) context.Context {
  9. requestId := uuid.NewV4().String()
  10. ctx = context.WithValue(ctx, consts.TraceIDHeader, requestId)
  11. keysAndValues = append([]interface{}{consts.TraceIDHeader, requestId}, keysAndValues...)
  12. ctxLog := log.WithValues(keysAndValues...)
  13. ctx = context.WithValue(ctx, log.GetContextKey(), ctxLog)
  14. return ctx
  15. }