diff --git a/CHANGELOG.md b/CHANGELOG.md index ae06a48e9..d92a4c026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Added `meta.WithTraceParent` context modifier for explicit putting traceparent header into grpc calls + ## v3.83.0 * Supported `db.Table().BulkUpsert()` from scv, arrow and ydb rows formats diff --git a/internal/meta/context.go b/internal/meta/context.go index 049d0a6da..0bbfa1a0a 100644 --- a/internal/meta/context.go +++ b/internal/meta/context.go @@ -48,3 +48,8 @@ func WithAllowFeatures(ctx context.Context, features ...string) context.Context return metadata.AppendToOutgoingContext(ctx, kv...) } + +// WithTraceParent returns a copy of parent context with traceparent header +func WithTraceParent(ctx context.Context, traceparent string) context.Context { + return metadata.AppendToOutgoingContext(ctx, HeaderTraceParent, traceparent) +} diff --git a/internal/meta/headers.go b/internal/meta/headers.go index 41f025941..875556b39 100644 --- a/internal/meta/headers.go +++ b/internal/meta/headers.go @@ -10,6 +10,7 @@ const ( HeaderApplicationName = "x-ydb-application-name" HeaderClientCapabilities = "x-ydb-client-capabilities" HeaderClientPid = "x-ydb-client-pid" + HeaderTraceParent = "traceparent" // outgoing hints HintSessionBalancer = "session-balancer" diff --git a/meta/context.go b/meta/context.go index 3f8673003..211956757 100644 --- a/meta/context.go +++ b/meta/context.go @@ -44,3 +44,8 @@ func WithTrailerCallback( ) context.Context { return meta.WithTrailerCallback(ctx, callback) } + +// WithTraceParent returns a copy of parent context with traceparent header +func WithTraceParent(ctx context.Context, traceparent string) context.Context { + return meta.WithTraceParent(ctx, traceparent) +} diff --git a/tests/integration/connection_test.go b/tests/integration/connection_test.go index 886564396..522adcc83 100644 --- a/tests/integration/connection_test.go +++ b/tests/integration/connection_test.go @@ -42,6 +42,7 @@ func TestConnection(sourceTest *testing.T) { var ( userAgent = "connection user agent" requestType = "connection request type" + traceParentID = "test-traceparent-id" checkMetadata = func(ctx context.Context) { md, has := metadata.FromOutgoingContext(ctx) if !has { @@ -66,10 +67,20 @@ func TestConnection(sourceTest *testing.T) { t.Fatalf("no traceIDs") } if len(traceIDs[0]) == 0 { - t.Fatalf("no traceID") + t.Fatalf("empty traceID header") + } + traceParent := md.Get(meta.HeaderTraceParent) + if len(traceParent) == 0 { + t.Fatalf("no traceparent header") + } + if len(traceParent[0]) == 0 { + t.Fatalf("empty traceparent header") + } + if traceParent[0] != traceParentID { + t.Fatalf("unexpected traceparent header") } } - ctx = xtest.Context(t) + ctx = meta.WithTraceParent(xtest.Context(t), traceParentID) ) t.RunSynced("ydb.New", func(t *xtest.SyncedTest) {