RuntimeTools / appmetrics

Node Application Metrics provides a foundational infrastructure for collecting resource and performance monitoring data for Node.js-based applications.
https://developer.ibm.com/open/node-application-metrics/
Apache License 2.0
976 stars 127 forks source link

Add a Couchbase Probe #494

Open jakewarr8 opened 7 years ago

jakewarr8 commented 7 years ago

I wanted to add a couchbase-probe to time some of the common bucket methods from couchnode.

jakewarr8 commented 7 years ago
'use strict';

var Probe = require('../lib/probe.js');
var aspect = require('../lib/aspect.js');
var util = require('util');
var am = require('../');

function CouchbaseProbe() {
  Probe.call(this, 'couchbase');
}
util.inherits(CouchbaseProbe, Probe);

CouchbaseProbe.prototype.aspectBucketMethod = function(bucket, method) {
  var that = this;

  aspect.before(bucket, method, function(target, methodName, methodArgs, probeData) {
    that.metricsProbeStart(probeData, method, methodArgs);
    if (aspect.findCallbackArg(methodArgs) != undefined) {
      aspect.aroundCallback(methodArgs, probeData, function(target, args) {
        that.metricsProbeEnd(probeData, method, methodArgs);
      });
    }
  });
};

// Most used couchbase bucket methods
const bucketMethods = ['upsert', 'insert', 'replace', 'remove', 'get', 'getMulti'];

CouchbaseProbe.prototype.attach = function(name, target) {
  var that = this;
  if (name != 'couchbase') return target;
  if (target.__ddProbeAttached__) return target;
  target.__ddProbeAttached__ = true;

  var mock = target['Mock']['Cluster'].prototype;
  var cluster = target['Cluster'].prototype;

  var data = {};
  aspect.after(mock, 'openBucket', data, function(target, methodName, args, probeData, bucket) {
    for(key in bucketMethods) {
      that.aspectBucketMethod(bucket, bucketMethods[key]);
    }
    return bucket;
  });

  aspect.after(cluster, 'openBucket', data, function(target, methodName, args, probeData, bucket) {
    for(key in bucketMethods) {
      that.aspectBucketMethod(bucket, bucketMethods[key]);
    }
    return bucket;
  });

  return target;
};

/*
 * Lightweight metrics probe for couchbase queries
 *
 * These provide:
 *      time:         time event started
 *      bucket:     The bucket executed on
 *    method:   the method called on the bucket
 *      duration:   the time for the request to respond
 */
CouchbaseProbe.prototype.metricsEnd = function(probeData, method, methodArgs) {
  if (probeData && probeData.timer) {
    probeData.timer.stop();
    var eventTimer = probeData.timer;
    am.emit('couchbase', {
      time: eventTimer.startTimeMillis,
      bucket: methodArgs[0],
      method: method,
      duration: eventTimer.timeDelta,
    });
  }
};

module.exports = CouchbaseProbe;
tobespc commented 7 years ago

@astub Please go ahead and raise a pull request. We will need you to agree to the CLA terms https://github.com/RuntimeTools/appmetrics/blob/master/CONTRIBUTING.md before we can accept your request

NikCanvin commented 7 years ago

@astub.. what might be the benefits of visualising couchbase metrics on appmetrics-dash?

jakewarr8 commented 7 years ago

@NikCanvin it would be good to see response times and error rates if any occur when making couchbase queries.