Created
April 26, 2013 12:03
-
-
Save dasher/5466975 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
); | |
}); |
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
On line 27, you should defer the measurement until you have the time elapsed, and pass that time into the measure call, making sure to pass
null
for the scope.Transaction.prototype.measure
isn't inserting a probe so much as generating the metric once there's enough information to capture its metadata.