Skip to content

Commit

Permalink
Merge pull request #608 from uber/log_filtering_framework_client
Browse files Browse the repository at this point in the history
Log filtering framework client. This adds a feature to filter unnecessary/sensitive logs from the client.
  • Loading branch information
tejaswiagarwal committed Jul 17, 2019
2 parents 2ebd5a4 + b35c8b8 commit 8eac3ef
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 40 deletions.
6 changes: 4 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.

1 change: 1 addition & 0 deletions codegen/templates/tchannel_client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func {{$exportName}}(deps *module.Dependencies) Client {
deps.Default.Channel,
deps.Default.Logger,
deps.Default.ContextMetrics,
deps.Default.ContextExtractor,
&zanzibar.TChannelClientOption{
ServiceName: serviceName,
ClientID: "{{$clientID}}",
Expand Down
1 change: 1 addition & 0 deletions codegen/templates/tchannel_endpoint.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (h *{{$handlerName}}) redirectToDeputy(
h.Deps.Default.Channel,
h.Deps.Default.Logger,
h.Deps.Default.ContextMetrics,
h.Deps.Default.ContextExtractor,
&zanzibar.TChannelClientOption{
ServiceName: serviceName,
ClientID: "{{$clientID}}",
Expand Down
16 changes: 8 additions & 8 deletions examples/example-gateway/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package app

import (
"net/textproto"

"go.uber.org/zap"

"context"
Expand Down Expand Up @@ -62,14 +64,12 @@ func getRequestTags(ctx context.Context) map[string]string {
func getRequestFields(ctx context.Context) []zap.Field {
var fields []zap.Field
headers := zanzibar.GetEndpointRequestHeadersFromCtx(ctx)
uuid, ok := headers["X-Uuid"]
if !ok {
uuid = headers["x-uuid"]
}
fields = append(fields, zap.String("x-uuid", uuid))
fields = append(fields, zap.String("regionname", headers["Regionname"]))
fields = append(fields, zap.String("device", headers["Device"]))
fields = append(fields, zap.String("deviceversion", headers["Deviceversion"]))

for k, v := range headers {
if textproto.CanonicalMIMEHeaderKey("x-token") == textproto.CanonicalMIMEHeaderKey(k) {
continue
}
fields = append(fields, zap.String(k, v))
}
return fields
}
1 change: 1 addition & 0 deletions examples/example-gateway/build/clients/baz/baz.go

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

1 change: 1 addition & 0 deletions examples/example-gateway/build/clients/corge/corge.go

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

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

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

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

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

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

6 changes: 5 additions & 1 deletion runtime/tchannel_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type TChannelClient struct {
timeoutPerAttempt time.Duration
routingKey *string
metrics ContextMetrics
contextExtractor ContextExtractor

requestUUIDHeaderKey string
}
Expand All @@ -90,12 +91,14 @@ func NewTChannelClient(
ch *tchannel.Channel,
logger *zap.Logger,
scope tally.Scope,
contextExtractor ContextExtractor,
opt *TChannelClientOption,
) *TChannelClient {
return NewTChannelClientContext(
ch,
logger,
NewContextMetrics(scope),
contextExtractor,
opt,
)
}
Expand All @@ -105,6 +108,7 @@ func NewTChannelClientContext(
ch *tchannel.Channel,
logger *zap.Logger,
metrics ContextMetrics,
contextExtractor ContextExtractor,
opt *TChannelClientOption,
) *TChannelClient {
numMethods := len(opt.MethodNames)
Expand All @@ -130,6 +134,7 @@ func NewTChannelClientContext(
routingKey: opt.RoutingKey,
Loggers: loggers,
metrics: metrics,
contextExtractor: contextExtractor,

requestUUIDHeaderKey: opt.RequestUUIDHeaderKey,
}
Expand All @@ -154,7 +159,6 @@ func (c *TChannelClient) Call(
scopeTagsTargetService: c.serviceName,
scopeTagsTargetEndpoint: serviceMethod,
}

ctx = WithScopeTags(ctx, scopeTags)
call := &tchannelOutboundCall{
client: c,
Expand Down
2 changes: 1 addition & 1 deletion runtime/tchannel_client_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewRawTChannelClient(

metrics := NewContextMetrics(scope)
return &RawTChannelClient{
tc: NewTChannelClientContext(ch, logger, metrics, opt),
tc: NewTChannelClientContext(ch, logger, metrics, nil, opt),
logger: l,
metrics: metrics,
}
Expand Down
22 changes: 16 additions & 6 deletions runtime/tchannel_outbound_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,25 @@ func (c *tchannelOutboundCall) logFields(ctx context.Context) []zapcore.Field {
zap.Time("timestamp-finished", c.finishTime),
}

headers := map[string]string{}
for k, v := range c.reqHeaders {
fields = append(fields, zap.String(
fmt.Sprintf("%s-%s", logFieldClientRequestHeaderPrefix, k), v,
))
s := fmt.Sprintf("%s-%s", logFieldClientRequestHeaderPrefix, k)
headers[s] = v
}

for k, v := range c.resHeaders {
fields = append(fields, zap.String(
fmt.Sprintf("%s-%s", logFieldClientResponseHeaderPrefix, k), v,
))
s := fmt.Sprintf("%s-%s", logFieldClientResponseHeaderPrefix, k)
headers[s] = v
}

// If an extractor function is provided, use it, else copy all the headers
if c.client != nil && c.client.contextExtractor != nil {
ctx = WithEndpointRequestHeadersField(ctx, headers)
fields = append(fields, c.client.contextExtractor.ExtractLogFields(ctx)...)
} else {
for k, v := range headers {
fields = append(fields, zap.String(k, v))
}
}

fields = append(fields, GetLogFieldsFromCtx(ctx)...)
Expand Down
11 changes: 7 additions & 4 deletions test/endpoints/bar/bar_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,13 @@ func TestCallMetrics(t *testing.T) {
"service": "example-gateway",
"endpointID": "bar",
"endpointHandler": "normal",
"x-uuid": "uuid",
"regionname": "san_francisco",
"device": "ios",
"deviceversion": "carbon",
"X-Uuid": "uuid",
"Regionname": "san_francisco",
"Device": "ios",
"Deviceversion": "carbon",
"Content-Length": "128",
"User-Agent": "Go-http-client/1.1",
"Accept-Encoding": "gzip",
}
for actualKey, actualValue := range logMsg {
assert.Equal(t, expectedValues[actualKey], actualValue, "unexpected field %q", actualKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ func TestCallTChannelSuccessfulRequestOKResponse(t *testing.T) {
"x-uuid": "uuid",
"calling-service": "test-gateway",
"zone": "unknown",
"regionname": "sf",
"device": "ios",
"deviceversion": "1.0",
"Device": "ios",
"Regionname": "sf",
"Deviceversion": "1.0",

"Res-Header-some-res-header": "something",
}
Expand Down Expand Up @@ -178,9 +178,9 @@ func TestCallTChannelSuccessfulRequestOKResponse(t *testing.T) {
"endpointThriftMethod": "SimpleService::Call",
"endpointHandler": "call",
"x-uuid": "uuid",
"deviceversion": "1.0",
"device": "ios",
"regionname": "sf",
"Deviceversion": "1.0",
"Device": "ios",
"Regionname": "sf",

// client specific logs
"clientID": "baz",
Expand Down
1 change: 1 addition & 0 deletions test/lib/bench_gateway/bench_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func CreateGateway(
gateway.Channel,
gateway.Logger,
gateway.RootScope,
gateway.ContextExtractor,
&zanzibar.TChannelClientOption{
ServiceName: gateway.ServiceName,
MethodNames: opts.TChannelClientMethods,
Expand Down
35 changes: 23 additions & 12 deletions test/lib/test_gateway/test_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,6 @@ func CreateGateway(
timeoutPerAttempt = time.Duration(t.(int)) * time.Millisecond
}

tchannelClient := zanzibar.NewTChannelClientContext(
channel,
zap.NewNop(),
zanzibar.NewContextMetrics(tally.NoopScope),
&zanzibar.TChannelClientOption{
ServiceName: serviceName,
MethodNames: opts.TChannelClientMethods,
Timeout: timeout,
TimeoutPerAttempt: timeoutPerAttempt,
},
)

scopeExtractor := func(ctx context.Context) map[string]string {
tags := map[string]string{}
headers := zanzibar.GetEndpointRequestHeadersFromCtx(ctx)
Expand All @@ -216,10 +204,33 @@ func CreateGateway(
return tags
}

logFieldsExtractors := func(ctx context.Context) []zap.Field {
reqHeaders := zanzibar.GetEndpointRequestHeadersFromCtx(ctx)
fields := make([]zap.Field, 0, len(reqHeaders))
for k, v := range reqHeaders {
fields = append(fields, zap.String(k, v))
}
return fields
}

extractors := &zanzibar.ContextExtractors{
ScopeTagsExtractors: []zanzibar.ContextScopeTagsExtractor{scopeExtractor},
LogFieldsExtractors: []zanzibar.ContextLogFieldsExtractor{logFieldsExtractors},
}

tchannelClient := zanzibar.NewTChannelClientContext(
channel,
zap.NewNop(),
zanzibar.NewContextMetrics(tally.NoopScope),
extractors,
&zanzibar.TChannelClientOption{
ServiceName: serviceName,
MethodNames: opts.TChannelClientMethods,
Timeout: timeout,
TimeoutPerAttempt: timeoutPerAttempt,
},
)

testGateway := &ChildProcessGateway{
channel: channel,
serviceName: serviceName,
Expand Down

0 comments on commit 8eac3ef

Please sign in to comment.