-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eae4c63
commit 2274511
Showing
6 changed files
with
100 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package meta | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
"google.golang.org/grpc/metadata" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" | ||
) | ||
|
||
type newTraceIDOpts struct { | ||
newRandom func() (uuid.UUID, error) | ||
} | ||
|
||
func TraceID(ctx context.Context, opts ...func(opts *newTraceIDOpts)) (context.Context, string, error) { | ||
if id, has := traceID(ctx); has { | ||
return ctx, id, nil | ||
} | ||
options := newTraceIDOpts{newRandom: uuid.NewRandom} | ||
for _, opt := range opts { | ||
opt(&options) | ||
} | ||
uuid, err := options.newRandom() | ||
if err != nil { | ||
return ctx, "", xerrors.WithStackTrace(err) | ||
} | ||
id := uuid.String() | ||
return metadata.AppendToOutgoingContext(ctx, HeaderTraceID, id), id, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package meta | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
func TestTraceID(t *testing.T) { | ||
t.Run("TraceID from rand", func(t *testing.T) { | ||
ctx, id, err := TraceID( | ||
context.Background(), | ||
func(opts *newTraceIDOpts) { | ||
opts.newRandom = func() (uuid.UUID, error) { | ||
return uuid.UUID{}, nil | ||
} | ||
}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, "00000000-0000-0000-0000-000000000000", id) | ||
md, has := metadata.FromOutgoingContext(ctx) | ||
require.True(t, has) | ||
require.Equal(t, 1, len(md[HeaderTraceID])) | ||
require.Equal(t, id, md[HeaderTraceID][0]) | ||
}) | ||
t.Run("TraceID from rand failed", func(t *testing.T) { | ||
_, _, err := TraceID( | ||
context.Background(), | ||
func(opts *newTraceIDOpts) { | ||
opts.newRandom = func() (uuid.UUID, error) { | ||
return uuid.UUID{}, errors.New("") | ||
} | ||
}, | ||
) | ||
require.Error(t, err) | ||
}) | ||
t.Run("TraceID from outgoing metadata", func(t *testing.T) { | ||
ctx, id, err := TraceID( | ||
WithTraceID(context.Background(), "{test}"), | ||
func(opts *newTraceIDOpts) { | ||
opts.newRandom = func() (uuid.UUID, error) { | ||
return uuid.UUID{}, errors.New("") | ||
} | ||
}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, "{test}", id) | ||
md, has := metadata.FromOutgoingContext(ctx) | ||
require.True(t, has) | ||
require.Equal(t, 1, len(md[HeaderTraceID])) | ||
require.Equal(t, id, md[HeaderTraceID][0]) | ||
}) | ||
} |