Transaction API
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.
Added in: v0.1.0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_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.
Added in: v0.1.0
Split components into type
, subtype
and action
in: v3.0.0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_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.
Added in: v3.0.0
Deprecated in: v3.25.0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
Default:custom
The subtype of the transaction. The transaction subtype
field is deprecated: it is not used and will be removed in the next major version.
Added in: v3.0.0
Deprecated in: v3.25.0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
Default:custom
The action of the transaction. The transaction action
field is deprecated: it is not used and will be removed in the next major version.
Added in: v2.9.0
Get the serialized traceparent string of the transaction.
Added in: v0.1.0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
Default:success
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.
Added in: v2.0.0
Split type
into type
, subtype
and action
in: v3.0.0
name
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
The name of the span. You can alternatively set this viaspan.name
. Default:unnamed
type
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
The type of the span. You can alternatively set this viaspan.type
.subtype
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
The subtype of the span. You can alternatively set this viaspan.subtype
.action
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
The action of the span. You can alternatively set this viaspan.action
.options
- The following options are supported:startTime
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type[<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 usedexitSpan
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type[<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
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array[<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. Thelinks
argument is an array of objects with a single "context" field that is aTransaction
,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.
Added in: v0.1.0
Renamed from transaction.setTag()
to transaction.setLabel()
: v2.10.0
Added stringify
argument in: v3.11.0
name
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
Any periods (.
), asterisks (*
), or double quotation marks ("
) will be replaced by underscores (_
), as those characters have special meaning in Elasticsearchvalue
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type[<number>]
|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type[<boolean>]
If thestringify
argument is not given, or set totrue
then the given value will be converted to a string.stringify
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type[<boolean>]
This defaults totrue
for backwards compatibility, but new usage will typically wantfalse
. When true, if a non-stringvalue
is given, it is converted to a string before being sent to the APM Server.
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.
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.
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.
Added in: v1.5.0
Renamed from transaction.addTags()
to transaction.addLabels()
: v2.10.0
Added stringify
argument in: v3.11.0
labels
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object[<Object>]
Contains key/value pairs:name
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
Any periods (.
), asterisks (*
), or double quotation marks ("
) will be replaced by underscores (_
), as those characters have special meaning in Elasticsearchvalue
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type[<number>]
|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type[<boolean>]
If thestringify
argument is not given, or set totrue
then the given value will be converted to a string.
stringify
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type[<boolean>]
This defaults totrue
for backwards compatibility, but new usage will typically wantfalse
. When true, if a non-stringvalue
is given, it is converted to a string before being sent to the APM Server.
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.
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.
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.
Added in: v2.0.0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_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 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<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',1
serverUrl: 'https://example.com:8200',2
pageLoadTraceId: '${transaction.traceId}',
pageLoadSpanId: '${transaction.ensureParentId()}',
pageLoadSampled: ${transaction.sampled}
})
- Name of your frontend app
- APM Server host
See the JavaScript RUM agent documentation for more information.
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"
}
Added in: v0.1.0
result
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
Describes the result of the transaction. This is typically the HTTP status code, or e.g. "success" or "failure" for a background taskendTime
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type[<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.
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. Anunknown
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
.
Added in: v3.12.0
outcome
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
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.
Added in: v4.7.0
link
{{type-link}}
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 })
.
Added in: v4.7.0
links
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array[<Array>]
Span links.
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 }])
.