Configure Node.js agent
The standard configuration for all agents uses this order of precedence.
There are several ways to configure the Node.js agent, but generally, you should:
Use environment variables for agent authentication keys and application-specific configuration values. Learn more about environment variables in general.
Use a YAML configuration file to set common configuration values for all applications in an organization or container (for example, to redirect logging or proxy configuration). This template shows all valid configuration options for the Node.js agent. Learn more about YAML configuration in general.
Tip
Use the Contrast agent configuration editor to create or upload a YAML configuration file, validate YAML, and get setting recommendations. The editor also provides the correct environment variables, if desired.
Configuration file location
The agent looks for a file named contrast_security.yaml in the following places, in the following order. It reads the files in the first place that has one and does not look in the places after it.
The working directory of the Node.js process.
The
/etc/contrast/node/folder and then the/etc/contrast/folder.On Windows, these are
%ProgramData%\contrast\node\and%ProgramData%\contrast\.The
~/.config/contrast/node/folder and then the~/.config/contrast/folder.
If a place has a file in both of its folders, the agent combines them and the node folder wins. If a file contains root: true, the agent uses that file alone.
To use one specific file and skip the search, set CONTRAST_CONFIG_PATH to the path of the file. If the agent cannot read that file, it logs an error and does not instrument the application. To use no file at all, set CONTRAST_CONFIG_PATH to /dev/null (\\.\nul on Windows).
Environment variables
Use environment variables for application-specific configuration values (like configuring server environment, application names or agent logging). You can also use environment variables to set any other valid properties for the Node.js agent.
You can see a full list of valid properties in the Node.js YAML template, but here are some common examples as environment variables:
Agent Token: This variable is a base64-encoded JSON object containing the url, api_key, service_key, and user_name configuration settings, allowing you to set them in a single variable.
CONTRAST__API__TOKEN
If your agent configuration refers to both the legacy settings and the agent token, (in environment variables or the YAML file), the legacy settings take precedence. To use just the agent token value, remove references to the legacy settings.
Legacy settings: If you are using a Node.js agent version earlier than 5.15.0, the required variables are:
Environment variable | Description |
|---|---|
| Set the service key needed to communicate with Contrast. |
| Set the API key needed to communicate with Contrast. |
| Set the user name needed to communicate with Contrast. |
| Set the URL for the Contrast web interface. |
Optional variables include:
Environment variable | Description |
|---|---|
| Override the reported application name. |
| When set, supersedes the default location of the YAML configuration file. (Unlike other environment variables, this one cannot be set as a YAML property, and contains only single underscores.) |
| Override the reported application path. Default is |
| Provides a consistent server name for cases where containerized apps generate many server records. This could be the microservice name or app name. |
| Writes Default is Set |
| When set to |
| Logging level. Valid values are |
| Where the agent writes its log file. Default is |
| When set to |
Note
For legacy v4 of the agent:
For the Node.js agent you must manually configure DEBUG. INFO-level statements aren't logged to the console unless the environment variable DEBUG is set to include the Contrast namespace: DEBUG=contrast:*. This could be useful in environments where you don't have access to the file system (like Docker or ECS).
If you want to redirect logging for the Node.js, see more examples on the npm site or contact Support for assistance.