- APM Node.js Agent: other versions:
- Introduction
- Set up the Agent
- Monitoring AWS Lambda Node.js Functions
- Monitoring Node.js Azure Functions
- Get started with Express
- Get started with Fastify
- Get started with hapi
- Get started with Koa
- Get started with Next.js
- Get started with Restify
- Get started with TypeScript
- Get started with a custom Node.js stack
- Starting the agent
- Supported technologies
- Configuration
- API Reference
- Metrics
- Logs
- OpenTelemetry bridge
- OpenTracing bridge
- Source map support
- ECMAScript module support
- Distributed tracing
- Message queues
- Performance Tuning
- Troubleshooting
- Upgrading
- Release notes
Custom transactions
editCustom transactions
editThis is an example of how to use custom transactions. For general information about the Elastic APM Node.js Transaction API, see the Transaction API documentation.
The Elastic APM agent for Node.js instruments your application by grouping incoming HTTP requests into logical buckets. Each HTTP request is recorded in what we call a transaction. But if your application is not a regular HTTP server, the Node.js agent will not able to know when a transaction should start and when it ends.
If for instance your application is a background job processing worker or is only accepting WebSockets, you’ll need to manually start and end transactions.
Example of a background job application polling the SQS queuing system for jobs:
var apm = require('elastic-apm-node').start() var sqs = require('simple-sqs')() // listen for jobs on the queue var queue = sqs(queueUrl, function (msg, cb) { // The SQS queue will send multiple messages using an array // of records var tasks = msg.Body.Records.map(function (job) { return new Promise(function (resolve, reject) { // start one new transaction for each job record received // on the queue var name = 'Job ' + job.type var type = 'job' var trans = apm.startTransaction(name, type) // call the function that actually processes the job processJob(job, function (err) { // if the job could not be processes, set the result to // a 5xx error code. Here 500 indicates error, 200 is ok trans.result = err ? 'error' : 'success' // end the transaction trans.end() if (err) { reject(err) } else { resolve() } }) }) }) Promise.all(tasks).then(function () { cb() }, cb) }) queue.on('error', function (err) { // in case the queue encounters an error, report it to Elastic APM apm.captureError(err) })