Agent APIedit

You can access agent API after initializing the agent:

var apm = require('@elastic/apm-rum').init(...)

apm.init([config])edit

Initializes the agent with the given configuration and returns itself. Under the hood init does the following

  • Registers a global error listener to track JavaScript errors on the page.
  • Adds a onload event listener to collect the page load metrics.

When the agent is inactive, both error and onload listeners will not be registered on the page

Both XHR and Fetch API are patched as soon as the agent script is executed on the page and does not get changed even if the agent is inactive. The reason is to allow users to initialize the agent asynchronously on the page.

apm.setUserContext()edit

apm.setUserContext(context)

Call this method to enrich collected performance data and errors with information about the user.

The given context argument must be an object and can contain the following properties (all optional):

  • id - The users ID
  • username - The users username
  • email - The users e-mail

The provided user context is stored under context.user in Elasticsearch on both errors and transactions.

It’s possible to call this function multiple times within the scope of the same active transaction. For each call, the properties of the context argument are shallow merged with the context previously given.

apm.setCustomContext()edit

apm.setCustomContext(context)

Call this to enrich collected errors and transactions with any information that you think will help you debug performance issues or errors.

The provided custom context is stored under context.custom in Elasticsearch on both errors and transactions.

It’s possible to call this function multiple times within the scope of the same active transaction. For each call, the properties of the context argument are shallow merged with the context previously given.

The given context argument must be an object and can contain any property that can be JSON encoded.

Before using custom context, ensure you understand the different types of metadata that are available.

apm.addTags()edit

[4.1.x] Deprecated in 4.1.x. Tags have been replaced with labels , Please use apm.addLabels() instead

apm.addLabels()edit

apm.addLabels({ [name]: value })

Set labels on transactions and errors.

Labels are key/value pairs that are indexed by Elasticsearch and therefore searchable (as opposed to data set via apm.setCustomContext()). You can set multiple labels.

Before using custom labels, ensure you understand the different types of metadata that are available.

Arguments:

  • name - Any string. All periods (.), asterisks (*), and double quotation marks (") will be replaced by underscores (_), as those characters have special meaning in Elasticsearch
  • value - Any string. If a non-string data type is given, it’s converted to a string before being sent to the APM Server.

Avoid defining too many user-specified labels. Defining too many unique fields in an index is a condition that can lead to a mapping explosion.

apm.addFilter()edit

A filter can be used to modify the APM payload before it is sent to the apm-server. This can be useful in for example redacting sensitive information from the payload:

apm.addFilter(function (payload) {
  if (payload.errors) {
    payload.errors.forEach(function (error) {
      error.exception.message = error.exception.message.replace('secret', '[REDACTED]')
    })
  }
  if (payload.transactions) {
    payload.transactions.forEach(function (tr) {
      tr.spans.forEach(function (span) {
        if (span.context && span.context.http && span.context.http.url) {
          var url = new URL(span.context.http.url)
          if (url.searchParams.get('token')) {
            url.searchParams.set('token', 'REDACTED')
          }
          span.context.http.url = url.toString()
        }
      })
    })
  }
  // Make sure to return the payload
  return payload
})

The payload will be dropped if one of the filters return a falsy value.

apm.startTransaction()edit

apm.startTransaction(name, type)

Starts and returns a new transaction.

Arguments:

  • name - The name of the transaction (string). Defaults to Unknown
  • type - The type of the transaction (string). Defaults to custom

Use this method to create a custom transaction. By default, custom transactions are not managed by the agent, therefore they need to be ended manually.

This method returns undefined if apm is disabled or if active flag is set to false in the config.

apm.startSpan()edit

var span = apm.startSpan(name, type)

Starts and returns a new span on the current transaction.

Arguments:

  • name - The name of the span (string). Defaults to Unknown
  • type - The type of the span (string). Defaults to custom

This method returns undefined if apm is disabled or if active flag is set to false in the config.

apm.setInitialPageLoadName()edit

apm.setInitialPageLoadName(name)

Arguments:

  • name - The name of the page-load transaction (string).

Use this method to set the name of the page-load transaction that is sent automatically on page load event. See the custom initial page load transaction names documentation for strategies on using this method.

apm.getCurrentTransaction()edit

apm.getCurrentTransaction()

Use this method to get the current active transaction. If there is no active transaction it will return undefined.

apm.captureError()edit

apm.captureError(error)

Arguments:

  • error - An instance of Error.

Use this method to manually send an error to APM Server:

apm.captureError(new Error('<error-message>'))