Transaction API

edit

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

edit

Added in: v0.1.0

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

edit

Added in: v0.1.0

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 [v3.25.0] Deprecated in v3.25.0.

edit

Added in: v3.0.0
Deprecated in: v3.25.0

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 [v3.25.0] Deprecated in v3.25.0.

edit

Added in: v3.0.0
Deprecated in: v3.25.0

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

edit

Added in: v2.9.0

Get the serialized traceparent string of the transaction.

transaction.result

edit

Added in: v0.1.0

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])

edit

Added in: v2.0.0

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

  • name <string> The name of the span. You can alternatively set this via span.name. Default: unnamed
  • type <string> The type of the span. You can alternatively set this via span.type.
  • subtype <string> The subtype of the span. You can alternatively set this via span.subtype.
  • action <string> The action of the span. You can alternatively set this via span.action.
  • options - The following options are supported:

    • startTime <number> The time when the span started. Must be a Unix Time Stamp representing the number of milliseconds since January 1, 1970, 00:00:00 UTC. Sub-millisecond precision can be achieved using decimals. If not provided, the current time will be used
    • exitSpan <boolean> Make an "exit span". Exit spans represent outgoing communication. They are used to create a node in the Service Map and a downstream service in the Dependencies Table. The provided subtype will be used as the downstream service name.
    • links <Array> Span links. A span 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 links argument is an array of objects with a single "context" field that is a Transaction, Span, or W3C trace-context traceparent string. For example: transaction.startSpan('aName', { links: [{ context: anotherSpan }] }).

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])

edit

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

  • name <string> Any periods (.), asterisks (*), or double quotation marks (") will be replaced by underscores (_), as those characters have special meaning in Elasticsearch
  • value <string> | <number> | <boolean>
  • stringify <boolean> Defaults to true. When true, if a non-string value is given, it is converted to a string before being sent to the APM Server

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.

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

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

edit

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

  • labels <Object> Contains key/value pairs:

    • name <string> Any periods (.), asterisks (*), or double quotation marks (") will be replaced by underscores (_), as those characters have special meaning in Elasticsearch
    • value <string> | <number> | <boolean>
  • stringify <boolean> Defaults to true. When true, if a non-string value is given, it is converted to a string before being sent to the APM Server.

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.

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

transaction.ensureParentId()

edit

Added in: v2.0.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', // 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.ids

edit

Added in: v2.17.0

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.toString() [v3.23.0] Deprecated in v3.23.0.

edit

Added in: v2.17.0
Deprecated in: v3.23.0

Produces a string representation of the transaction to inject in log messages. This enables log correlation to APM traces with text-only loggers.

"trace.id=abc123 transaction.id=abc123"

Relying on the format of transaction.toString() has been deprecated and may change in v4 of the agent. Prefer the use of transaction.ids or apm.currentTraceIds. The v3 format may be reproduced via:

const { stringify } = require('querystring')
console.log( stringify(transaction.ids, ' ', '=')) )

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

edit

Added in: v0.1.0

  • result <string> Describes the result of the transaction. This is typically the HTTP status code, or e.g. "success" or "failure" for a background task
  • endTime <number> The time when the transaction ended. Must be a Unix Time Stamp representing the number of milliseconds since January 1, 1970, 00:00:00 UTC. Sub-millisecond precision can be achieved using decimals. If not provided, the current time will be used

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

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

transaction.outcome

edit

Added in: v3.12.0

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

  • success: Indicates the transaction’s operation was a success.
  • failure: Indicates the transaction’s operation was not a success.
  • unknown: Indicates we were unable to determine if the transaction’s operation was a success or not. An unknown outcome removes a transaction from error rate considerations.

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)

edit

Added in: v3.12.0

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.