Configuring the agentedit

There are three ways to configure the Node.js agent. In order of precedence (higher overwrites lower):

  1. Environment variables.
  2. If calling the apm.start() function, you can supply a configurations object as the first argument.
  3. Via the agent configuration file.

For information on the available configuration properties, and the expected names of environment variables, see the Configuration options documentation.

Agent configuration objectedit

To use the optional options argument, simply pass it into the apm.start() method:

var apm = require('elastic-apm-node').start({
  // add configuration options here
})

Here’s an example usage configuring the agent to only be active in production:

// Add this to the VERY top of the first file loaded in your app
require('elastic-apm-node').start({

  // Override service name from package.json
  // Allowed characters: a-z, A-Z, 0-9, -, _, and space
  serviceName: '',

  // Use if APM Server requires a token
  secretToken: '',

  // Set custom APM Server URL (default: http://localhost:8200)
  serverUrl: '',

  // Only activate the agent if it's running in production
  active: process.env.NODE_ENV === 'production'
})

Agent configuration fileedit

The Node.js agent will look for a file named elastic-apm-node.js in the current working directory. You can specify a custom path for this file using the ELASTIC_APM_CONFIG_FILE environment variable (this path must include the filename), e.g:

ELASTIC_APM_CONFIG_FILE=/path/to/elastic-apm-node.js

The configuration file is expected to export an object following the same conventions as the options object given as the first argument to the start function, e.g.:

module.exports = {
  // Override service name from package.json
  // Allowed characters: a-z, A-Z, 0-9, -, _, and space
  serviceName: '',

  // Use if APM Server requires a token
  secretToken: '',

  // Set custom APM Server URL (default: http://localhost:8200)
  serverUrl: ''
}