1 |
|
2 |
|
3 |
|
4 |
|
5 | var methodCall = require('./flowpath/methodCall').MethodCall;
|
6 | var eventData = require('./flowpath/eventData').eventData;
|
7 |
|
8 |
|
9 |
|
10 | var category = require('./category');
|
11 | var path = require('path');
|
12 |
|
13 | var samples = require('./nodetime/lib/samples.js');
|
14 | var ndMethodMetaData = require('./metaData/ndMethodMetaData');
|
15 | var ndMethodMonitor = require('./method-monitor/ndMethodMonitor.js');
|
16 | var asMonitorFile = require('./autoSensor/autoSensorMonitor');
|
17 | var ndExceptionStats = require('./exception/ndExceptionStats');
|
18 | var agentSetting = require("./agent-setting");
|
19 | var btManager = require('./BT/btManager');
|
20 | var asManagerFile = require('./autoSensor/autoSensorManager');
|
21 | var asSettingObj = require('./autoSensor/autoSensorSetting');
|
22 | var localStorage = require('./utils/continuation-local-storage')
|
23 | var NDHttpReqRespCaptureSettings = require('./HttpHeader/NDHttpReqRespCaptureSettings');
|
24 | var NDHttpCaptureSettings = require('./HttpHeader/NDHttpCaptureSettings');
|
25 | var util = require("./util");
|
26 |
|
27 | var asThreashold = asSettingObj.asSampleInterval*asSettingObj.asThresholdMatchCount;
|
28 |
|
29 |
|
30 | function MyFormatter() {
|
31 |
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | MyFormatter.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 |
|
52 | MyFormatter.prototype.onEntry = function(args) {
|
53 | var context;
|
54 |
|
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;
|
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);
|
78 |
|
79 |
|
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')
|
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')
|
117 | args.methObj.seqId = context.seqId
|
118 | flowpath.eventArray.push(new eventData(methodId,context.seqId,headerId,argsName))
|
119 | |
120 |
|
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 |
|
142 | MyFormatter.prototype.onCatchClause = function(args) {
|
143 | if(agentSetting.isToInstrument)
|
144 | ndExceptionStats.dumpExceptionRecord(args);
|
145 | };
|
146 |
|
147 | function 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 |
|
157 | function 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 |
|
171 | MyFormatter.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);
|
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 |
|
242 | MyFormatter.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 |
|
253 |
|
254 |
|
255 |
|
256 | if(asSettingObj.asSampleInterval > 0 ) {
|
257 |
|
258 | if (endTime > asSettingObj.threshold) {
|
259 | var stackTrace = asManagerFile.stackTrace();
|
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 |
|
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 |
|
315 | module.exports = new MyFormatter();
|