API Reference
editAPI Reference
editAgent API
editYou can access agent API after initializing the agent:
var apm = require('elastic-apm-js-base').init(...)
apm.setUserContext()
editapm.setUserContext(context)
Call this method to enrich collected performance data and errors with information about the user.
The given context
argument must be an object and can contain the following properties (all optional):
-
id
- The users ID -
username
- The users username -
email
- The users e-mail
The provided user context is stored under context.user
in Elasticsearch on both errors and transactions.
apm.setCustomContext()
editapm.setCustomContext(context)
Call this to enrich collected errors and transactions with any information that you think will help you debug performance issues or errors.
The provided custom context is stored under context.custom
in Elasticsearch on both errors and transactions.
The given context
argument must be an object and can contain any property that can be JSON encoded.
apm.setTags()
editapm.setTags(tags)
Set tags on transactions and errors.
Tags are key/value pairs that are indexed by Elasticsearch and therefore searchable (as opposed to data set via setCustomContext()
). You can set multiple tags.
Arguments:
-
tags
- Any key/value object with the following specifications:-
key - Any string. Must not contain periods (
.
) as those 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.
-
key - Any string. Must not contain periods (
apm.addFilter()
editA filter can be used to modify the APM payload before it is sent to the apm-server. This can be useful in for example redacting sensitive information from the payload:
apm.addFilter(function (payload) { if (payload.errors) { payload.errors.forEach(function (error) { error.exception.message = error.exception.message.replace('secret', '[REDACTED]') }) } if (payload.transactions) { payload.transactions.forEach(function (tr) { tr.spans.forEach(function (span) { if (span.context && span.context.http && span.context.http.url) { var url = new URL(span.context.http.url) if (url.searchParams.get('token')) { url.searchParams.set('token', 'REDACTED') } span.context.http.url = url.toString() } }) }) } // Make sure to return the payload return payload })
The payload will be dropped if one of the filters return a falsy value.
apm.startTransaction()
editapm.startTransaction(name, type)
Starts and returns a new transaction.
Arguments:
-
name
- The name of the transaction (string). -
type
- The type of the transaction (string).
Use this method to create a custom transaction.
Calling this method would result in ending the current transaction and replacing it with the new transaction.
This method returns undefined
if apm is disabled or if isActive
is set to false
.
apm.startSpan()
editapm.startSpan(name, type)
Starts and returns a new span on the current transaction.
Arguments:
-
name
- The name of the span (string). -
type
- The type of the span (string).
This method returns undefined
if apm is disabled or if isActive
is set to false
.
apm.setInitialPageLoadName()
editapm.setInitialPageLoadName(name)
Arguments:
-
name
- The name of the page-load transaction (string).
Use this method to set the name of the page-load
transaction that is sent automatically on page load event.
apm.captureError()
editapm.captureError(error)
Arguments:
-
error
- An instance ofError
.
Use this method to manually send an error to APM Server:
apm.captureError(new Error('<error-message>'))
Transaction API
editA transaction groups multiple spans in a logical group.
To start a transaction,
you need to call apm.startTransaction()
.
To see an example of using custom transactions, see the Custom Transactions article.
transaction.name
edit- Type: String
-
Default:
Unknown
The name of the transaction.
Can be used to set or overwrite the name of the transaction (visible in the performance monitoring breakdown).
transaction.type
edit- Type: String
-
Default:
custom
The type of the transaction.
transaction.timestamp
edit- Type: String
-
Default:
undefined
The timestamp of the transaction.
If the transaction timestamp is not provided (the default behaviour), it will be set by the apm-server (v6.3+).
You can, however, set the timestamp on the client (using new Date().toISOString()
), but you should be aware that the timestamp will reflect the client’s local time
which might not always be accurate.
transaction.end()
edittransaction.end()
Ends the transaction. If the transaction has already ended, nothing happens.
transaction.mark(key)
edittransaction.mark(key)
Marks the current point in time relative to the start of the transaction. Use this method to mark significant events that happen while the transaction is active.