Skip to content

Commit

Permalink
fix x-ydb-trace-id
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Oct 25, 2023
1 parent eae4c63 commit 2274511
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 59 deletions.
4 changes: 2 additions & 2 deletions internal/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (c *conn) Invoke(
c.touchLastUsage()
defer c.touchLastUsage()

traceID, err := newTraceID()
ctx, traceID, err := meta.TraceID(ctx)
if err != nil {
return xerrors.WithStackTrace(err)
}
Expand Down Expand Up @@ -422,7 +422,7 @@ func (c *conn) NewStream(
c.touchLastUsage()
defer c.touchLastUsage()

traceID, err := newTraceID()
ctx, traceID, err := meta.TraceID(ctx)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
Expand Down
23 changes: 0 additions & 23 deletions internal/conn/trace_id.go

This file was deleted.

33 changes: 0 additions & 33 deletions internal/conn/trace_id_test.go

This file was deleted.

12 changes: 11 additions & 1 deletion internal/meta/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ import (

// WithTraceID returns a copy of parent context with traceID
func WithTraceID(ctx context.Context, traceID string) context.Context {
return metadata.AppendToOutgoingContext(ctx, HeaderTraceID, traceID)
if md, has := metadata.FromOutgoingContext(ctx); !has || len(md[HeaderTraceID]) == 0 {
return metadata.AppendToOutgoingContext(ctx, HeaderTraceID, traceID)
}
return ctx
}

func traceID(ctx context.Context) (string, bool) {
if md, has := metadata.FromOutgoingContext(ctx); has && len(md[HeaderTraceID]) > 0 {
return md[HeaderTraceID][0], true
}
return "", false
}

// WithUserAgent returns a copy of parent context with custom user-agent info
Expand Down
30 changes: 30 additions & 0 deletions internal/meta/trace_id.go
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
}
57 changes: 57 additions & 0 deletions internal/meta/trace_id_test.go
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])
})
}

0 comments on commit 2274511

Please sign in to comment.