github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/meta/trace_id.go (about)

     1  package meta
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/google/uuid"
     7  	"google.golang.org/grpc/metadata"
     8  
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    10  )
    11  
    12  type newTraceIDOpts struct {
    13  	newRandom func() (uuid.UUID, error)
    14  }
    15  
    16  func TraceID(ctx context.Context, opts ...func(opts *newTraceIDOpts)) (context.Context, string, error) {
    17  	if id, has := traceID(ctx); has {
    18  		return ctx, id, nil
    19  	}
    20  	options := newTraceIDOpts{newRandom: uuid.NewRandom}
    21  	for _, opt := range opts {
    22  		if opt != nil {
    23  			opt(&options)
    24  		}
    25  	}
    26  	uuid, err := options.newRandom()
    27  	if err != nil {
    28  		return ctx, "", xerrors.WithStackTrace(err)
    29  	}
    30  	id := uuid.String()
    31  
    32  	return metadata.AppendToOutgoingContext(ctx, HeaderTraceID, id), id, nil
    33  }