Comparing version 0.1.4 to 0.1.5
@@ -15,1 +15,2 @@ /** | ||
exports.NodeAgent = NW; | ||
exports.Tracer = require('./lib/trace/trace-context-manager'); |
@@ -49,91 +49,103 @@ /** | ||
p.active_tx_slice = [0,0,0]; | ||
var en = TraceContextManager.getContextEnumeration(); | ||
while(en.hasMoreElements()) { | ||
var ctx = en.nextElement(); | ||
if(ctx == null) { return false; } | ||
try{ | ||
while(en.hasMoreElements()) { | ||
var ctx = en.nextElement(); | ||
if(ctx == null) { return false; } | ||
var actStack = new ActiveStackPack(); | ||
actStack.time = Long.fromValue(p.time); | ||
actStack.seq = KeyGen.next(); | ||
actStack.profile_seq = Long.fromValue(ctx.profile_seq); | ||
actStack.service = ctx.service_hash; | ||
actStack.remote_ip = ctx.remoteIp; | ||
actStack.userid = ctx.userid; | ||
actStack.elapsed = ctx.getElapsedTime(); | ||
actStack.sqlCount = ctx.sql_count; | ||
actStack.sqlTime = ctx.sql_time; | ||
actStack.httpcCount = ctx.httpc_count; | ||
actStack.httpcTime = ctx.httpc_time; | ||
actStack.thread_id = ctx.thread_id; | ||
actStack.callstack = []; | ||
actStack.cpuTime = ResourceProfile.getCPUTime().sub(ctx.start_cpu).toNumber(); | ||
actStack.malloc = ResourceProfile.getUsedHeapSize().sub(ctx.start_malloc).toNumber(); | ||
if(actStack.cpuTime < 0) { | ||
actStack.cpuTime = 0; | ||
} | ||
if(actStack.malloc < 0) { | ||
actStack.malloc = 0; | ||
} | ||
var actStack = new ActiveStackPack(); | ||
actStack.time = Long.fromValue(p.time); | ||
actStack.seq = KeyGen.next(); | ||
actStack.profile_seq = Long.fromValue(ctx.profile_seq); | ||
actStack.service = ctx.service_hash; | ||
actStack.remote_ip = ctx.remoteIp; | ||
actStack.userid = ctx.userid; | ||
actStack.elapsed = ctx.getElapsedTime(); | ||
actStack.sqlCount = ctx.sql_count; | ||
actStack.sqlTime = ctx.sql_time; | ||
actStack.httpcCount = ctx.httpc_count; | ||
actStack.httpcTime = ctx.httpc_time; | ||
actStack.thread_id = ctx.thread_id; | ||
actStack.callstack = []; | ||
actStack.cpuTime = ResourceProfile.getCPUTime().sub(ctx.start_cpu).toNumber(); | ||
actStack.malloc = ResourceProfile.getUsedHeapSize().sub(ctx.start_malloc).toNumber(); | ||
if(callstack_enabled) { | ||
for (var i = 0; i < ctx.stack.length; i++) { | ||
actStack.callstack[i] = HashUtil.hashFromString(ctx.stack[i]); | ||
actStack.callstack_hash ^= actStack.callstack[i]; | ||
DataTextAgent.STACK.add(actStack.callstack[i], ctx.stack[i]); | ||
if(actStack.cpuTime < 0) { | ||
actStack.cpuTime = 0; | ||
} | ||
if(actStack.callstack.length == 1) { | ||
actStack.callstack.unshift(0); | ||
if(actStack.malloc < 0) { | ||
actStack.malloc = 0; | ||
} | ||
actUniqStackSet.add(actStack.callstack_hash); | ||
} | ||
if (ctx.active_sqlhash != 0) { | ||
actStack.act_dbc = ctx.active_dbc; | ||
actStack.act_sql = ctx.active_sqlhash; | ||
actStack.act_crud = ctx.active_crud; | ||
StatSql.addSqlActive(actStack.act_dbc, actStack.act_sql); | ||
} else if (ctx.active_httpc_hash != 0) { | ||
actStack.act_httpc = ctx.active_httpc_hash; | ||
} | ||
if(callstack_enabled) { | ||
for (var i = 0; i < ctx.stack.length; i++) { | ||
actStack.callstack[i] = HashUtil.hashFromString(ctx.stack[i]); | ||
actStack.callstack_hash ^= actStack.callstack[i]; | ||
DataTextAgent.STACK.add(actStack.callstack[i], ctx.stack[i]); | ||
} | ||
if(actStack.callstack.length == 1) { | ||
actStack.callstack.unshift(0); | ||
} | ||
actUniqStackSet.add(actStack.callstack_hash); | ||
} | ||
if (actStack.callstack != null && actStack.length > 0) { | ||
ctx.profileActive++; | ||
if (ctx.active_sqlhash != 0) { | ||
actStack.act_dbc = ctx.active_dbc; | ||
actStack.act_sql = ctx.active_sqlhash; | ||
actStack.act_crud = ctx.active_crud; | ||
StatSql.addSqlActive(actStack.act_dbc, actStack.act_sql); | ||
} else if (ctx.active_httpc_hash != 0) { | ||
actStack.act_httpc = ctx.active_httpc_hash; | ||
} | ||
var step = new ActiveStackStep(actStack.seq); | ||
step.start_time = ctx.getElapsedTime(); | ||
step.hasCallstack = true; | ||
ctx.profile.add(step); | ||
} | ||
if (actStack.callstack != null && actStack.length > 0) { | ||
ctx.profileActive++; | ||
if (actStack.elapsed < conf.trace_active_transaction_yellow_time) { | ||
p.active_tx_slice[0]++; | ||
} else if (actStack.elapsed < conf.trace_active_transaction_red_time) { | ||
p.active_tx_slice[1]++; | ||
} else { | ||
p.active_tx_slice[2]++; | ||
if (actStack.elapsed >= conf.trace_active_transaction_hang_time) { | ||
p.act_svc_hang++; | ||
var step = new ActiveStackStep(actStack.seq); | ||
step.start_time = ctx.getElapsedTime(); | ||
step.hasCallstack = true; | ||
ctx.profile.add(step); | ||
} | ||
} | ||
p.active_tx_count++; | ||
if (actStack.callstack_hash != 0) { | ||
actUniqStackSet.add(actStack.callstack_hash); | ||
if (actStack.elapsed < conf.trace_active_transaction_yellow_time) { | ||
p.active_tx_slice[0]++; | ||
} else if (actStack.elapsed < conf.trace_active_transaction_red_time) { | ||
p.active_tx_slice[1]++; | ||
} else { | ||
p.active_tx_slice[2]++; | ||
if (actStack.elapsed >= conf.trace_active_transaction_hang_time) { | ||
p.act_svc_hang++; | ||
} | ||
} | ||
p.active_tx_count++; | ||
if (actStack.callstack_hash != 0) { | ||
actUniqStackSet.add(actStack.callstack_hash); | ||
} | ||
actUniqServiceSet.add(actStack.service); | ||
if (ctx.userid != 0) { | ||
actUser.add(ctx.userid); | ||
MeterUsers.add(ctx.userid); | ||
} | ||
if (ctx.remoteIp != 0) { | ||
actIpSet.add(ctx.remoteIp); | ||
} | ||
if (conf.active_stack_enabled) { | ||
DataPackSender.sendActiveStackPack(actStack); | ||
} | ||
} | ||
actUniqServiceSet.add(actStack.service); | ||
if (ctx.userid != 0) { | ||
actUser.add(ctx.userid); | ||
MeterUsers.add(ctx.userid); | ||
} catch (e) { | ||
if(Array.isArray(p.active_tx_slice)) { | ||
if(p.active_tx_slice.length != 3) { | ||
p.active_tx_slice = [0,0,0]; | ||
} | ||
} else { | ||
p.active_tx_slice = [0,0,0]; | ||
} | ||
if (ctx.remoteIp != 0) { | ||
actIpSet.add(ctx.remoteIp); | ||
} | ||
} | ||
if (conf.active_stack_enabled) { | ||
DataPackSender.sendActiveStackPack(actStack); | ||
} | ||
} | ||
}; | ||
module.exports = ActiveTransaction; |
@@ -122,3 +122,3 @@ /** | ||
} | ||
ctx.service_hash = hashUtil.hashFromString(ctx.service_name); | ||
@@ -190,3 +190,3 @@ DataTextAgent.SERVICE.add(ctx.service_hash, ctx.service_name); | ||
} | ||
if(header_enabled) { | ||
@@ -322,7 +322,6 @@ var step = new MessageStep(); | ||
ctx.active_httpc_hash = step.url; | ||
ctx.profile.push(step); | ||
lctx.step = step; | ||
} catch(e) { | ||
} catch(e) { return; } | ||
} | ||
aop.functionHook(args, -1, function(obj, args) { | ||
@@ -354,4 +353,2 @@ if(TraceContextManager.resume(ctx._id) == null) { return; } | ||
var step = lctx.step; | ||
ctx.profile.push(step); | ||
ret.on('error', function(err) { | ||
@@ -358,0 +355,0 @@ if (TraceContextManager.resume(ctx._id) == null) { return; } |
@@ -30,3 +30,3 @@ /** | ||
var queryHook = function (dbc_hash, agent) { | ||
return function (obj, args) { | ||
@@ -36,3 +36,3 @@ var ctx = TraceContextManager.getCurrentContext(); | ||
if(args[0] == null || typeof args[0] != 'string') { return; } | ||
var error_stack = new Error().stack.split('\n'); | ||
@@ -61,3 +61,3 @@ StatError.error[ctx.service_hash] = error_stack; | ||
} | ||
if(psql != null) { | ||
@@ -69,3 +69,2 @@ sql_step.hash = psql.sql; | ||
var els = new EscapeLiteralSQL(sql); | ||
@@ -83,11 +82,15 @@ els.process(); | ||
TraceContextManager.resume(ctx._id); | ||
if(args[0]) { | ||
var msgObj = { 'class': args[0].code, 'msg': args[0].message }; | ||
sql_step.error = StatError.addError({}, msgObj, ctx.service_hash, ctx.profile, TextTypes.SQL, sql_step.hash); | ||
if(ctx.error.isZero()) { | ||
ctx.error = Long.fromValue(sql_step.error); | ||
try{ | ||
var msgObj = { 'class': (args[0].code || 'mysql'), 'msg': (args[0].message || 'mysql error') }; | ||
sql_step.error = StatError.addError({}, msgObj, ctx.service_hash, ctx.profile, TextTypes.SQL, sql_step.hash); | ||
if(ctx.error.isZero()) { | ||
ctx.error = Long.fromValue(sql_step.error); | ||
} | ||
}catch(e) { | ||
} | ||
} | ||
sql_step.elapsed = ctx.getElapsedTime() - sql_step.start_time; | ||
@@ -121,7 +124,7 @@ ctx.sql_time += sql_step.elapsed; | ||
var errorDelegate = function (ctx) { | ||
var errorDelegate = function () { | ||
return function (obj, args) { | ||
var ctx = TraceContextManager.getCurrentContext(); | ||
if(ctx == null) { return; } | ||
var laststep = ctx.profile.getLastSteps(1); | ||
@@ -136,11 +139,14 @@ if(laststep == null || laststep.length == 0) { return; } | ||
var msgObj = { 'class': args[0].code, 'msg': args[0].message }; | ||
if(args[0].fatal) { | ||
// Connection Error | ||
step.error = StatError.addError({}, msgObj, ctx.service_hash, ctx.profile, 0, 0); /*long*/ | ||
} else { | ||
msgObj = { 'class': args[0].code, 'msg': args[0].message }; | ||
step.error = StatError.addError({}, msgObj, ctx.service_hash, ctx.profile, TextTypes.SQL, step.hash); /*long*/ | ||
try{ | ||
var msgObj = { 'class': args[0].code, 'msg': args[0].message }; | ||
if(args[0].fatal) { | ||
// Connection Error | ||
step.error = StatError.addError({}, msgObj, ctx.service_hash, ctx.profile, 0, 0); /*long*/ | ||
} else { | ||
msgObj = { 'class': args[0].code, 'msg': args[0].message }; | ||
step.error = StatError.addError({}, msgObj, ctx.service_hash, ctx.profile, TextTypes.SQL, step.hash); /*long*/ | ||
} | ||
if (ctx.error.isZero()) { ctx.error = step.error; } | ||
} catch(e) { | ||
} | ||
if (ctx.error.isZero()) { ctx.error = step.error; } | ||
} | ||
@@ -155,5 +161,5 @@ }; | ||
var aop = self.agent.aop; | ||
if( conf.trx_profile_sql_enabled == false ) { return; } | ||
aop.both(mod, 'createConnection', | ||
@@ -202,3 +208,3 @@ function (obj, args, lctx) { | ||
ctx.stack.push('Mysql Connect'); | ||
aop.functionHook(args, -1, function (obj, args) { | ||
@@ -216,4 +222,3 @@ if(args[0] && step.error.isZero()) { | ||
aop.before(ret, 'query', queryHook(dbc_hash, self.agent)); | ||
// aop.after(ret._protocol, '_delegateError', errorDelegate()); | ||
aop.after(ret._protocol, '_delegateError', errorDelegate()); | ||
}); | ||
@@ -233,3 +238,2 @@ | ||
dbc_hash = HashUtil.hashFromString(dbc); | ||
DataTextAgent.DBC.add(dbc_hash, dbc); | ||
@@ -242,13 +246,11 @@ aop.both(ret, 'getConnection', function (obj, args) { | ||
TraceContextManager.resume(ctx._id); | ||
DataTextAgent.DBC.add(dbc_hash, dbc); | ||
if(args[0] != null) { return; } | ||
var conn = args[1]; | ||
if(conn.__query_hook__) { return; } | ||
conn.__query_hook__ = true; | ||
aop.before(conn, 'query', queryHook(dbc_hash, self.agent)); | ||
// aop.before(conn._protocol, '_delegateError', errorDelegate(ctx)); | ||
aop.before(conn._protocol, '_delegateError', errorDelegate(ctx)); | ||
}); | ||
}, function (obj, args) { | ||
@@ -255,0 +257,0 @@ |
@@ -44,7 +44,8 @@ /** | ||
step.port = port; | ||
obj.on('lookup', function(err, ip, addressType){ | ||
step.ipaddr = new Buffer(IPUtil.toBytes(ip)); | ||
if(step != null) { | ||
step.ipaddr = new Buffer(IPUtil.toBytes(ip)); | ||
} | ||
}); | ||
ctx.profile.push(step); | ||
lctx.step = step; | ||
@@ -58,3 +59,2 @@ }, | ||
step.elapsed = ctx.getElapsedTime() - step.start_time; | ||
ctx.profile.push(step); | ||
ctx.stack.pop(); | ||
@@ -61,0 +61,0 @@ } |
@@ -36,3 +36,3 @@ /** | ||
this.value = din.readDecimal(); | ||
this.desc = din.zreadText(); | ||
this.desc = din.readText(); | ||
return this; | ||
@@ -39,0 +39,0 @@ }; |
@@ -8,2 +8,3 @@ /** | ||
var IntKeyMap = require('../util/intkey-map'), | ||
HashUtil = require('../util/hashutil'), | ||
TraceContext = require('./trace-context'), | ||
@@ -13,62 +14,61 @@ Long = require('long'); | ||
function TraceContextManager() { | ||
this.initialized = false; | ||
this.nextId = 1; | ||
this.currentId = undefined; | ||
this.entry = new IntKeyMap(); | ||
this.local = {}; | ||
} | ||
TraceContextManager.initialized = false; | ||
TraceContextManager.nextId = 1; | ||
TraceContextManager.currentId = undefined; | ||
TraceContextManager.entry = new IntKeyMap(); | ||
TraceContextManager.local = {}; | ||
TraceContextManager.keys = function () { | ||
return TraceContextManager.entry.keys(); | ||
TraceContextManager.prototype.keys = function () { | ||
return this.entry.keys(); | ||
}; | ||
TraceContextManager.getContextEnumeration = function () { | ||
return TraceContextManager.entry.values(); | ||
TraceContextManager.prototype.getContextEnumeration = function () { | ||
return this.entry.values(); | ||
}; | ||
TraceContextManager.size = function () { | ||
return TraceContextManager.entry.size(); | ||
TraceContextManager.prototype.size = function () { | ||
return this.entry.size(); | ||
}; | ||
TraceContextManager.getContext = function (key) { | ||
TraceContextManager.prototype.getContext = function (key) { | ||
if(key == null) { | ||
return null; | ||
} | ||
return TraceContextManager.entry.get(key); | ||
return this.entry.get(key); | ||
}; | ||
TraceContextManager.getCurrentContext = function () { | ||
if(TraceContextManager.entry.containsKey(TraceContextManager.currentId)) | ||
return TraceContextManager.getContext(TraceContextManager.currentId); | ||
TraceContextManager.prototype.getCurrentContext = function () { | ||
if(this.entry.containsKey(this.currentId)) | ||
return this.getContext(this.currentId); | ||
else | ||
return null; | ||
}; | ||
TraceContextManager.start = function () { | ||
if(TraceContextManager.initialized == false) { return null; } | ||
TraceContextManager.prototype.start = function () { | ||
if(this.initialized == false) { return null; } | ||
var conf = require('../conf/configure'); | ||
if(TraceContextManager.size() > conf.trx_profile_max_count) { return null; } | ||
if(this.size() > conf.trx_profile_max_count) { return null; } | ||
var ctx = new TraceContext(TraceContextManager.getNextId()); | ||
var ctx = new TraceContext(this.getNextId()); | ||
ctx.start_time = Long.fromNumber(Date.now()); | ||
TraceContextManager.entry.put(ctx._id, ctx); | ||
TraceContextManager.currentId = ctx._id; | ||
this.entry.put(ctx._id, ctx); | ||
this.currentId = ctx._id; | ||
return ctx; | ||
}; | ||
TraceContextManager.end = function (id) { | ||
TraceContextManager.prototype.end = function (id) { | ||
if(id == null) { | ||
TraceContextManager.entry.remove(TraceContextManager.currentId); // important: high risky | ||
TraceContextManager.currentId = undefined; | ||
this.entry.remove(this.currentId); // important: high risky | ||
this.currentId = undefined; | ||
} else { | ||
TraceContextManager.entry.remove(id); | ||
this.entry.remove(id); | ||
return; | ||
} | ||
}; | ||
TraceContextManager.getNextId = function () { | ||
TraceContextManager.nextId = (TraceContextManager.nextId + 1) % 10000000; | ||
return TraceContextManager.nextId; | ||
TraceContextManager.prototype.getNextId = function () { | ||
this.nextId = (this.nextId + 1) % 10000000; | ||
return this.nextId; | ||
}; | ||
TraceContextManager.getCurrentId = function () { | ||
return TraceContextManager.currentId; | ||
TraceContextManager.prototype.getCurrentId = function () { | ||
return this.currentId; | ||
}; | ||
TraceContextManager.resume = function (id) { | ||
TraceContextManager.prototype.resume = function (id) { | ||
if(id instanceof TraceContext) { | ||
@@ -78,11 +78,11 @@ id = id._id; | ||
if(this.entry.containsKey(id)) { | ||
TraceContextManager.currentId = id; | ||
return TraceContextManager.getCurrentContext(); | ||
this.currentId = id; | ||
return this.getCurrentContext(); | ||
} else { | ||
TraceContextManager.currentId = undefined; | ||
this.currentId = undefined; | ||
return null; | ||
} | ||
}; | ||
TraceContextManager.getTxProfile = function(n) { | ||
var ctx = TraceContextManager.getCurrentContext(); | ||
TraceContextManager.prototype.getTxProfile = function(n) { | ||
var ctx = this.getCurrentContext(); | ||
if(ctx == null) | ||
@@ -92,6 +92,34 @@ return null; | ||
}; | ||
TraceContextManager.isExist = function (id) { | ||
return TraceContextManager.entry.containsKey(id); | ||
TraceContextManager.prototype.isExist = function (id) { | ||
return this.entry.containsKey(id); | ||
}; | ||
module.exports = TraceContextManager; | ||
TraceContextManager.prototype.addStep = function (title, desc) { | ||
var ctx = this.getCurrentContext(); | ||
if(ctx == null) { | ||
return null; | ||
} | ||
try { | ||
var DataTextAgent = require('../data/datatext-agent'); | ||
var MessageStep = require('../step/message-step'); | ||
var step = new MessageStep(); | ||
step.start_time = ctx.getElapsedTime(); | ||
step.hash = HashUtil.hashFromString(title); | ||
DataTextAgent.MESSAGE.add(step.hash, title); | ||
step.desc = desc; | ||
step.ctx = ctx; | ||
ctx.profile.push(step); | ||
return step; | ||
} catch(e) { | ||
return null; | ||
} | ||
}; | ||
TraceContextManager.prototype.endStep = function (step) { | ||
if(step == null || step.ctx == null) { | ||
return; | ||
} | ||
this.resume(step.ctx); | ||
step.elapsed = step.ctx.getElapsedTime() - step.start_time; | ||
}; | ||
module.exports = new TraceContextManager(); |
{ | ||
"name": "whatap", | ||
"homepage": "http://www.whatap.io", | ||
"version": "0.1.4", | ||
"releaseDate": "20170512", | ||
"version": "0.1.5", | ||
"releaseDate": "20170518", | ||
"description": "Monitoring and Profiling Service", | ||
@@ -7,0 +7,0 @@ "main": "index.js", |
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
957349
14050