Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active August 6, 2021 11:37
Show Gist options
  • Save percybolmer/0ce15331953dae0f67f96b1ddd54fbf8 to your computer and use it in GitHub Desktop.
Save percybolmer/0ce15331953dae0f67f96b1ddd54fbf8 to your computer and use it in GitHub Desktop.
// UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls.
func UnaryClientInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryClientInterceptor {
o := evaluateClientOpt(opts)
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
fields := newClientLoggerFields(ctx, method)
startTime := time.Now()
err := invoker(ctx, method, req, reply, cc, opts...)
newCtx := ctxzap.ToContext(ctx, logger.With(fields...))
logFinalClientLine(newCtx, o, startTime, err, "finished client unary call")
return err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment