Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from stalleyj/tidy
Browse files Browse the repository at this point in the history
Fix probes and add https
  • Loading branch information
tobespc authored Oct 19, 2017
2 parents 7b04c30 + d9c4a65 commit 3ce2254
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 254 deletions.
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "strongloop",
"env": {
"node": true
},
"parserOptions": { "ecmaVersion": 6 },
"rules": {
"eqeqeq": "off",
"max-len": ["error", 120, 8, {"ignoreComments": true}],
"comma-dangle": "off",
"spaced-comment": ["error", "always", { "exceptions": ["*"] }]
}
}
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "6"
- "4"
- "node"
33 changes: 18 additions & 15 deletions appmetrics-zipkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
var path = require("path")
var module_dir = path.dirname(module.filename)
'use strict';

var path = require('path');

var aspect = require('./lib/aspect.js');
var fs = require('fs');
var PropertyReader = require('properties-reader');
Expand All @@ -30,27 +32,24 @@ const {

// Load module probes into probes array by searching the probes directory.
var probes = [];
var options;

var dirPath = path.join(__dirname, 'probes');
var files = fs.readdirSync(dirPath);

var path = require('path');
var processName = '';

module.exports = function(options) {
options = options;
processName = path.basename(process.argv[1]);
if (processName.includes(".js")) {
if (processName.includes('.js')) {
processName = processName.substring(0, processName.length - 3);
}
files.forEach(function(fileName) {
var file = path.join(dirPath, fileName);
var probeModule = new(require(file))();
var probeModule = new (require(file))();
probes.push(probeModule);
});
start(options);
}
};

function start(options) {
// Set up the zipkin
Expand All @@ -65,10 +64,10 @@ function start(options) {
// Uses properties from file if present
if (properties){
if (properties.get('host')) {
host = properties.get('host');
host = properties.get('host');
}
if (properties.get('port')) {
port = properties.get('port');
port = properties.get('port');
}
}

Expand All @@ -83,14 +82,18 @@ function start(options) {
}

// Test if the host & port are valid
tcpp.probe(host, port, function(err, available){
if(!available){
tcpp.probe(host, port, function(err, available) {
if (err) {
console.log('Unable to contact Zipkin at ' + host + ':' + port);
return;
}
if (!available) {
console.log('Unable to contact Zipkin at ' + host + ':' + port);
}
});

const zipkinUrl = `http://${host}:${port}`;
recorder = new BatchRecorder({
const recorder = new BatchRecorder({
logger: new HttpLogger({
endpoint: `${zipkinUrl}/api/v1/spans`
})
Expand All @@ -102,7 +105,7 @@ function start(options) {
probe.setRecorder(recorder);
probe.setServiceName(serviceName);
probe.start();
// probe.enableRequests();
// probe.enableRequests();
});
}

Expand All @@ -111,7 +114,7 @@ function start(options) {
* for any matching module. This loads the monitoring probes into the modules
*/
var data = {};

/* eslint no-proto:0 */
aspect.after(module.__proto__, 'require', data, function(obj, methodName, args, context, ret) {
if (ret == null || ret.__ddProbeAttached__) {
return ret;
Expand Down
177 changes: 89 additions & 88 deletions lib/aspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,112 +13,113 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
'use strict';

exports.aroundCallback = function(args, context, hookBefore, hookAfter) {
var position = this.findCallbackArg(args);
if(position == undefined) return;
var position = this.findCallbackArg(args);
if (position == undefined) return;

var orig = args[position];
var orig = args[position];

args[position] = function() {
if(hookBefore) {
hookBefore(this, arguments, context);
}
args[position] = function() {
if (hookBefore) {
hookBefore(this, arguments, context);
}

var ret = orig.apply(this, arguments);
var ret = orig.apply(this, arguments);

if(hookAfter) {
hookAfter(this, arguments, context, ret);
}
return ret;
};
if (hookAfter) {
hookAfter(this, arguments, context, ret);
}
return ret;
};
};

exports.findCallbackArg = function(args) {
var position = undefined;
for (var i = 0; i < args.length; i++) {
if((typeof args[i] === 'function')) {
position = i;
break;
}
var position;
for (var i = 0; i < args.length; i++) {
if ((typeof args[i] === 'function')) {
position = i;
break;
}
return position;
}
return position;
};

exports.before = function(target, meths, hookBefore) {
if(!Array.isArray(meths)) {
meths = [meths];
}

meths.forEach(function(methodName) {
var existing = target[methodName];
if(!existing) return;

var newFunc = function() {
var context = {};
hookBefore(this, methodName, arguments, context);
return existing.apply(this, arguments);
};
newFunc.prototype = existing.prototype;

target[methodName] = newFunc;
});
if (!Array.isArray(meths)) {
meths = [meths];
}

meths.forEach(function(methodName) {
var existing = target[methodName];
if (!existing) return;

var newFunc = function() {
var context = {};
hookBefore(this, methodName, arguments, context);
return existing.apply(this, arguments);
};
newFunc.prototype = existing.prototype;

target[methodName] = newFunc;
});
};

exports.around = function(target, meths, hookBefore, hookAfter) {
if(!Array.isArray(meths)) {
meths = [meths];
}

meths.forEach(function(methodName) {
var existing = target[methodName];
if(!existing) return;

var newFunc = function() {
var context = {};
hookBefore(this, methodName, arguments, context);
var ret = existing.apply(this, arguments);
return hookAfter(this, methodName, arguments, context, ret);
};
newFunc.prototype = existing.prototype;

target[methodName] = newFunc;
});
if (!Array.isArray(meths)) {
meths = [meths];
}

meths.forEach(function(methodName) {
var existing = target[methodName];
if (!existing) return;

var newFunc = function() {
var context = {};
hookBefore(this, methodName, arguments, context);
var ret = existing.apply(this, arguments);
return hookAfter(this, methodName, arguments, context, ret);
};
newFunc.prototype = existing.prototype;
target[methodName] = newFunc;
});
};

exports.after = function(target, meths, context, hookAfter) {
if(!Array.isArray(meths)) {
meths = [meths];
}

meths.forEach(function(methodName) {
var existing = target[methodName];
if(!existing) return;

var newFunc = function() {
var ret = existing.apply(this, arguments);
return hookAfter(this, methodName, arguments, context, ret);
};
newFunc.prototype = existing.prototype;

target[methodName] = newFunc;
});
if (!Array.isArray(meths)) {
meths = [meths];
}

meths.forEach(function(methodName) {
var existing = target[methodName];
if (!existing) return;

var newFunc = function() {
var ret = existing.apply(this, arguments);
return hookAfter(this, methodName, arguments, context, ret);
};
newFunc.prototype = existing.prototype;

target[methodName] = newFunc;
});
};

exports.afterConstructor = function (target, context, hookAfter) {
if (typeof(target) === 'function') {
var newFunc = function() {
var ret = target.apply(null, arguments);
return hookAfter(this, '()', arguments, context, ret);
};
for (var property in target) {
if (target.hasOwnProperty(property)){
newFunc[property] = target[property];
}
}
newFunc.prototype = target.prototype;
newFunc.exports = target.exports;
return newFunc;
} else {
return target;
}
exports.afterConstructor = function(target, context, hookAfter) {
if (typeof (target) === 'function') {
var newFunc = function() {
var ret = target.apply(null, arguments);
return hookAfter(this, '()', arguments, context, ret);
};
for (var property in target) {
if (target.hasOwnProperty(property)){
newFunc[property] = target[property];
}
}
newFunc.prototype = target.prototype;
newFunc.exports = target.exports;
return newFunc;
} else {
return target;
}
};
Loading

0 comments on commit 3ce2254

Please sign in to comment.