Focused Profiling in Node.js

Long-running applications are not always busy doing work. In the case of, for instance, web, serverless or microservice applications, the work is done on every request, and the processes are idle the rest of the time. Similarly, if the program is processing messages from the queue, it may be idle when it is waiting for messages.

In order to profile those specific activity intervals in a long-running application, a profiler should be started before the activity begins, which is not always automatically possible.

StackImpact agents start and stop profilers automatically. The goal of the profiler scheduler is to increase the probability of profiling the program when it is doing relevant work. This works well for applications with equally distributed workloads, but is not optimal for applications with intermittent activity such as HTTP request handling.

Profiling API

For these cases, the Node.js agent provides agent.profile(name) method. Using it will tell the agent about relevant sections of the program execution. These calls only start and stop profilers if agent is active and the overhead requirements are met. The decision of which profiler to start (e.g. CPU profiler, memory allocation profiler, async call profiler) is also made by the agent.

Using focused profiling will not disable automatic profiling. Both may activate the profilers as long as the overhead limit is not reached for the profiling cycle.

Here is an example of a Node.js application that uses the API:

const express = require('express');
const stackimpact = require('stackimpact');

const agent = stackimpact.start({
  agentKey: 'agent key here',
  appName: 'MyNodeApp'

const app = express();

app.get('/hello', (req, res) => {
  let span = agent.profile("hello handler");

  // request handler code here


app.listen(3000, () => console.log('Example app listening on port 3000!'))

See Node.js agent’s focused profiling API for more information.

Span timing

In addition to improved profiling precision, the timing of profiled spans is reported to the Dashboard and available in the Health / Spans section.

See Node.js application metrics reference for more information.