github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/utils/context.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/projecteru2/core/types"
     7  
     8  	"google.golang.org/grpc/peer"
     9  )
    10  
    11  // NewInheritCtx new a todo context and get the previous values
    12  func NewInheritCtx(ctx context.Context) context.Context {
    13  	return InheritTracingInfo(ctx, context.TODO())
    14  }
    15  
    16  // InheritTracingInfo pass through the tracing info: peer, tracing id
    17  func InheritTracingInfo(ctx, newCtx context.Context) context.Context {
    18  	rCtx := newCtx
    19  	if ctx == nil {
    20  		return rCtx
    21  	}
    22  
    23  	p, ok := peer.FromContext(ctx)
    24  	if ok {
    25  		rCtx = peer.NewContext(rCtx, p)
    26  	}
    27  
    28  	if traceID := ctx.Value(types.TracingID); traceID != nil {
    29  		if tid, ok := traceID.(string); ok {
    30  			rCtx = context.WithValue(rCtx, types.TracingID, tid)
    31  		}
    32  	}
    33  
    34  	return rCtx
    35  }