Opbeat is joining forces with Elastic – Read our blog post

Custom Transactions in Node.js

This is an example of how to use custom transactions. For general information about the Opbeat Node.js Transaction API, see the Transaction API documentation.

Opbeat 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, Opbeat 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 opbeat = require('opbeat').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
  msg.Body.Records.forEach(function (job) {
    // start one new transaction for each job record received
    // on the queue
    var name = 'Job ' + job.type
    var type = 'job'
    var trans = opbeat.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 ? 500 : 200

      // end the transaction
      trans.end()

      cb(err)
    })
  })
})

queue.on('error', function (err) {
  // in case the queue encounters an error, report it to Opbeat
  opbeat.captureError(err)
})