API Referenceedit

Agent APIedit

You can access agent API after initializing the agent:

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

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.

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.

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

apm.setTags()edit

apm.setTags(tags)

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.

Arguments:

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

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
})
Note

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 transsaction (string).
  • type - The type of the transsaction (string).

Use this method to create a custom transaction.

Note

Calling this method would result in ending the current transaction and replacing it with the new transaction.

Note

This method returns undefined if apm is disabled or if isActive is set to false.

apm.startSpan()edit

apm.startSpan(name, type)

Starts and returns a new span on the current transaction.

Arguments:

  • name - The name of the span (string).
  • type - The type of the span (string).
Note

This method returns undefined if apm is disabled or if isActive is set to false.

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.

Transaction APIedit

A transaction groups multiple spans in a logical group.

To start a transaction, you need to call apm.startTransaction().

To see an example of using custom transactions, see the Custom Transactions article.

transaction.nameedit

  • Type: String
  • Default: Unknown

The name of the transaction.

Can be used to set or overwrite the name of the transaction (visible in the performance monitoring breakdown).

transaction.typeedit

  • Type: String
  • Default: custom

The type of the transaction.

transaction.timestampedit

  • Type: String
  • Default: undefined

The timestamp of the transaction. If the transaction timestamp is not provided (the default behaviour), it will be set by the apm-server (v6.3+). You can, however, set the timestamp on the client (using new Date().toISOString()), but you should be aware that the timestamp will reflect the client’s local time which might not always be accurate.

transaction.end()edit

transaction.end()

Ends the transaction. If the transaction has already ended, nothing happens.

transaction.mark(key)edit

transaction.mark(key)

Marks the current point in time relative to the start of the transaction. Use this method to mark significant events that happen while the transaction is active.