Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Couchbase Probe #494

Open
jakewarr8 opened this issue Nov 2, 2017 · 4 comments
Open

Add a Couchbase Probe #494

jakewarr8 opened this issue Nov 2, 2017 · 4 comments

Comments

@jakewarr8
Copy link

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

@jakewarr8
Copy link
Author

jakewarr8 commented Nov 2, 2017

'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
Copy link
Member

tobespc commented Nov 3, 2017

@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
Copy link
Collaborator

NikCanvin commented Nov 3, 2017

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

@jakewarr8
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants