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
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
Depending on your environment, complete the steps that are outlined in the following sections to install the Node.js collector. The following table lists the collector installation options available on different platforms:
| Platforms | Automatic installation | Manual installation |
|---|---|---|
| Kubernetes and Red Hat OpenShift | [x] | [x] |
| Cloud Foundry | [x] | [x] |
| Apigee Microgateway | [x] | |
| Next.js | [x] |
Kubernetes and Red Hat OpenShift
If your Node.js application runs in a Kubernetes and Red Hat OpenShift environment, you can install the Instana Node.js collector automatically or manually as follows:
-
Automatic collector installation (preferred): To install the collector automatically, complete the following steps:
-
Install the Instana AutoTrace webhook.
Note: 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, configure network access for monitored applications. For more information, see Kubernetes network access.
-
-
Manual collector installation: To install the collector manually, see Installing the Node.js collector manually. Use the manual approach only if automatic installation is not feasible.
Cloud Foundry
To install the Instana Node.js collector on Cloud Foundry, select one of the following options:
-
Automatic collector installation (preferred): 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.
-
Manual collector installation: To install the collector manually, see Installing the Node.js collector manually. Use the manual approach only if automatic installation is not feasible.
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
To use the Instana Node.js collector for monitoring Next.js applications, complete the following steps:
- Install the collector globally or locally.
- Use the load flags to activate the collector.
Installing the Node.js collector manually
Before you install the collector, ensure that you read through the Before you begin section.
You can install the Node.js collector locally or globally.
Installing the collector locally
To install the Instana Node.js collector, install the npm package @instana/collector in your application by running the following command:
npm install --save @instana/collector
After you install the collector, activate the collector to set up monitoring. For instructions, see Activate the Node.js collector.
Installing the collector globally
As an alternative approach, you can install the Node.js collector globally by completing the following steps:
-
Install the package
@instana/collectoron 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 package is installed. 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/collectorwith all its dependencies is available on the target system in a well-known location, such as/path/to/instana/node_modules/@instana/collector.
After you install the collector, activate the collector to set up monitoring. For instructions, see Activate the Node.js collector.
Activating the Node.js collector
Depending on the modules that are used in your Node.js application, activate the collector as follows:
- If your application uses CommonJS modules, follow the steps in the CommonJS section.
- If your application uses ECMAScript modules, follow the steps in the ECMAScript section.
CommonJS
For applications that use CommonJS modules, activate the Node.js collector by using one of the following methods:
- Use the
--requireload flag for activation. - Require and initialize the Node.js collector as the first statement in your application.
These methods load and initialize the Node.js collector before your application code.
Load flag --require
This approach is preferred.
To activate the collector for applications that use CommonJS modules, complete one of the following steps when you start the Node.js executable.
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_OPTIONSvariable 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_OPTIONSby adding the following line to yourDockerfile(after the lastFROMstatement and before the finalCMDorENTRYPOINTthat starts thenodeprocess):ENV NODE_OPTIONS="--require ./node_modules/@instana/collector/src/immediate" -
Alternately, add the
--requireparameter to the command that starts Node.js. If you normally start the application by using thenode app/index.jscommand, add an extra--requireload flag. See the following example:node --require ./node_modules/@instana/collector/src/immediate app/index.jsIf this application is built and run by using Docker, you need to modify the final
CMDor theENTRYPOINTin your Dockerfile.For more information about the
--requireparameter, see -r, --require module in the Node.js documentation.
CommonJS Manual Require
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 using the --import argument initializes the Node.js collector before the application code. For more information about ECMAScript modules, see the Node.js official documentation.
-
Set the
NODE_OPTIONSvariable before you start the Node.js activation process:If you build the application with Docker, set
NODE_OPTIONSby adding the following line to yourDockerfile(after the lastFROMstatement and before the finalCMDorENTRYPOINTthat starts thenodeprocess):Use the following command to import the Instana collector (Node.js 18.19 and later):
ENV NODE_OPTIONS="--import /path/to/instana/node_modules/@instana/collector/esm-register.mjs" - Alternatively, include the
--importparameter when you start Node.js. If you start your application withnode app/index.js, append the--importcommand.Use the following command to import the Instana collector (Node.js 18.19 and later):
node --import /path/to/instana/node_modules/@instana/collector/esm-register.mjs app/index.jsThe loader automatically initializes the Instana Node.js collector.
- If your Node.js version is earlier than v18.19.0, you must use
--experimental-loader /path/to/instana/node_modules/@instana/collector/esm-loader.mjsinstead of--import /path/to/instana/node_modules/@instana/collector/esm-register.mjs. - The use of
--experimental-loaderwithesm-loader.mjsis no longer supported in current versions and will be removed in the next upgrade. For more details, see the Breaking Changes section.
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
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/collectordoes not support being pre-processed with Webpack. Among other problems, dynamicrequirestatements that are used in your code are not resolved correctly. - To instrument third-party libraries, the npm package
@instana/collectorintercepts therequireprocess of Node.js. But if those libraries are not loaded at run time and instead are bundled with your own code at build time, norequireprocess 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 likehttpbecause 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/autoprofileoryarn add @instana/autoprofile -
npm install event-loop-statsoryarn add event-loop-stats -
npm install gcstats.jsoryarn 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_ENVvariable set todevelopment. See the following examples:export NODE_ENV=developmentNODE_ENV=development node myApp.js
Related topics
- Node.js overview Learn about the Instana Node.js collector, including its restrictions, supported Node.js versions and libraries, and other subjects about the collector.
- Node.js collector configuration Learn about ways to configure the Instana Node.js collector to do more than the default options.
- Node.js collector API Get details about using the function that initializes the Instana Node.js collector and about using the Instana API for activities that include setting the logger after initialization and enabling OpenTracing for Node.js with Instana.
- Node.js collector GitHub repository The GitHub repository for
@instana/collectorpackage that is used for monitoring Node.js server applications with Instana.