From 28094c24f44fbc6135ff7512cbfd5daed3a23f41 Mon Sep 17 00:00:00 2001 From: Prinson Fernandes Date: Mon, 9 Jun 2025 23:44:15 +0530 Subject: [PATCH 1/2] Added baggage support for Dapr Go SDK Signed-off-by: Prinson Fernandes --- client/client.go | 15 +++++++++++++++ client/client_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/client/client.go b/client/client.go index 4b8215ec..0afa17a1 100644 --- a/client/client.go +++ b/client/client.go @@ -48,6 +48,7 @@ const ( daprPortEnvVarName = "DAPR_GRPC_PORT" /* #nosec */ daprGRPCEndpointEnvVarName = "DAPR_GRPC_ENDPOINT" traceparentKey = "traceparent" + baggageHeader = "baggage" apiTokenKey = "dapr-api-token" /* #nosec */ apiTokenEnvVarName = "DAPR_API_TOKEN" /* #nosec */ clientDefaultTimeoutSeconds = 5 @@ -199,6 +200,9 @@ type Client interface { // WithTraceID adds existing trace ID to the outgoing context. WithTraceID(ctx context.Context, id string) context.Context + // WithBaggage adds baggage information to the outgoing context + WithBaggage(ctx context.Context, baggage map[string]string) context.Context + // WithAuthToken sets Dapr API token on the instantiated client. WithAuthToken(token string) @@ -506,6 +510,17 @@ func (c *GRPCClient) WithTraceID(ctx context.Context, id string) context.Context return metadata.NewOutgoingContext(ctx, md) } +// WithBaggage adds baggage information to the outgoing context +func (c *GRPCClient) WithBaggage(ctx context.Context, baggage map[string]string) context.Context { + var baggageValues []string + for key, value := range baggage { + baggageValues = append(baggageValues, key+"="+value) + } + + baggageString := strings.Join(baggageValues, ",") + return metadata.AppendToOutgoingContext(ctx, baggageHeader, baggageString) +} + // Shutdown the sidecar. func (c *GRPCClient) Shutdown(ctx context.Context) error { _, err := c.protoClient.Shutdown(ctx, &pb.ShutdownRequest{}) diff --git a/client/client_test.go b/client/client_test.go index 9b8e969b..d29cd3b8 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -30,6 +30,7 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/metadata" "google.golang.org/grpc/test/bufconn" "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/emptypb" @@ -101,6 +102,19 @@ func TestNewClient(t *testing.T) { _ = testClient.WithTraceID(context.Background(), "test") }) + t.Run("new client with baggage", func(t *testing.T) { + baggage := map[string]string{ + "key1": "value1", + "key2": "value2", + } + + ctx := testClient.WithBaggage(context.Background(), baggage) + metadata, ok := metadata.FromOutgoingContext(ctx) + require.True(t, ok) + baggageString := metadata.Get(baggageHeader) + require.Equal(t, []string{"key1=value1,key2=value2"}, baggageString) + }) + t.Run("new socket client closed with token", func(t *testing.T) { t.Setenv(apiTokenEnvVarName, "test") c, err := NewClientWithSocket(testSocket) @@ -123,6 +137,23 @@ func TestNewClient(t *testing.T) { ctx := c.WithTraceID(context.Background(), "") _ = c.WithTraceID(ctx, "test") }) + + t.Run("new socket client with baggage", func(t *testing.T) { + c, err := NewClientWithSocket(testSocket) + require.NoError(t, err) + defer c.Close() + + baggage := map[string]string{ + "key1": "value1", + "key2": "value2", + } + + ctx := c.WithBaggage(context.Background(), baggage) + metadata, ok := metadata.FromOutgoingContext(ctx) + require.True(t, ok) + baggageString := metadata.Get(baggageHeader) + require.Equal(t, []string{"key1=value1,key2=value2"}, baggageString) + }) } func TestShutdown(t *testing.T) { From 8af118325cc679a97c73e7a64802ccd7f558895e Mon Sep 17 00:00:00 2001 From: Prinson Fernandes Date: Tue, 10 Jun 2025 12:23:17 +0530 Subject: [PATCH 2/2] Updated code to pass lint checks Signed-off-by: Prinson Fernandes --- client/client.go | 2 +- client/client_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index 0afa17a1..cb999112 100644 --- a/client/client.go +++ b/client/client.go @@ -512,7 +512,7 @@ func (c *GRPCClient) WithTraceID(ctx context.Context, id string) context.Context // WithBaggage adds baggage information to the outgoing context func (c *GRPCClient) WithBaggage(ctx context.Context, baggage map[string]string) context.Context { - var baggageValues []string + baggageValues := make([]string, 0, len(baggage)) for key, value := range baggage { baggageValues = append(baggageValues, key+"="+value) } diff --git a/client/client_test.go b/client/client_test.go index d483eec7..67d463d1 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -108,7 +108,7 @@ func TestNewClient(t *testing.T) { "key2": "value2", } - ctx := testClient.WithBaggage(context.Background(), baggage) + ctx := testClient.WithBaggage(t.Context(), baggage) metadata, ok := metadata.FromOutgoingContext(ctx) require.True(t, ok) baggageString := metadata.Get(baggageHeader) @@ -148,7 +148,7 @@ func TestNewClient(t *testing.T) { "key2": "value2", } - ctx := c.WithBaggage(context.Background(), baggage) + ctx := c.WithBaggage(t.Context(), baggage) metadata, ok := metadata.FromOutgoingContext(ctx) require.True(t, ok) baggageString := metadata.Get(baggageHeader)