Skip to content

Commit

Permalink
Merge 2fc0a74 into 9f97351
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinzg committed Sep 8, 2018
2 parents 9f97351 + 2fc0a74 commit e90d093
Show file tree
Hide file tree
Showing 52 changed files with 2,287 additions and 24 deletions.
11 changes: 11 additions & 0 deletions codegen/module_system.go
Expand Up @@ -50,6 +50,7 @@ type EndpointMeta struct {
ResRequiredHeadersKeys []string
TraceKey string
DeputyReqHeader string
ShadowReqHeader string
}

// EndpointCollectionMeta saves information used to generate an initializer
Expand Down Expand Up @@ -548,6 +549,7 @@ func (g *TChannelClientGenerator) Generate(
SidecarRouter: clientSpec.SidecarRouter,
StagingReqHeader: g.packageHelper.StagingReqHeader(),
DeputyReqHeader: g.packageHelper.DeputyReqHeader(),
ShadowReqHeader: g.packageHelper.ShadowReqHeader(),
}

client, err := g.templates.ExecTemplate(
Expand Down Expand Up @@ -916,6 +918,13 @@ func (g *EndpointGenerator) generateEndpointFile(
TransformTo: shk,
Field: &compile.FieldSpec{Required: false},
}

shk = textproto.CanonicalMIMEHeaderKey(g.packageHelper.ShadowReqHeader())
reqHeaders[shk] = &TypedHeader{
Name: shk,
TransformTo: shk,
Field: &compile.FieldSpec{Required: false},
}
// TODO: http client needs to support multiple thrift services
meta := &EndpointMeta{
Instance: instance,
Expand All @@ -935,6 +944,7 @@ func (g *EndpointGenerator) generateEndpointFile(
WorkflowPkg: workflowPkg,
TraceKey: g.packageHelper.traceKey,
DeputyReqHeader: g.packageHelper.DeputyReqHeader(),
ShadowReqHeader: g.packageHelper.ShadowReqHeader(),
}

var endpoint []byte
Expand Down Expand Up @@ -1276,6 +1286,7 @@ type ClientMeta struct {
Fixture *Fixture
StagingReqHeader string
DeputyReqHeader string
ShadowReqHeader string
}

func findMethod(
Expand Down
18 changes: 18 additions & 0 deletions codegen/package.go
Expand Up @@ -55,6 +55,8 @@ type PackageHelper struct {
stagingReqHeader string
// Use deputy client when this header is set
deputyReqHeader string
// This is a shadow request - call Apprentice instead of a downstream client
shadowReqHeader string
// traceKey is the key for unique trace id that identifies request / response pair
traceKey string
}
Expand Down Expand Up @@ -86,6 +88,8 @@ type PackageHelperOptions struct {
StagingReqHeader string
// header key to redirect client requests to local environment, defaults to "x-deputy-forwarded"
DeputyReqHeader string
// header key to redirect client request to apprentice, defaults to "X-Uber-Should-Request-Downstream"
ShadowReqHeader string
// header key to uniquely identifies request/response pair, defaults to "x-trace-id"
TraceKey string
}
Expand Down Expand Up @@ -146,6 +150,13 @@ func (p *PackageHelperOptions) deputyReqHeader() string {
return "x-deputy-forwarded"
}

func (p *PackageHelperOptions) shadowReqHeader() string {
if p.ShadowReqHeader != "" {
return p.ShadowReqHeader
}
return "X-Shadow-Start-Trace-Id"
}

func (p *PackageHelperOptions) traceKey() string {
if p.TraceKey != "" {
return p.TraceKey
Expand Down Expand Up @@ -192,6 +203,7 @@ func NewPackageHelper(
annotationPrefix: options.annotationPrefix(),
stagingReqHeader: options.stagingReqHeader(),
deputyReqHeader: options.deputyReqHeader(),
shadowReqHeader: options.shadowReqHeader(),
traceKey: options.traceKey(),
}
return p, nil
Expand Down Expand Up @@ -319,3 +331,9 @@ func (p PackageHelper) StagingReqHeader() string {
func (p PackageHelper) DeputyReqHeader() string {
return p.deputyReqHeader
}

// ShadowReqHeader returns the header name that will be checked to determine
// if a request should go to apprentice or downstream client
func (p PackageHelper) ShadowReqHeader() string {
return p.shadowReqHeader
}
5 changes: 5 additions & 0 deletions codegen/runner/runner.go
Expand Up @@ -89,6 +89,10 @@ func main() {
if config.ContainsKey("deputyReqHeader") {
deputyReqHeader = config.MustGetString("deputyReqHeader")
}
shadowReqHeader := "X-Shadow-Start-Trace-Id"
if config.ContainsKey("shadowReqHeader") {
shadowReqHeader = config.MustGetString("shadowReqHeader")
}
options := &codegen.PackageHelperOptions{
RelThriftRootDir: config.MustGetString("thriftRootDir"),
RelTargetGenDir: config.MustGetString("targetGenDir"),
Expand All @@ -98,6 +102,7 @@ func main() {
CopyrightHeader: string(copyright),
StagingReqHeader: stagingReqHeader,
DeputyReqHeader: deputyReqHeader,
ShadowReqHeader: shadowReqHeader,
TraceKey: config.MustGetString("traceKey"),
}

Expand Down
92 changes: 90 additions & 2 deletions codegen/template_bundle/template_files.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions codegen/templates/http_client.tmpl
Expand Up @@ -47,6 +47,9 @@ type Client interface {
type {{$clientName}} struct {
clientID string
httpClient *zanzibar.HTTPClient
{{if ne $clientID "apprentice" -}}
apprenticeClient *zanzibar.TChannelClient
{{end -}}

{{if $sidecarRouter -}}
calleeHeader string
Expand Down Expand Up @@ -76,6 +79,44 @@ func {{$exportName}}(deps *module.Dependencies) Client {
deps.Default.Config.MustGetStruct("clients.{{$clientID}}.defaultHeaders", &defaultHeaders)
}

{{if ne $clientID "apprentice" -}}
apprenticeMethodNames := map[string]string{
"Apprentice::getRequest": "getRequest",
"Apprentice::getResponse": "getResponse",
}

apprenticeTimeout := time.Millisecond * time.Duration(50)
if deps.Default.Config.ContainsKey("clients.apprentice.timeout") {
apprenticeTimeout = time.Millisecond * time.Duration(
deps.Default.Config.MustGetInt("clients.apprentice.timeout"),
)
}

apprenticeTimeoutPerAttempt := time.Millisecond * time.Duration(50)
if deps.Default.Config.ContainsKey("clients.apprentice.timeoutPerAttempt") {
apprenticeTimeoutPerAttempt = time.Millisecond * time.Duration(
deps.Default.Config.MustGetInt("clients.apprentice.timeoutPerAttempt"),
)
}

apprenticeRoutingKey := ""

apprenticeClient := zanzibar.NewTChannelClient(
deps.Default.Channel,
deps.Default.Logger,
deps.Default.Scope,
&zanzibar.TChannelClientOption{
ServiceName: "apprentice",
ClientID: "apprentice",
MethodNames: apprenticeMethodNames,
Timeout: apprenticeTimeout,
TimeoutPerAttempt: apprenticeTimeoutPerAttempt,
RoutingKey: &apprenticeRoutingKey,
AltSubchannelName: "apprentice",
},
)
{{end}}

return &{{$clientName}}{
clientID: "{{$clientID}}",
{{if $sidecarRouter -}}
Expand All @@ -96,6 +137,9 @@ func {{$exportName}}(deps *module.Dependencies) Client {
defaultHeaders,
timeout,
),
{{if ne $clientID "apprentice" -}}
apprenticeClient: apprenticeClient,
{{end -}}
}
}

Expand Down
44 changes: 44 additions & 0 deletions codegen/templates/tchannel_client.tmpl
Expand Up @@ -117,14 +117,58 @@ func {{$exportName}}(deps *module.Dependencies) Client {
},
)

{{if ne $clientID "apprentice" -}}
apprenticeMethodNames := map[string]string{
"Apprentice::getRequest": "getRequest",
"Apprentice::getResponse": "getResponse",
}

apprenticeTimeout := time.Millisecond * time.Duration(50)
if deps.Default.Config.ContainsKey("clients.apprentice.timeout") {
apprenticeTimeout = time.Millisecond * time.Duration(
deps.Default.Config.MustGetInt("clients.apprentice.timeout"),
)
}

apprenticeTimeoutPerAttempt := time.Millisecond * time.Duration(50)
if deps.Default.Config.ContainsKey("clients.apprentice.timeoutPerAttempt") {
apprenticeTimeoutPerAttempt = time.Millisecond * time.Duration(
deps.Default.Config.MustGetInt("clients.apprentice.timeoutPerAttempt"),
)
}

apprenticeRoutingKey := ""

apprenticeClient := zanzibar.NewTChannelClient(
deps.Default.Channel,
deps.Default.Logger,
deps.Default.Scope,
&zanzibar.TChannelClientOption{
ServiceName: "apprentice",
ClientID: "apprentice",
MethodNames: apprenticeMethodNames,
Timeout: apprenticeTimeout,
TimeoutPerAttempt: apprenticeTimeoutPerAttempt,
RoutingKey: &apprenticeRoutingKey,
AltSubchannelName: "apprentice",
},
)
{{end}}

return &{{$clientName}}{
client: client,
{{if ne $clientID "apprentice" -}}
apprenticeClient: apprenticeClient,
{{end -}}
}
}

// {{$clientName}} is the TChannel client for downstream service.
type {{$clientName}} struct {
client *zanzibar.TChannelClient
{{if ne $clientID "apprentice" -}}
apprenticeClient *zanzibar.TChannelClient
{{end -}}
}

{{range $svc := .Services}}
Expand Down

0 comments on commit e90d093

Please sign in to comment.