Skip to content

Commit

Permalink
Merge pull request #635 from uber/set-shardkey-in-tchannel-client
Browse files Browse the repository at this point in the history
Set shardkey in tchannel client
  • Loading branch information
smb158 committed Sep 19, 2019
2 parents 4751fc8 + 5bbfeb7 commit 1433203
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
endpointKey = contextFieldKey("endpoint")
requestUUIDKey = contextFieldKey("requestUUID")
routingDelegateKey = contextFieldKey("rd")
shardKey = contextFieldKey("sk")
endpointRequestHeader = contextFieldKey("endpointRequestHeader")
requestLogFields = contextFieldKey("requestLogFields")
scopeTags = contextFieldKey("scopeTags")
Expand Down Expand Up @@ -147,6 +148,22 @@ func GetRoutingDelegateFromCtx(ctx context.Context) string {
return ""
}

// WithShardKey adds the tchannel shard key information in the
// request context.
func WithShardKey(ctx context.Context, sk string) context.Context {
return context.WithValue(ctx, shardKey, sk)
}

// GetShardKeyFromCtx returns the tchannel shardkey info
// extracted from context.
func GetShardKeyFromCtx(ctx context.Context) string {
if val := ctx.Value(shardKey); val != nil {
sk, _ := val.(string)
return sk
}
return ""
}

// WithLogFields returns a new context with the given log fields attached to context.Context
func WithLogFields(ctx context.Context, newFields ...zap.Field) context.Context {
return context.WithValue(ctx, requestLogFields, accumulateLogFields(ctx, newFields))
Expand Down
18 changes: 18 additions & 0 deletions runtime/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ func TestGetRoutingDelegateFromCtx(t *testing.T) {
assert.Equal(t, expected, rd)
}

func TestWithShardKey(t *testing.T) {
expected := "myshardkey"
ctx := WithShardKey(context.TODO(), expected)
sk := ctx.Value(shardKey)
shardKey, ok := sk.(string)

assert.True(t, ok)
assert.Equal(t, shardKey, expected)
}

func TestGetShardKeyFromCtx(t *testing.T) {
expected := "myshardkey"
ctx := WithShardKey(context.TODO(), expected)
sk := GetShardKeyFromCtx(ctx)

assert.Equal(t, expected, sk)
}

func TestContextLogger(t *testing.T) {
zapLoggerCore, logs := observer.New(zap.DebugLevel)
zapLogger := zap.New(zapLoggerCore)
Expand Down
7 changes: 7 additions & 0 deletions runtime/tchannel_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type TChannelClient struct {
timeout time.Duration
timeoutPerAttempt time.Duration
routingKey *string
shardKey *string
metrics ContextMetrics
contextExtractor ContextExtractor

Expand Down Expand Up @@ -231,6 +232,12 @@ func (c *TChannelClient) call(
if rd != "" {
ctxBuilder.SetRoutingDelegate(rd)
}

sk := GetShardKeyFromCtx(ctx)
if sk != "" {
ctxBuilder.SetShardKey(sk)
}

ctx, cancel := ctxBuilder.Build()
defer cancel()

Expand Down

0 comments on commit 1433203

Please sign in to comment.