Span API
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes. The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless Observability
Generally available
Stack
Generally available
APM Agent Node.js
Generally available
A span measures the duration of a single event. When a span is created it will measure the time until span.end() is called.
To get a Span object, you need to call apm.startSpan().
To see an example of using custom spans, see the Custom Spans in Node.js article.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
0.1+
Generally available since 0.1
- Type: Transaction
A reference to the parent transaction object.
All spans belong to a transaction.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
0.1+
Generally available since 0.1
<string>Default:unnamed
The name of the span. This can also be set via apm.startSpan().
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
0.1+
Generally available since 0.1
Split components into type, subtype and action in: v3.0.0
<string>Default:custom
The type of span. This can also be set via apm.startSpan().
The type is used to group similar spans together. For instance, all spans of MySQL queries are given the type db, with a subtype of mysql and an action of query.
In the above example, db is considered the type. Though there are no naming restrictions for the type, the following are standardized across all Elastic APM agents: app, db, cache, template, and ext.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
0.1+
Generally available since 0.1
<string>Default:custom
The subtype of the span. This can also be set via apm.startSpan().
The subtype is typically the name of a module or library. For example, MySQL queries have a subtype of mysql.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
0.1+
Generally available since 0.1
<string>Default:custom
The action of the span. This can also be set via apm.startSpan().
The action is typically a specific function name or a general description of specific functionality. For example, a database query would generally have an action of query.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
2.9+
Generally available since 2.9
Get the serialized traceparent string of the span.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
2.1+
Generally available since 2.1
Renamed from span.setTag() to span.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 Elasticsearchvalue<string>|<number>|<boolean>If thestringifyargument is not given, or set totruethen the given value will be converted to a string.stringify<boolean>This defaults totruefor backwards compatibility, but new usage will typically wantfalse. When true, if a non-stringvalueis given, it is converted to a string before being sent to the APM Server.
span.setLabel('productId', 42, false);
Set a label on the span. You can set multiple labels on the same span.
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.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
2.1+
Generally available since 2.1
Renamed from span.addTags() to span.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 Elasticsearchvalue<string>|<number>|<boolean>If thestringifyargument is not given, or set totruethen the given value will be converted to a string.
stringify<boolean>This defaults totruefor backwards compatibility, but new usage will typically wantfalse. When true, if a non-stringvalueis given, it is converted to a string before being sent to the APM Server.
span.addLabels({productId: 42, productName: 'butter'}, false);
Add several labels on the span. You can add labels multiple times.
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.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
2.17+
Generally available since 2.17
Produces an object containing span.id and trace.id. This enables log correlation to APM traces with structured loggers.
{
"trace.id": "abc123",
"span.id": "abc123"
}
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
0.1+
Generally available since 0.1
endTime<number>The time when the span 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
End the span. If the span has already ended, nothing happens.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
3.12+
Generally available since 3.12
The Node.js agent automatically sets an outcome property on spans. This property will be one of three values:
success: Indicates the span’s operation was a success.failure: Indicates the span’s operation was not a success.unknown: Indicates the agent was unable to determine whether the span’s operation was a success or not. Anunknownoutcome removes a transaction from error rate considerations.
What constitutes a success or failure will depend on the span type.
For the general case, a span’s outcome is considered a failure if the Node.js agent captures an error during the execution of the work a span represents.
However, for exit spans that represent an HTTP request, the outcome is based on the status code of the HTTP response. A status code less than 400 is considered a success. A status code greater or equal to 400 is considered a failure.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
3.12+
Generally available since 3.12
outcome<string>
The setOutcome method allows an end user to override the Node.js agent’s default setting of a span’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.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
3.39+
Generally available since 3.39
type<string>| null The target service type, usually the same value asspan.subtype, e.g. "mysql".name<string>| null The target service name, an optional scoping of the service. For databases it is typically the database name.
Manually set the service.target.type and service.target.name fields that identify a downstream service. They are used for Service Maps and Dependencies in the Kibana APM app. The values are only used for "exit" spans — spans representing outgoing communication, marked with exitSpan: true at span creation.
If false-y values (e.g. null) are given for both type and name, then service.target will explicitly be excluded from this span. This may impact Service Maps and other Kibana APM app reporting for this service.
If this method is not called, the service target values are inferred from other span fields (spec).
service.target.* fields are ignored for APM Server before v8.3.
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
4.7+
Generally available since 4.7
link{{type-link}}
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 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: span.addLink({ context: anotherSpan }).
The Elastic APM Node.js agent enables you to trace the execution of operations in your Node.js applications, sending performance metrics and errors to the Elastic APM Server. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
APM Agent Node.js
4.7+
Generally available since 4.7
links<Array>Span links.
Add span links to this span.
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 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: span.addLinks([{ context: anotherSpan }]).