Setting up agent-based Node.js monitoring
To monitor Node.js applications in your environment by using the Instana agent, install the Instana Node.js collector. The Instana Node.js collector is an npm package that you add to the dependencies of your Node.js applications. It enables metrics collection and automatic tracing, reporting metrics and traces to Instana.
For more information about other Node.js monitoring options, see Node.js monitoring options.
- Before you begin
- Setting up monitoring
- Installing the Node.js collector on different platforms
- Installing the Node.js collector manually
- Activating the Node.js collector
- Configuring build tools for Node.js collector
- Native add-ons
- Disabling the Node.js collector
- Related topics
Before you begin
To set up agent-based monitoring, complete the following steps:
- Review the prerequisites for monitoring Node.js applications.
- Install the Instana host agent.
Setting up monitoring
To set up Node.js monitoring by using the Instana agent, complete the following steps:
-
Install the Node.js collector: Choose an installation method based on your environment:
- Platform-specific installation: Follow the installation method that is tailored to your environment.
- Manual installation: If your platform isn't listed, manually install the Node.js collector.
-
If you use build tools, configure build tools.
After you set up monitoring, you can view the metrics and traces that are related to your Node.js applications in the Instana UI. For more information, see Viewing traces and metrics.
Installing the Node.js collector on different platforms
Use the section provided to guide you to the steps for installing the Node.js collector and any other considerations for the platform that your Node.js application runs on.
Kubernetes and Red Hat OpenShift
If your Node.js application runs in a Kubernetes and Red Hat OpenShift environment, the Instana AutoTrace webhook automatically installs the Instana Node.js collector in your cluster, follow the instructions in Installing the Instana AutoTrace webhook.
If you plan to use the Instana Node.js SDK, review the using the API together with the AutoTrace webhook section.
If your Node.js application and the Instana agent run in a Kubernetes cluster, you need to configure network access for monitored applications. For more information, see Kubernetes network access.
Native add-ons on read-only systems: Native add-ons that are compiled against a predefined Node.js version (currently v21) can cause compatibility issues with other Node.js versions (for example, v20). On read-only systems, the collector cannot unpack precompiled binary files, preventing certain native modules from loading. As a result, certain metrics, such as garbage collection data and specific event loop statistics, are not available in such environments. However, general tracing function remains unaffected. For more information, see the troubleshooting section.
Alternatively, you can manually install the Node.js collector to monitor your application.
Cloud Foundry
You can monitor Cloud Foundry applications only when the Instana agent is actively running on the Diego cells of the Cloud Foundry foundation. Without the agent that is running on the underpinning Diego cell, monitoring is not supported.
For Tile 1.177.0 and later versions, install the Node.js collector by using the Instana buildpack to automate the setup of Node.js applications on Cloud Foundry.
Alternatively, you can install the Node.js collector manually.
Apigee Microgateway
To use the Instana Node.js collector package with Apigee Microgateway (also known as edgemicro
),
follow the instructions in Monitoring Apigee Microgateway with the Instana Node.js collector.
Next.js
For Next.js applications, install the Instana Node.js collector globally or locally and use the load flags to activate the collector.
Installing the Node.js collector manually
Ensure that you read through the Before you begin section.
- ECMAScript support was added in Instana Node.js collector 2.14.0.
- Some ECMAScript modules features are in the experimental phase. For more information, see official documentation.
To install the Instana Node.js collector, complete the following steps:
-
Install the npm package
@instana/collector
in your application:npm install --save @instana/collector
-
Activate the Node.js collector from within the Node.js application:
To activate the Node.js collector in your Node.js application, see the CommonJS and ECMAScript sections.
Installing globally
As an alternative approach, you can install the Node.js collector globally by completing the following steps:
-
Install the package
@instana/collector
on the target system:-
If you use npm, run the following command:
npm install -g @instana/collector
-
If you use yarn, run the following command:
yarn global add @instana/collector
If you are installing the collector into a containerized application, you can add the preceding statement to
Dockerfile
. -
-
Determine the location where the
npm install -g
command installs the package. The location depends on the operating system. For more information about where npm puts installed packages, see the Node.js documentation about folders. -
Ensure that the package
@instana/collector
with all its dependencies is available on the target system in a well-known location, such as/path/to/instana/node_modules/@instana/collector
. -
Activate the Node.js collector:
-
CommonJS: If the application uses CommonJS, follow the steps in the CommonJS section.
-
ECMAScript modules: If the application uses ECMAScript modules, follow the steps in the ECMAScript section.
-
Activating the Node.js collector
To activate the Instana Node.js collector in different scenarios, see the following sections:
CommonJS
If the application uses CommonJS, activate the Node.js collector by using one of the following methods:
- Use the
--require
load flag for activation. - Require and initialize the Node.js collector as the first statement in your application.
Load flag --require
This approach is preferred.
For applications that use CommonJS, complete one of the following steps when you start the Node.js executable. Using either NODE_OPTIONS
or the --require
flag loads and initializes the Node.js collector
before your application code.
Make sure that you do not accidentally omit the src/immediate
part when you set NODE_OPTIONS
or add the --require
parameter. The path needs to start with ./
so that Node.js
identifies it as a relative path and not a module identifier. The path is evaluated based on the current working directory.
-
Set the
NODE_OPTIONS
variable before you start the Node.js activation process:NODE_OPTIONS="--require ./node_modules/@instana/collector/src/immediate"
If you build the application with Docker, you need to set
NODE_OPTIONS
by adding the following line to yourDockerfile
(after the lastFROM
statement and before the finalCMD
orENTRYPOINT
that starts thenode
process):ENV NODE_OPTIONS="--require ./node_modules/@instana/collector/src/immediate"
-
Alternately, add the
--require
parameter to the command that starts Node.js. If you normally start the application by using thenode app/index.js
command, add an extra--require
load flag. See the following example:node --require ./node_modules/@instana/collector/src/immediate app/index.js
If this application is built and run by using Docker, you need to modify the final
CMD
or theENTRYPOINT
in your Dockerfile.For more information about the
--require
parameter, see -r, --require module in the Node.js documentation.
CommonJS Manual Require
The collector initialization line must be the first statement in your application. Otherwise, the collector cannot access certain information. For more information about correctly adding this line, see Verifying collector integration.
require('@instana/collector')();
// All other require statements must be done after the collector is initialized.
// Note the () after the require statement of the collector which initializes it.
// const express = require('express');
The preceding code initializes the collector with default configuration options. You can also initialize the Instana Node.js collector with a custom configuration object. For the list of valid configuration options, see Node.js Collector Configuration. For more information about configuring connectivity between your monitored application and the Instana agent, see Agent communication.
ECMAScript
If your application uses ECMAScript and the @instana/collector
collector that is installed as a dependency, you can activate the collector by loading the Instana Node.js collector by using the Node.js load flags.
If the application uses ECMAScript modules, complete either of the following steps when you start the node executable. Setting NODE_OPTIONS
or selecting either the --import
or --experimental-loader
argument based on your Node.js version loads and initializes the Node.js collector before the application code. For more information
on ECMAScript modules, see the Node.js official documentation.
-
Set the
NODE_OPTIONS
variable before you start the Node.js activation process:If you build the application with Docker, set
NODE_OPTIONS
by adding the following line to yourDockerfile
(after the lastFROM
statement and before the finalCMD
orENTRYPOINT
that starts thenode
process):-
For Node.js 18.19.0 and later, use the following command to import the Instana collector:
ENV NODE_OPTIONS="--import /path/to/instana/node_modules/@instana/collector/esm-register.mjs"
-
For Node.js versions earlier to 18.19.0, use the following command to import the Instana collector:
ENV NODE_OPTIONS="--experimental-loader /path/to/instana/node_modules/@instana/collector/esm-loader.mjs"
-
-
Alternately, include the
--import
or--experimental-loader
parameter when you start Node.js. If you start your application withnode app/index.js
, append the appropriate--import
or--experimental-loader
command based on your Node.js version. See the following example:-
For Node.js 18.19.0 and later, use the following command to import the Instana collector:
node --import /path/to/instana/node_modules/@instana/collector/esm-register.mjs app/index.js
-
For Node.js versions earlier to 18.19.0, use the following command to import the Instana collector:
node --experimental-loader /path/to/instana/node_modules/@instana/collector/esm-loader.mjs app/index.js
The loader automatically initializes the Instana Node.js collector.
-
Enable the collector on Windows
If the application uses ECMAScript modules and your operating system is Windows, prepend file:///
to the path for the ECMAScript loader:
--import file:///path/to/instana/node_modules/@instana/collector/esm-register.mjs
or
--experimental-loader file:///path/to/instana/node_modules/@instana/collector/esm-loader.mjs
Configuring build tools for Node.js collector
If you use a bundler (such as Webpack or Rollup) or a transpiler (such as Babel, Traceur, or the TypeScript compiler tsc
), use the following section to guide you to the steps for installing the Node.js collector and any other considerations
for your build scenario.
Bundlers
When you pre-process your Node.js server application with a bundler, such as Webpack or Rollup, make sure that you bundle only your code and not the code of your dependencies from the node_modules
folder.
You must exclude the dependencies due to the following reasons:
- The module
@instana/collector
does not support being pre-processed with Webpack. Among other problems, dynamicrequire
statements that are used in your code are not resolved correctly. - To instrument third-party libraries, the npm package
@instana/collector
intercepts therequire
process of Node.js. But if those libraries are not loaded at run time and instead are bundled with your own code at build time, norequire
process is run, and the instrumentation is not applied. Therefore, Instana tracing doesn't work or works partially, that is, tracing works only for Node.js core modules likehttp
because those modules are excluded from bundling by default.
Thus, you must configure your bundler by using the module webpack-node-externals so that everything in node_modules
is excluded from all code transformations:
// your webpack.config.js:
const nodeExternals = require('webpack-node-externals');
module.exports = {
...
// externals: nodeModules,
externals: [nodeExternals()],
...
};
If you use Angular CLI, in particular with Angular Universal or SSR, you need to add "externalDependencies": ["@instana/collector"]
(and possibly other libraries
that @instana/collector
instruments) to your angular.json
file to exclude them from bundling.
Transpilers
If you are using a transpiler, such as TypeScript or Babel, make sure that your transpiler produces CommonJS or ECMAScript dist code.
Initialize the tracer via the load flags for CommonJS and ECMAScript.
For more information about accessing the SDK within your code, see ECMAScript modules.
Native add-ons
The tracer installs the following native add-ons:
- gcstats.js: Retrieves information about garbage collection.
- event-loop-stats: Retrieves information about event loop activity.
- autoprofile: Investigates performance issues or bottlenecks. The AutoProfile™ is disabled by default. To enable AutoProfile™, see Node.js collector configuration.
These add-ons are optional npm dependencies. If they can't be installed on your system, the collector still works as expected. If you are experiencing a problem with the add-ons, see the troubleshooting section. For more information about C++ add-ons requirements, see the official Node.js docs or node-gyp.
Installing dependencies without optional dependencies
If you install your dependencies with npm install --no-optional
or yarn --ignore-optional
command, npm or yarn doesn't try to install the native add-ons.
To fix the issue of not installing the native add-ons by npm or yarn, either remove the flags from the installation step, or add the following installation steps:
npm install @instana/autoprofile
oryarn add @instana/autoprofile
npm install event-loop-stats
oryarn add event-loop-stats
npm install gcstats.js
oryarn add gcstats.js
Disabling the Node.js collector
You can use environment variables to disable the Node.js collector for (local) development. The Express framework popularized the environment variable NODE_ENV
for this purpose.
To disable the Node.js collector, complete the following steps:
-
Load the Node.js collector as follows:
if (process.env.NODE_ENV !== 'development') { require('@instana/collector')(); }
-
Start your application locally with the
NODE_ENV
variable set todevelopment
. See the following examples:export NODE_ENV=development
NODE_ENV=development node myApp.js