Skip to content

Instantly share code, notes, and snippets.

@dasher
Created April 26, 2013 12:03
Show Gist options
  • Select an option

  • Save dasher/5466975 to your computer and use it in GitHub Desktop.

Select an option

Save dasher/5466975 to your computer and use it in GitHub Desktop.

Revisions

  1. dasher created this gist Apr 26, 2013.
    78 changes: 78 additions & 0 deletions RelicMonitoring - background or external
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    // 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();
    }
    );
    });