UNPKG

14 kBJavaScriptView Raw
1/**
2 * Created by compass241 on 17-08-2015.
3 */
4 //Error.stackTraceLimit = Infinity;
5var methodCall = require('./flowpath/methodCall').MethodCall;
6var eventData = require('./flowpath/eventData').eventData;
7
8//var Formatter = require('./njstrace/formatter.js');
9//var njstrace = require('./njstrace/njsTrace');
10var category = require('./category');
11var path = require('path');
12
13var samples = require('./nodetime/lib/samples.js');
14var ndMethodMetaData = require('./metaData/ndMethodMetaData');
15var ndMethodMonitor = require('./method-monitor/ndMethodMonitor.js');
16var asMonitorFile = require('./autoSensor/autoSensorMonitor');
17var ndExceptionStats = require('./exception/ndExceptionStats');
18var agentSetting = require("./agent-setting");
19var btManager = require('./BT/btManager');
20var asManagerFile = require('./autoSensor/autoSensorManager');
21var asSettingObj = require('./autoSensor/autoSensorSetting');
22var localStorage = require('./utils/continuation-local-storage')
23var NDHttpReqRespCaptureSettings = require('./HttpHeader/NDHttpReqRespCaptureSettings');
24var NDHttpCaptureSettings = require('./HttpHeader/NDHttpCaptureSettings');
25var util = require("./util");
26
27var asThreashold = asSettingObj.asSampleInterval*asSettingObj.asThresholdMatchCount;
28
29// Create my custom Formatter class
30function MyFormatter() {// No need to call Formatter ctor here
31
32}
33
34// But must "inherit" from Formatter
35//require('util').inherits(MyFormatter, Formatter);
36
37// Implement the onEntry method
38MyFormatter.prototype.isReqToInstrument = function() {
39 try{
40 if(localStorage && localStorage.getNamespace('cavissonNamespace') && localStorage.getNamespace('cavissonNamespace').get('httpReq'))
41 return localStorage.getNamespace('cavissonNamespace').get('httpReq').cavIncludeFp;
42 else
43 return false;
44 }
45 catch(e)
46 {
47 util.logger.warn("Error in checking ,is current req to be instrument : ",e)
48 return false;
49 }
50}
51
52MyFormatter.prototype.onEntry = function(args) {
53 var context;
54 //Getting cavisson namespace that have httpReq object.
55 var flowpath=agentSetting.isToInstrument && (context= agentSetting.getContextObj()) && context.cavFlowPathId && agentSetting.flowMap[context.cavFlowPathId]
56 if(!flowpath)return args.cavIncludeFp = false;
57
58 if(!args.isFirstOrlastMethod) {
59 if (context.cavIncludeFp)
60 args.cavIncludeFp = true
61 else
62 return args.cavIncludeFp = false
63 }
64
65 ++context.seqId; //Incrementing seqID, at entry of every method, it will incremented and at end of FP it will be reinitialized
66
67 var methodId;
68
69 methodId= ndMethodMetaData.getValue(args.methodName,args);
70 if(!methodId)return;
71 var startUpTime = new Date().getTime();
72 var correlationAsyncId
73 if(global.cavisson_correlation_asyncId){
74 correlationAsyncId = typeof global.cavisson_correlation_asyncId == 'number' && global.cavisson_correlation_asyncId > -1 ? global.cavisson_correlation_asyncId : false;
75 global.cavisson_correlation_asyncId = undefined;
76 }
77 args.methObj = new methodCall (methodId,'_0_',startUpTime-context.fpTimeWithoutCavepoch,undefined,correlationAsyncId); //Creating Obj for every method entry
78
79 //Creating Obj for every method entry
80 args.flowPathObj=flowpath;
81
82 flowpath.nonServiceMethodDepth = ++flowpath.nonServiceMethodDepth;
83 if(flowpath.nonServiceMethodDepth > agentSetting.bciMaxNonServiceMethodsPerFP) {
84 if (!flowpath.isNonServiceMethodDepthExceeds){
85 flowpath.isNonServiceMethodDepthExceeds = true
86 util.logger.error(agentSetting.currentTestRun, '| Not dumping method entry record for methodId : ', methodId, 'because nonServiceMethodDepthis ',
87 flowpath.nonServiceMethodDepth, ' is greater then', agentSetting.bciMaxNonServiceMethodsPerFP, '.FPID is :', flowpath.flowPathId);
88 }
89 return;
90 }
91
92 if (agentSetting.corelateEventCallback >0 ) {
93 var eventInfo, delay,argsName,last_arg,eventName,headerId;
94 if(global.cavisson_event_callback_releation_data) {
95 last_arg = global.cavisson_event_callback_releation_data;
96 global.cavisson_event_callback_releation_data = undefined
97 }
98 eventName = args.eventName;
99 if(last_arg) {
100 eventInfo = last_arg.split(':')
101 delay = startUpTime - eventInfo[1]
102 argsName = eventInfo[0] + ' - ' + delay;
103
104 headerId = NDHttpCaptureSettings.getIDAndDumpHttpMetaRecord('asyncEvent') //to do dump 6 record
105 args.methObj.seqId = context.seqId
106 flowpath.eventArray.push(new eventData(methodId,context.seqId,headerId,argsName))
107 if (flowpath.eventArray.length >= 100) {
108 var encoded_19 = flowpath.dumpMethodArgs()
109 if(encoded_19)samples.add(encoded_19);
110 }
111 }
112 else if(eventName) {
113 argsName = 'Event- ' + eventName
114 args.parentId = context.seqId;
115
116 headerId = NDHttpCaptureSettings.getIDAndDumpHttpMetaRecord('asyncEvent') //to do dump 6 record
117 args.methObj.seqId = context.seqId
118 flowpath.eventArray.push(new eventData(methodId,context.seqId,headerId,argsName))
119 /*If 100 event areguments are dumped then we will dump 19 record , because NDC will accept
120 maximum string size of 13k bytes so we are expecting that in 100 arguments we will reach the threashold
121 */
122 if (flowpath.eventArray.length >= 100) {
123 var encoded_19 = flowpath.dumpMethodArgs()
124 if(encoded_19)samples.add(encoded_19);
125 }
126 }
127 }
128
129 flowpath.calls.push(args.methObj);
130 if (flowpath.calls.length >= agentSetting.maxCharInSeqBlob) {
131 if (!flowpath.flowpathHdrDump) {
132 var encoded2_record = flowpath.generate_2_record();
133 samples.add(encoded2_record);
134 flowpath.flowpathHdrDump = true;
135 }
136 var encoded3_record = flowpath.generate_3_record();
137 samples.add(encoded3_record);
138 flowpath.fp3RecordDump = true;
139 }
140};
141
142MyFormatter.prototype.onCatchClause = function(args) {
143 if(agentSetting.isToInstrument)
144 ndExceptionStats.dumpExceptionRecord(args);
145};
146
147function getRequestObjectFromStackMap(stackMap) {
148 var keys = Object.keys(stackMap);
149
150 for(var i = 0; i < keys.length; i++) {
151 var requestedArgument = util.checkArguments(stackMap[keys[i]].stackArgs, "IncomingMessage")
152 if(requestedArgument)
153 return requestedArgument;
154 }
155}
156
157function getResponseObject(functionArguments)
158{
159 if(functionArguments == null)
160 return null;
161 else if(functionArguments.callee.caller == null)
162 return null;
163
164 var requestedArgument = util.checkArguments(functionArguments, "ServerResponse");
165 if(requestedArgument)
166 return requestedArgument;
167 else
168 return getResponseObject(functionArguments.callee.caller.arguments);
169}
170
171MyFormatter.prototype.onCompleteFlowPath = function(req,res,context) {
172 if (!req)return;
173 var endTime = new Date().getTime();
174 if (!context) return;
175 var localFlowPathId = -1,reqSize=0,resSize=0;
176 localFlowPathId = context.cavFlowPathId;
177 var flowpath = agentSetting.flowMap[localFlowPathId];
178 if (!flowpath)
179 return;
180 context.seqId = 0;
181 var btObj = context.cavBtObj;
182 if (flowpath.errorStatusCode)
183 flowpath.statusCode = flowpath.errorStatusCode;
184 else
185 flowpath.statusCode = res.statusCode;
186 if(req["headers"])
187 reqSize = req["headers"]["content-length"];
188 if(res["_headers"])
189 resSize = res["_headers"]["content-length"];
190
191 context['cavFlowPathId'] = null;
192 if (agentSetting.isToInstrument && agentSetting.dataConnHandler) {
193 try {
194 process.nextTick(function () {
195 try {
196 var respTime = (endTime - agentSetting.cavEpochDiffInMills) - context.cavTimeInMillis;
197 var bt_Data = btManager.getBTData(btObj.btId);
198 flowpath.respTime = respTime;
199 flowpath.category = category.getCategory(flowpath.statusCode, respTime, btObj.threshold, btObj.threshold.dynamicSlowThresold, btObj.threshold.dynamicVSlowThresold);// this function responsible for calculate slow and vslow flowpaths on basis of threshold values
200 btManager.createAndUpdateBTRecord(btObj.btId, btObj.btName, respTime, flowpath.category, flowpath.statusCode,btObj.threshold.dynamicSlowThresold, btObj.threshold.dynamicVSlowThresold, reqSize,resSize);
201
202 if (context.cavIncludeFp || (!(context.cavIncludeFp) && flowpath.category > 10) || flowpath.dumpForcefullL1FP) {
203 NDHttpCaptureSettings.dumpHttpReqResHeader(req, res, flowpath)
204 var encoded_19 = flowpath.dumpMethodArgs()
205 if(encoded_19)samples.add(encoded_19);
206 if (flowpath.calls.length) {
207 if (!flowpath.flowpathHdrDump) {
208 var encoded2_record = flowpath.generate_2_record();
209 samples.add(encoded2_record);
210 }
211 agentSetting.checkforCorrelationIdInReqRes(res, flowpath);
212 var encoded4_record = flowpath.generate_4_record();
213 samples.add(encoded4_record);
214 }
215 else {
216 if (flowpath.flowpathHdrDump) {
217 agentSetting.checkforCorrelationIdInReqRes(res, flowpath);
218 var encoded4_record = flowpath.generate_4_record();
219 samples.add(encoded4_record);
220 }
221 }
222 }
223 else if (!(context.cavIncludeFp) && flowpath.fp3RecordDump && flowpath.category == 10) {
224 flowpath.statusCode = -99;
225 agentSetting.checkforCorrelationIdInReqRes(res, flowpath);
226 var encoded4_record = flowpath.generate_4_record();
227 samples.add(encoded4_record);
228 }
229 delete agentSetting.flowMap[localFlowPathId];
230 }
231 catch (err) {
232 util.logger.warn(err);
233 }
234 });
235 } catch (err) {
236 util.logger.warn(err);
237 }
238 }
239}
240
241// Implement the onExit method
242MyFormatter.prototype.onExit = function(args) {
243 var flowPathObj = args.flowPathObj;
244 if(!agentSetting.isToInstrument || !flowPathObj) return;
245 if(!args.methObj) return;
246
247 var endTime = ((new Date().getTime() - flowPathObj.fpTimeWithoutCavepoch) - args.methObj.startUpTime );
248 var exitMethodObj = new methodCall(args.methObj.methodId,'_1_',endTime);
249 var threadID = flowPathObj.threadID;
250
251 /*
252 * Dumping AS data
253 * Checking method duration(endTime) with AS Threshold Value
254 * Args:stackTrace,endTime(duration),fpId,methodId,threadId,currentTime relative to cavEpochDiff.
255 */
256 if(asSettingObj.asSampleInterval > 0 ) {
257
258 if (endTime > asSettingObj.threshold) {
259 var stackTrace = asManagerFile.stackTrace(); //Getting Stack Trace for particular method.
260 process.nextTick(function () {
261 try {
262 if(asSettingObj.ASTraceLevel > 0)
263 util.logger.info(agentSetting.currentTestRun,' | Handling hotspot for methodId :',exitMethodObj.methodId)
264 asManagerFile.handledHotspotData(stackTrace, endTime, (+args.methObj.startUpTime + +flowPathObj.timeInMillis) ,
265 flowPathObj.cavCurrReqFPID,
266 exitMethodObj.methodId, threadID, (new Date().getTime() - agentSetting.cavEpochDiffInMills));
267 }
268 catch (err) {
269 util.logger.warn("Getting Error in AS :- " + err);
270 }
271 });
272 }
273
274 }
275 if (!args.isFirstOrlastMethod && !args.methObj.isDumped && Number(endTime) < agentSetting.excludeMethodOnRespTime) {
276 args.methObj.exclude = true;
277 //
278 --flowPathObj.nonServiceMethodDepth;
279 return;
280 }
281 if(flowPathObj.nonServiceMethodDepth > agentSetting.bciMaxNonServiceMethodsPerFP) {
282 if (!flowPathObj.isNonServiceMethodDepthExceeds) {
283 flowPathObj.isNonServiceMethodDepthExceeds = true;
284 util.logger.error(agentSetting.currentTestRun, '| Not dumping method exit record for methodId : ', exitMethodObj.methodId, 'because nonServiceMethodDepthis ',
285 flowPathObj.nonServiceMethodDepth, ' is greater then', agentSetting.bciMaxNonServiceMethodsPerFP, '.FPID is :', flowPathObj.flowPathId);
286 }
287 flowPathObj.nonServiceMethodDepth = --flowPathObj.nonServiceMethodDepth;
288 return;
289 }
290 flowPathObj.calls.push(exitMethodObj);
291
292 //var mthName = ndMethodMetaData.getMethodMonitorName(exitMethodObj.methodId);
293 var aliasName = ndMethodMonitor.isMethodInCurrentMonitoringList(args.dumpedMethodName);
294 if (aliasName) {
295 process.nextTick(function () {
296 try {
297 ndMethodMonitor.updateMMCounters(args.dumpedMethodName, exitMethodObj.methodId, endTime, aliasName)
298 } catch (err) {
299 util.logger.warn(err)
300 }
301 });
302 }
303 if (flowPathObj.calls.length >= agentSetting.maxCharInSeqBlob) {
304 if (!flowPathObj.flowpathHdrDump) {
305 var encoded2_record = flowPathObj.generate_2_record();
306 samples.add(encoded2_record);
307 flowPathObj.flowpathHdrDump = true;
308 }
309 var encoded3_record = flowPathObj.generate_3_record();
310 samples.add(encoded3_record);
311 flowPathObj.fp3RecordDump = true;
312 }
313};
314
315module.exports = new MyFormatter();