Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
baggageValues := make([]string, 0, len(baggage))
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{})
Expand Down
31 changes: 31 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -101,6 +102,19 @@ func TestNewClient(t *testing.T) {
_ = testClient.WithTraceID(t.Context(), "test")
})

t.Run("new client with baggage", func(t *testing.T) {
baggage := map[string]string{
"key1": "value1",
"key2": "value2",
}

ctx := testClient.WithBaggage(t.Context(), 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)
Expand All @@ -123,6 +137,23 @@ func TestNewClient(t *testing.T) {
ctx := c.WithTraceID(t.Context(), "")
_ = 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(t.Context(), baggage)
metadata, ok := metadata.FromOutgoingContext(ctx)
require.True(t, ok)
baggageString := metadata.Get(baggageHeader)
require.Equal(t, []string{"key1=value1,key2=value2"}, baggageString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be pertinent to make this test accommodating of the non deterministic nature of the keys?

This test will be flaky otherwise

})
}

func TestShutdown(t *testing.T) {
Expand Down