Type to search.

Custom transactions

Serverless Observability

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    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.

APM Agent Node.js

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

  • Generally available

    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.

This 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:

JavaScript
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)
})