github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/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  		opt(&options)
    23  	}
    24  	uuid, err := options.newRandom()
    25  	if err != nil {
    26  		return ctx, "", xerrors.WithStackTrace(err)
    27  	}
    28  	id := uuid.String()
    29  
    30  	return metadata.AppendToOutgoingContext(ctx, HeaderTraceID, id), id, nil
    31  }