Transaction API | APM Node.js agent

Transaction API

Serverless Observability

APM Agent Node.js

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

APM Agent Node.js 0.1+

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

APM Agent Node.js 0.1+

Split components into type, subtype and action in: v3.0.0

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

APM Agent Node.js Deprecated 3.25+

The subtype of the transaction. The transaction subtype field is deprecated: it is not used and will be removed in the next major version.

transaction.action

APM Agent Node.js Deprecated 3.25+

The action of the transaction. The transaction action field is deprecated: it is not used and will be removed in the next major version.

transaction.traceparent

APM Agent Node.js 2.9+

Get the serialized traceparent string of the transaction.

transaction.result

APM Agent Node.js 0.1+

A string describing the result of the transaction. This is typically the HTTP status code, or e.g. "success" or "failure" for a background task.

transaction.startSpan([name][, type][, subtype][, action][, options])

APM Agent Node.js 2.0+

Split type into type, subtype and action in: v3.0.0

Start and return a new custom span associated with this transaction. 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.setLabel(name, value[, stringify = true])

APM Agent Node.js 0.1+

Renamed from transaction.setTag() to transaction.setLabel(): v2.10.0 Added stringify argument in: v3.11.0

transaction.setLabel('productId', 42, false);

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

Tip

Labels are key/value pairs that are indexed by Elasticsearch and therefore searchable (as opposed to data set via apm.setCustomContext()). Before using custom labels, ensure you understand the different types of metadata that are available.

Warning

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.

transaction.addLabels({ [name]: value }[, stringify = true])

APM Agent Node.js 1.5+

Renamed from transaction.addTags() to transaction.addLabels(): v2.10.0 Added stringify argument in: v3.11.0

transaction.addLabels({productId: 42, productName: 'butter'}, false);

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

Tip

Warning

transaction.ensureParentId()

APM Agent Node.js 2.0+

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',
  serverUrl: 'https://example.com:8200',
  pageLoadTraceId: '${transaction.traceId}',
  pageLoadSpanId: '${transaction.ensureParentId()}',
  pageLoadSampled: ${transaction.sampled}
})
  1. Name of your frontend app
  2. APM Server host

See the JavaScript RUM agent documentation for more information.

transaction.ids

APM Agent Node.js 2.17+

Produces an object containing transaction.id and trace.id. This enables log correlation to APM traces with structured loggers.

{
  "trace.id": "abc123",
  "transaction.id": "abc123"
}

transaction.end([result][, endTime])

APM Agent Node.js 0.1+

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

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

transaction.outcome

APM Agent Node.js 3.12+

The Node.js agent automatically sets an outcome property on transactions. This property will be one of three values:

A transaction is considered a success if the underlying HTTP request handling produces a response with a status code that is less than 500. A status code of 500 or greater is considered a failure.

Non-HTTP transactions will begin with an outcome of unknown.

transaction.setOutcome(outcome)

APM Agent Node.js 3.12+

The setOutcome method allows an end user to override the Node.js agent’s default setting of a transaction’s outcome property. The setOutcome method accepts a string of either success, failure, or unknown, and will force the agent to report this value for a specific span.

transaction.addLink(link)

APM Agent Node.js 4.7+

A transaction can refer to zero or more other transactions or spans (separate from its parent). Span links will be shown in the Kibana APM app trace view. The link argument is an object with a single "context" field that is a Transaction, Span, OpenTelemetry SpanContext object, or W3C trace-context traceparent string. For example: transaction.addLink({ context: anotherSpan }).

transaction.addLinks([links])

APM Agent Node.js 4.7+

Add span links to this transaction.

A transaction can refer to zero or more other transactions or spans (separate from its parent). Span links will be shown in the Kibana APM app trace view. The link argument is an object with a single "context" field that is a Transaction, Span, OpenTelemetry SpanContext object, or W3C trace-context traceparent string. For example: transaction.addLinks([{ context: anotherSpan }]).