API Referenceedit

You can access agent API after initializing the agent:

var apm = require('elastic-apm-js-base').init(...)



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.



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.

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



Set tags on transactions and errors.

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


  • tags - Any key/value object with the following specifications:

    • key - Any string. Must not contain periods (.) as those 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.


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.raw)
          if (url.searchParams.get('token')) {
            url.searchParams.set('token', 'REDACTED')
          span.context.http.url.raw = url.toString()
  // Make sure to return the payload
  return payload

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