Skip to main content

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.

  1. The working directory of the Node.js process.

  2. The /etc/contrast/node/ folder and then the /etc/contrast/ folder.

    On Windows, these are %ProgramData%\contrast\node\ and %ProgramData%\contrast\.

  3. 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

CONTRAST__API__SERVICE_KEY

Set the service key needed to communicate with Contrast.

CONTRAST__API__API_KEY

Set the API key needed to communicate with Contrast.

CONTRAST__API__USER_NAME

Set the user name needed to communicate with Contrast.

CONTRAST__API__URL

Set the URL for the Contrast web interface.

Optional variables include:

Environment variable

Description

CONTRAST__APPLICATION__NAME

Override the reported application name.

CONTRAST_CONFIG_PATH

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.)

CONTRAST__APPLICATION__PATH

Override the reported application path. Default is /.

CONTRAST__SERVER__NAME

Provides a consistent server name for cases where containerized apps generate many server records. This could be the microservice name or app name.

CONTRAST__AGENT__DIAGNOSTICS__ENABLE

Writes contrast_effective_config.json and contrast_system_info.json at startup, with credentials redacted, to help with troubleshooting.

Default is true.

Set CONTRAST__AGENT__DIAGNOSTICS__REPORT_PATH to change the folder. The default is the working directory.

CONTRAST__AGENT__LOGGER__APPEND

When set to false, creates a new log file on startup instead of appending and rolling daily. Default is true.

CONTRAST__AGENT__LOGGER__LEVEL

Logging level. Valid values are ERROR, WARN, INFO, DEBUG, and TRACE. Default is INFO.

CONTRAST__AGENT__LOGGER__PATH

Where the agent writes its log file. Default is contrast.log in the working directory. Set it to /dev/null to stop writing a log file.

CONTRAST__AGENT__LOGGER__STDOUT

When set to false, suppresses output to stdout. Default is true.

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.