Skip to content

Instantly share code, notes, and snippets.

@dasher
Created April 26, 2013 12:03
Show Gist options
  • Save dasher/5466975 to your computer and use it in GitHub Desktop.
Save dasher/5466975 to your computer and use it in GitHub Desktop.
// Relic integration point
/**
* Create a transactional scope in which instrumentation that will only add
* trace segments to existing transactions will funciton.
*
* @param Agent agent The agent whose tracer should be used to create the
* transaction.
* @param Function callback The function to be run within the transaction.
*/
var runInTransaction = function runInTransaction(agent, callback) {
return agent.tracer.transactionProxy(function () {
var transaction = agent.getTransaction();
callback(transaction);
})();
}
var Q = require("q");
var agent = require("newrelic");
var coinbase = "litecoin";
runInTransaction(agent, function transactionInScope(transaction) {
var iTX = agent.getTransaction();
var trace = transaction.getTrace();
var measurement = transaction.measure('Blockchain/'+coinbase+'/block');
// Start a timer
transaction.timer.begin();
console.log("transaction", transaction);
// a work unit
var delay = function (delay) {
var d = Q.defer();
setTimeout(
function() {
// stupid timeout to cause a little lag
console.log("metric", metric);
var metric = transaction
.metrics
.getOrCreateMetric('Blockchain/'+coinbase+'/block');
console.log("metric",metric);
var stats = measurement.stats;
if (stats.callCount < 1) {
console.log("!OK");
console.log("stats", stats);
}
d.resolve();
}
, delay);
return d.promise;
};
// setup some work to track
// simple promise based approach
Q.when(delay(1000), function () {
// stop the timer
trace.root.timer.end();
console.log("finalTx", transaction);
console.log("metrics", transaction.metrics);
console.log("unscoped", transaction.metrics.unscoped);
// trace first - it was started inside the tx
trace.end();
transaction.end();
}).then(
function(){
agent.stop();
}
);
});
@othiym23
Copy link

In general, you should only need line 72 – ending the transaction will also end the trace, which actually recursively closes all its trace segments (this is something of a hack, in that I don't know what would happen inside New Relic's presentation logic if there's a transaction trace with segments that end after the end of the transaction, but I'm reasonably confident it would be nothing good). If you look at https://github.com/newrelic/node-newrelic/blob/master/lib/instrumentation/core/http.js#L24-L32, you can see how we tie a segment's timer to the measurement process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment