Skip to content

Commit

Permalink
Add client side payload logging (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael authored Jan 14, 2023
1 parent 20a9285 commit 4faad07
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
10 changes: 8 additions & 2 deletions debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func LogPayloads(opts ...LogPayloadsOption) func(goa.Endpoint) goa.Endpoint {
opt(options)
}
}
reqKey := "payload"
resKey := "result"
if options.client {
reqKey = "client-" + reqKey
resKey = "client-" + resKey
}
return func(ctx context.Context, req interface{}) (interface{}, error) {
if !log.DebugEnabled(ctx) {
return next(ctx, req)
Expand All @@ -105,7 +111,7 @@ func LogPayloads(opts ...LogPayloadsOption) func(goa.Endpoint) goa.Endpoint {
if len(reqs) > options.maxsize {
reqs = reqs[:options.maxsize]
}
log.Debug(ctx, log.KV{K: "payload", V: reqs})
log.Debug(ctx, log.KV{K: reqKey, V: reqs})
res, err := next(ctx, req)
if err != nil {
return nil, err
Expand All @@ -114,7 +120,7 @@ func LogPayloads(opts ...LogPayloadsOption) func(goa.Endpoint) goa.Endpoint {
if len(ress) > options.maxsize {
ress = ress[:options.maxsize]
}
log.Debug(ctx, log.KV{K: "result", V: ress})
log.Debug(ctx, log.KV{K: resKey, V: ress})
return res, nil
}
}
Expand Down
1 change: 1 addition & 0 deletions debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestDebugPayloads(t *testing.T) {
{"debug error", newDebugLogContext(), nil, testErr, `payload={"S":"test","I":1} `, "test error"},
{"maxsize", newDebugLogContext(), WithMaxSize(1), nil, `payload={ result={ `, ""},
{"format", newDebugLogContext(), WithFormat(formatTest), nil, `payload=test result=test `, ""},
{"client", newDebugLogContext(), WithClient(), nil, `client-payload={"S":"test","I":1} client-result={"S":"test","I":1} `, ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions debug/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type (
options struct {
maxsize int // maximum number of bytes in a single log message or value
format FormatFunc
client bool
}
)

Expand All @@ -39,6 +40,14 @@ func WithMaxSize(n int) LogPayloadsOption {
}
}

// WithClient prefixes the log keys with "client-". This is useful when
// logging client requests and responses.
func WithClient() LogPayloadsOption {
return func(o *options) {
o.client = true
}
}

// FormatJSON returns a function that formats the given value as JSON.
func FormatJSON(ctx context.Context, v interface{}) string {
js, err := json.Marshal(v)
Expand Down
8 changes: 8 additions & 0 deletions debug/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func TestWithMaxSize(t *testing.T) {
}
}

func TestWithClient(t *testing.T) {
opts := defaultOptions()
WithClient()(opts)
if !opts.client {
t.Errorf("got client %v, expected true", opts.client)
}
}

func TestFormatJSON(t *testing.T) {
ctx := context.Background()
js := FormatJSON(ctx, map[string]string{"foo": "bar"})
Expand Down
3 changes: 2 additions & 1 deletion example/weather/services/front/clients/forecaster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
goa "goa.design/goa/v3/pkg"
"google.golang.org/grpc"

"goa.design/clue/debug"
genforecast "goa.design/clue/example/weather/services/forecaster/gen/forecaster"
genclient "goa.design/clue/example/weather/services/forecaster/gen/grpc/forecaster/client"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ type (
// New instantiates a new forecast service client.
func New(cc *grpc.ClientConn) Client {
c := genclient.NewClient(cc, grpc.WaitForReady(true))
return &client{c.Forecast()}
return &client{debug.LogPayloads()(c.Forecast())}
}

// Forecast returns the forecast for the given location or current location if
Expand Down
3 changes: 2 additions & 1 deletion example/weather/services/front/clients/locator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
goa "goa.design/goa/v3/pkg"
"google.golang.org/grpc"

"goa.design/clue/debug"
genclient "goa.design/clue/example/weather/services/locator/gen/grpc/locator/client"
genlocator "goa.design/clue/example/weather/services/locator/gen/locator"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ type (
// New instantiates a new locator service client.
func New(cc *grpc.ClientConn) Client {
c := genclient.NewClient(cc, grpc.WaitForReady(true))
return &client{c.GetLocation()}
return &client{debug.LogPayloads()(c.GetLocation())}
}

// GetLocation returns the location for the given IP address.
Expand Down

0 comments on commit 4faad07

Please sign in to comment.