Capturing Exception Call Stacks from Node.js Applications
Tags: javascript, node, observability, debug
Published: 2022-09-23 12:41:32


## Sidekick allows you to collect stacks as errors happen, without needing for putting a tracepoint beforehand

Sidekick is an [open-source](https://github.com/runsidekick/sidekick) live application debugger that lets you troubleshoot your applications while they keep on running. It is like Chrome DevTools for your and built for everyone who needs extra information from their applications.

{% embed https://github.com/runsidekick/sidekick %}


Sidekick Actions ( [logpoint](https://docs.runsidekick.com/sidekick-actions/logpoint) and [tracepoint](https://docs.runsidekick.com/sidekick-actions/tracepoint) ) are giving developers dynamic logging and live debugging abilities. They are most powerful when you know exactly where you should put them.

Our recent user research shows that when uncaught exceptions occur, proactively putting tracepoints for stack collection might not be enough as in some rare cases, putting a tracepoint after spotting a problem can’t help until the error happens again. That’s why we have developed;

> Automatic error stack collection

With our [Node agent](https://github.com/runsidekick/sidekick-agent-nodejs)’s latest update, developers can now activate automatic error stack collection and start collecting exception call stacks from their running applications without stopping them.

Simply we can describe this new feature as an always on tracepoint that is looking out for uncaught exceptions. Of course this can bring an overhead but we see a great potential to lower MTTR’s for exceptions.

![](https://miro.medium.com/max/700/1*k2PXiKHPv4KOsYcUmQDc3A.png)

## See it in action:

We will be using our quickstart repository from a previous tutorial.

{% embed https://github.com/boroskoyo/sidekick-nodejs-quickstart %}

The only difference will the 2 new parameters we are adding at the initialization. errorCollectionEnabled is set true to enable the basic error collection and the latest parameter is set true to enable detailed frame capturing in case of errors.

![app/app.js](https://miro.medium.com/max/700/1*KWO1O5G_veldY6xZHjewIQ.png)
app/app.js

To show an example of uncaught exception we will add

> throw new Error(‘yet another error’);

at the end of the function.

![routes/speakers.js](https://miro.medium.com/max/700/1*9aOF2kgIKx9nvC6qngV3Zg.png)
routes/speakers.js

Now then we run our application and visit the speakers page it will throw an exception. It is time to collect information about this exception. For this tutorial we will be using our Sidekick Node.js Client and visualisation of collected errors will soon come to our Web IDE and extensions. You can get our client from:

{% embed https://www.npmjs.com/package/@runsidekick/sidekick-client %}


Below you can see a simple script to collect error snapshots and print them to console. Sidekick client allows you to customize your ingest functions, check out our [recipes page](https://docs.runsidekick.com/sidekick-recipes) for examples.

![client script](https://miro.medium.com/max/700/1*OhXXde3TLsR5TNE5ypEamA.png)
client script

We have started both our main application and client script. After visiting the speakers page our app throw an exception and the agent sent every single detail about it to our broker.

![](https://miro.medium.com/max/700/1*EQ5s2Lw7KhDr5WU0PED-7Q.png)

To keep the tutorial simple we have printed everyting to console but to make thing a little bit prettier we will use JSON visualization tool called JSON Crack to understand our collected data. In your own cases sending the collected data to your elastic instances or any other alternative would be much better way to use this data.

{% embed https://jsoncrack.com/ %}

Below is the initial node of our collected data. You can see when and where it happened and other details like the error message, file name, line no etc.

![](https://miro.medium.com/max/700/1*-fl3D_EDkIcRqZRsURO6LA.png)

Since we have enabled frame capture we were able to collect every information in our scope. So as you can see below in case of an error, rather than trying to reproduce it or looking out for clues we are now able get everything we need automatically.

> All thanks to Sidekick error stack collection

![](https://miro.medium.com/max/700/1*O3p1OCxFtYXIftQqxRWpHQ.png)

If you would like to ingest this collected data to your desired target you can use Sidekick’s integrations (Logz.io, Slack, Sumo Logic etc.) or see the examples below to learn how you can build your own.

{% embed https://github.com/boroskoyo/sidekick-elastic %}


{% embed https://github.com/boroskoyo/sidekick-loki %}


## **TLDR;**

> With Sidekick [Node agent](https://github.com/runsidekick/sidekick-agent-nodejs)’s latest update, developers can now activate automatic error stack collection and start collecting exception call stacks from their running applications without stopping them. A great addition to Sidekick Actions that will help you boost your developer productivity even more.

Sidekick is open-source and brings almost no overhead to your applications. Visit runsidekick.com to meet your new live application debugger.

{% embed https://www.runsidekick.com/ %}


Please let us know your invaluable ideas because they will guide us on our path to discovering the future of application observability. You can get in touch with us through [**Twitter**](https://twitter.com/RunSidekick), and join our [**Discord community**](https://www.runsidekick.com/discord-invitation).