apminsight
Version:
monitor nodejs applications
82 lines (74 loc) • 2.47 kB
JavaScript
var wrapper = require("./../wrapper");
var logger = require("./../../util/logger");
var componentName = "REDIS";
var moduleInfo = {
functions: [
{
functionName: ["prototype.sendCommand"],
component: componentName,
wrapper: wrapSendCommand
},
{
functionName: ["Command.prototype.toWritable"],
component: componentName,
wrapper: wrapWrite
}
]
};
function wrapSendCommand(actual) {
return function (command) {
var curTxn = apmInsightAgentInstance.getCurTxn();
if (!curTxn || curTxn.isCompleted()) {
return actual.apply(this, arguments);
}
if (!command || !command.name) {
return actual.apply(this, arguments);
}
var parentTracker = apmInsightAgentInstance.getCurTracker();
var trackerName = componentName + " - " + command.name;
var trackerInfo = {
trackerName: trackerName,
component: componentName,
stale: true,
parent: parentTracker
};
var curTracker = apmInsightAgentInstance.createTracker(trackerInfo);
appendMachineInfo(this, curTracker);
try {
var result = actual.apply(this, arguments);
result = wrapper.checkAndWrapPromise(result, curTxn, curTracker);
return result;
} catch (e) {
logger.error("ioredis send command handler", e);
apmInsightAgentInstance.handleErrorTracker(curTracker, e);
throw e;
} finally {
apmInsightAgentInstance.setCurTracker(parentTracker);
}
};
}
function wrapWrite(actual) {
return function () {
var parentTracker = apmInsightAgentInstance.getCurTracker();
if (isRedisTracker(parentTracker)) {
parentTracker.setActive();
}
return actual.apply(this, arguments);
};
}
function appendMachineInfo(invoker, curTracker) {
try {
if (invoker && curTracker) {
var hostInfo = {};
hostInfo.host = invoker.options.host;
hostInfo.port = invoker.options.port;
curTracker.updateInfo(hostInfo);
}
} catch (error) {
logger.error("Error while getting the machine info of ioredis.");
}
}
function isRedisTracker(tracker) {
return tracker && tracker.getComponent() === componentName;
}
module.exports = moduleInfo;