Transaction APIedit

A transaction groups multiple spans in a logical group.

To get a Transaction object, you need to call apm.startTransaction().

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

transaction.nameedit

  • Type: String
  • Default: unnamed

The name of the transaction.

Can be used to set or overwrite the name of the transaction (visible in the performance monitoring breakdown). If you don’t have access to the current transaction, you can also set the name using apm.setTransactionName().

Transactions with the same name and type are grouped together.

transaction.typeedit

  • Type: String
  • Default: custom

The type of the transaction.

There’s a special type called request which is used by the agent for the transactions automatically created when an incoming HTTP request is detected.

transaction.resultedit

  • Type: Number
  • Default: 200

The transaction result.

Think of the transaction result as equivalent to the status code of an HTTP response.

transaction.startSpan([name][, type])edit

var span = transaction.startSpan('My custom span')

Start and return a new custom span associated with this transaction.

Arguments:

  • name - The name of the span (string). You can alternatively set this via span.name. Defaults to unnamed
  • type - The type of span (string). You can alternatively set this via span.type. Defaults to custom

When a span is started it will measure the time until span.end() is called.

See Span API docs for details on how to use custom spans.

transaction.setTag()edit

transaction.setTag(name, value)

Set a tag on the transaction. You can set multiple tags on the same transaction. If an error happens during the transaction, it will also get tagged with the same tags.

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

Arguments:

  • name - Any string. 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

transaction.addTags()edit

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

Add several tags on the transaction. You can add tags multiple times. If an error happens during the transaction, it will also get tagged with the same tags.

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

Arguments:

  • tags - An object containing key/value pairs each representing tag name and tag value:

    • name - Any string. Any periods (.), asterisks (*), or 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

transaction.ensureParentId()edit

  • Type: String

If the transaction does not already have a parent id, calling this method generates a new parent id, sets it as the parent id of this transaction, and returns it as a String.

This enables the correlation of the spans the JavaScript Real User Monitoring (RUM) agent creates for the initial page load with the transaction of the backend service. If your backend service generates the HTML page dynamically, initializing the JavaScript RUM agent with the value of this method allows analyzing the time spent in the browser vs in the backend services.

To enable the JavaScript RUM agent, add a snippet similar to this to the body of your HTML page, preferably before other JavaScript libraries:

elasticApm.init({
  serviceName: 'my-frontend-app', // Name of your frontend app
  serverUrl: 'https://example.com:8200', // APM Server host
  pageLoadTraceId: '${transaction.traceId}',
  pageLoadSpanId: '${transaction.ensureParentId()}',
  pageLoadSampled: ${transaction.sampled}
})

See the JavaScript RUM agent documentation for more information.

transaction.end([result])edit

transaction.end([result])

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

Alternatively you can call apm.endTransaction() to end the active transaction.

A value may be provided to set as a result.