Node.js v8 Error Tracking Improvements: How to Use Them in Real Life

393 VIEWS

·

The latest major release of Node.js, version 8.0.0, has been out for a few months. It features several major enhancements, such as the use of V8.5.8 as the Node JavaScript engine, as well as the inclusion of an API (well, technically, an ABI) to allow native modules to run on newer releases of Node.js. This improves upon the current model, which requires module developers to keep their modules constantly in sync with any new releases of Node.js.

But wait—There’s more. Two other important feature updates in Node.js are the async_hooks module and built-in JavaScript bindings for Node Inspector. These are the updates that I’ll focus on in this post, which explains how they work and how developers can use them.

async_hooks

The async_hooks (formerly async_wrap) module, while still experimental, lets developers have a diagnostic API, which allows them to watch their resources being used during any operations in progress. This gives developers a true view of resource usage through the entire lifecycle of any asynchronous calls.

This functionality, combined with the command line (CLI) debugger introduced in v7, makes it easier to track and debug changes within a running Node.js application. This functionality can be used by both commercial and open source vendors to pinpoint those points where performance degradation is occurring, which allows for a time to resolution for potential performance bottlenecks.

A common example where this kind of information might be useful is tracking how long it takes for a server to handle a connection lifecycle from opening a TCP session to closing it, and recycling the listener for the next use.

To access async_hooks inside your code, start with the following code:

Once it is available inside the code, it will need to be enabled, and when enabled, the default callbacks it watches are init, before, after, and destroy.

This is what registering callbacks then enabling async_hooks could look like:

Logging output from async_hooks is best done using a synchronous call like writeSync so as to not cause an infinite loop, as would happen with a asynchronous call like console.log().

Irina Shestak’s illustrated guide to async_hooks is a good place to start.

More extensive information is available in the official Node.js documentation for async_hooks.

JS Bindings for the Inspector

JS bindings for the Inspector are another experimental feature in Node.js v8. This feature leverages the new Inspector core, allowing developers to leverage the same debug protocol which is used by Chrome to scrutinize JavaScript code as it is running.

There are multiple ways to access the Inspector. These include a CLI-like node-inspector, Chrome DevTools version 55 or higher, VS Code, Visual Studio, and JetBrains’ WebStorm.

With the Inspector, you can see the value of any variable or resource as it flows through the application. This becomes important as you investigate application defects that involve data flow, especially data transformation activities. This use case for the Inspector can include adding breakpoints into the code to stop program execution at specific predefined points during execution to see the full application state, including variables, before allowing the application to continue executing.

Connecting to the Inspector is relatively straightforward:

Conclusion

Both of these Node enhancements lay a solid foundation for building better toolsets, which in turn help developers to create high-performing, scalable applications to serve their business needs.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Vince Power is an Enterprise Architect at Medavie Blue Cross. His focus is on cloud adoption and technology planning in key areas like core computing (IaaS), identity and access management, application platforms (PaaS), and continuous delivery.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu