How to debug custom plugins

This is a short guide on how to debug your custom plugins for Edge Microgateway.

This guide is also applicable for debugging the default plugins. These are maintained on github and are free for anyone to modify and send pull requests.

So let's get started with looking at the Node.JS, or more specifically, the NPM (Node Package Manager) environment.

Setting up development environment

Environment variables

The first useful step is to set the environment variables to point at your node_modules folder. This assumes that you have installed npm libraries globally. If you have installed it locally then you can simply set $NODE_MODULES_HOME to that directory.

1. Append the following in your ~/.bashrc

export NODE_MODULES_HOME=`npm list -g --depth=0 | head -1`/node_modules
export EDGEMICRO_HOME=${NODE_MODULES_HOME}/edgemicro

2. Use your favourite editor to open up the source code.

Preferably you should have everything under $EDGEMICRO_HOME so that you can reference the core microgateway source as well as included plugins to understand how they work.

For example, using sublime text:

subl --new-window $EDGEMICRO_HOME

How to debug code

We use a concept known as the Node.js inspector, which works by exposing a network port that a remote debugger client can connect to. In this case, that debugger is Google Chrome but there are alternatives in the link. For example you can use Visual Studio or Eclipse to debug remotely.

1. Run MGW in inspect mode with one process

node --inspect $EDGEMICRO_HOME/cli/edgemicro start -o $ORG -e $ENV -k $EDGEMICRO_KEY -s $EDGEMICRO_SECRET -p 1

Note. optionally use --inspect-brk to force the application to break until a debugger is attached. You will need this to debug the startup sequence.

Note. We use one process for simplicity. If you need to debug functionality around a multi-process architecture, then you can change or remove this (default is # of CPU cores).

2. Navigate to 'chrome:inspect' in Chrome

3. Enable “Discover Network targets” and add 127.0.0.1:9230 to the list

4. Inspect the Remote Target for the start-agent.js command.

You'll notice that there are two Node.js processes. One is the CLI and one is the actual agent. You'll want to connect to the first one if you are using --inspect-brk, otherwise connect to the second one.

5. In the popup window, in the Sources -> Filesystem menu, click +add folder to workspace

6. Add the entire $EDGEMICRO_HOME directory

7. Set breakpoints in the code.

Provided plugins are in $EDGEMICRO_HOME/node_modules/microgateway-plugins/

Custom plugins are in $EDGEMICRO_HOME/lib/node_modules/edgemicro/plugins

Place a breakpoint in the relevant lifecycle handler(s) that you'd like to debug.

8. Run an API call and step through the code

If you are unfamiliar with the Chrome DevTools debugger, check out the following guide.

https://developers.google.com/web/tools/chrome-devtools/javascript/reference

Version history
Last update:
‎06-14-2020 05:30 PM
Updated by: