Get a requests call stack with node-alice

So I recently started working on a new codebase and before writing any code I wanted to learn what code was being run for a certain URL, an aggregated call stack all the way down to the response.render().
I found my only options were either to walk-through using a debugger (one click at a time) and write down every step or read through the code itself which can get complicated and hard to follow at times.

I searched for a tool to help but I only found more debuggers. I spend so much time reading web application code I thought there must be something which can help.
There seemed a lot of tools out there but nothing to produce the kind of dynamically generated single-source-of-truth I was after.

Solution:

So I went about writing a tool using instrumentation (an approach used by code coverage tools, more details here) and the V8 call stacks (details here).

The tool uses require-hooks to instrument the code, marking what ran and by what file (in order). At the same time it gets the call stack for that line to help with later parent-child processing.
It uses a singleton to store the executed data so regardless of where it’s called from it can be stored. Contents are stored as part of the same file until it detects the next called file.
The instrumented code is stored in memory and later executed at runtime. The same applies for any transpiled code e.g Babel.

It then processes this data-dump array into a call-stack array with nested relationships. By using each lines call stack it can determine what called it and if any item has a child or several children. Using recursion to check if a child has a child itself.

This new processed array is then rendered into a HTML page which has some simple hand-cranked JS functionality allowing toggling/filtering etc.

Currently node-alice is not built to recognise types of parents, just whether something is or isn’t, but I plan to add react/express and several other framework support to flag if the framework called the parent file initially.

Example output

Routing file calling a Presenter, which calls a utility file (Date.js), then back to the Presenter.
Screen Shot 2016-10-24 at 23.13.15

Routing file calling an API:
Screen Shot 2016-10-24 at 23.09.48

Summary

Some parts of the mechanism and the UI need tidying/improving as its a bit clunky, but it will get there.
Once the JSON tree is generated it can be visualised in any way. It could connect to a chrome extension to supply live ‘request call stacks’.
I hope you give node-alice a go and find it gives you a deeper understanding of how your application works.

Leave a Reply