newrelic/node-newrelic — pull request #189 by mgartner (2014-12-17 07:47:09+00:00)
Title: Proposal for better transaction API

This is a proposal for a change of the API to support a more Javascript-esque style. Motivation for this proposal comes from my own experience failing to get background transaction tracing working with Bluebird promises. The magical finding of the current transaction in `newrelic.endTransaction()` is unnecessary, confusing, and broken. This idea is also shared by others: https://discuss.newrelic.com/t/createbackgroundtransaction-strange-behavior/10416/2

Obviously this isn't functional, it's just a proposal of how the `createBackgroundTransaction` interface should look. Usage of this change would look something like this.

```javascript
var newrelic = require('newrelic');

newrelic.createBackgroundTransaction('a:txn', function (tx) {
doSomethingAsync(function () {
tx.end();
});
});
```

Modifying the interface to allow this *should* allow instrumenting transactions while using promises, even without adding first-class promise support:

```javascript
newrelic.createBackgroundTransaction('a:txn', function (tx) {
fs.readFileAsync('file.json')
.then(JSON.parse)
.then(function(val) {
console.log(val.success);
})
.catch(SyntaxError, function(e) {
console.error("invalid json in file");
})
.finally(function () {
tx.end()
});
});
```