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

added couchbase probe. #506

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Authors ordered by first contribution:
- Gibson Fahnestock (https://github.com/gibfahn)
- ? (https://github.com/nqvst)
- Vladislav Botvin (https://github.com/darky)
- Jake Warren (https://github.com/astub)
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Node Application Metrics provides the following built-in data collection sources
socket.io | WebSocket data sent and received by the application
LevelDB | LevelDB queries made by the application
MySQL | MySQL queries made by the application
Couchbase | Couchbase queries made by the application using [couchnode](https://github.com/couchbase/couchnode)
MongoDB | MongoDB queries made by the application
PostgreSQL | PostgreSQL queries made by the application
MQTT | MQTT messages sent and received by the application
Expand Down Expand Up @@ -335,6 +336,15 @@ Emitted when a data is stored, retrieved or modified in Memcached using the `mem
* `key` (String) the key associated with the data.
* `duration` (Number) the time taken for the operation on the memcached data to occur.

### Event: 'couchbase'
Emitted when a Couchbase query is made using the [couchnode](https://github.com/couchbase/couchnode) module.
* `data` (Object) the data from the MongoDB request:
* `time` (Number) the milliseconds when the Couchbase query was made. This can be converted to a Date using `new Date(data.time)`
* `bucket` (String) the bucket the query was made to in Couchbase.
* `duration` (Number) the time taken for the MongoDB query to be responded to in ms.
* `method` (String) the executed method for the query, such as 'query', 'upsert', 'insert', 'replace', 'remove', 'get', 'getMulti'.
* `error` (String) and error if any that occurred. is null if not error occurred.

### Event: 'mongo'
Emitted when a MongoDB query is made using the `mongodb` module.
* `data` (Object) the data from the MongoDB request:
Expand Down
96 changes: 96 additions & 0 deletions probes/couchbase-probe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*******************************************************************************
* Copyright 2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
'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, methodName, bucket._name, args[0]);
});
}
});
};

// Most used couchbase bucket methods
const bucketMethods = ['query', '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 = {};

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

// couchbase cluster
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, bucketName, err) {
if (probeData && probeData.timer) {
probeData.timer.stop();
var eventTimer = probeData.timer;
am.emit('couchbase', {
time: eventTimer.startTimeMillis,
bucket: bucketName,
method: method,
duration: eventTimer.timeDelta,
error: err
});
}
};


module.exports = CouchbaseProbe;
49 changes: 49 additions & 0 deletions tests/probes/couchbase-probe-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*******************************************************************************
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
'use strict';

// This script tests the client instance functionality of the couchnode module.
var appmetrics = require('appmetrics');
var tap = require('tap');

// Live monitoring
var amapi = appmetrics.monitor();

// Testing variables
var actualEvents = 0;
var expectedEvents = 2;

// all cb events
amapi.on('couchbase', function(response) {
actualEvents++;
});

var couchbase = require('couchbase').Mock;
var cluster = new couchbase.Cluster();
var bucket = cluster.openBucket();

bucket.upsert('testdoc', {name:'Frank'}, function(err, result) {
if (err) throw err;

bucket.get('testdoc', function(err, result) {
if (err) throw err;

console.log(result.value);
// {name: Frank}

tap.equals(actualEvents, expectedEvents, 'the callback was not called');
});
});