This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ThundraWrapper.ts
509 lines (449 loc) · 17.9 KB
/
ThundraWrapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
*
* Wraps the lambda handler function.
*
* Implemented in Hook & Plugin structure. Runs plugins' related functions by executing hooks.
*
* Wraps the original callback and context.
*
* invoke function calls the original lambda handler with original event, wrapped context and wrapped callback.
*
* Wrapped context methods (done, succeed, fail) and callback call report function.
*
* report function uses the Reporter instance to make a single request to send reports if async monitoring is
* not enabled (environment variable thundra_lambda_report_cloudwatch_enable is not set). After reporting it calls
* original callback/succeed/done/fail.
*
*/
import Reporter from './Reporter';
import TimeoutError from './plugins/error/TimeoutError';
import HttpError from './plugins/error/HttpError';
import ThundraConfig from './plugins/config/ThundraConfig';
import PluginContext from './plugins/PluginContext';
import ThundraLogger from './ThundraLogger';
import InvocationSupport from './plugins/support/InvocationSupport';
import {
BROKER_WS_HTTP_ERR_CODE_TO_MSG,
BROKER_WS_HTTP_ERROR_PATTERN,
BROKER_WS_PROTOCOL,
BROKER_WSS_PROTOCOL,
DEBUG_BRIDGE_FILE_NAME,
} from './Constants';
import Utils from './plugins/utils/Utils';
import {readFileSync} from 'fs';
import ConfigProvider from './config/ConfigProvider';
import ConfigNames from './config/ConfigNames';
const path = require('path');
class ThundraWrapper {
private originalThis: any;
private originalEvent: any;
private originalContext: any;
private originalCallback: any;
private originalFunction: any;
private config: ThundraConfig;
private plugins: any;
private pluginContext: PluginContext;
private reported: boolean;
private reporter: Reporter;
private wrappedContext: any;
private timeout: NodeJS.Timer;
private resolve: any;
private reject: any;
private inspector: any;
private fork: any;
private debuggerPort: number;
private debuggerMaxWaitTime: number;
private monitoringDisabled: boolean;
private brokerHost: string;
private sessionName: string;
private brokerProtocol: string;
private authToken: string;
private sessionTimeout: number;
private brokerPort: number;
private debuggerProxy: any;
private debuggerLogsEnabled: boolean;
constructor(self: any, event: any, context: any, callback: any,
originalFunction: any, plugins: any, pluginContext: PluginContext, monitoringDisabled: boolean) {
this.originalThis = self;
this.originalEvent = event;
this.originalContext = context;
this.originalCallback = callback;
this.originalFunction = originalFunction;
this.config = pluginContext.config ? pluginContext.config : new ThundraConfig({});
this.plugins = plugins;
this.pluginContext = pluginContext;
this.pluginContext.maxMemory = parseInt(context.memoryLimitInMB, 10);
this.reported = false;
this.reporter = new Reporter(pluginContext.config);
this.monitoringDisabled = monitoringDisabled;
this.wrappedContext = {
...context,
done: (error: any, result: any) => {
this.report(error, result, () => {
this.originalContext.done(error, result);
});
},
succeed: (result: any) => {
this.report(null, result, () => {
this.originalContext.succeed(result);
});
},
fail: (error: any) => {
this.report(error, null, () => {
this.originalContext.fail(error);
});
},
};
const me = this;
this.wrappedContext = Object.assign({
set callbackWaitsForEmptyEventLoop(value) {
me.originalContext.callbackWaitsForEmptyEventLoop = value;
},
get callbackWaitsForEmptyEventLoop() {
return me.originalContext.callbackWaitsForEmptyEventLoop;
},
}, this.wrappedContext);
this.timeout = this.setupTimeoutHandler(this);
InvocationSupport.setFunctionName(this.originalContext.functionName);
if (this.shouldInitDebugger()) {
this.initDebugger();
}
}
wrappedCallback = (error: any, result: any) => {
this.report(error, result, () => {
this.invokeCallback(error, result);
});
}
shouldInitDebugger(): boolean {
const authToken = ConfigProvider.get<string>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_AUTH_TOKEN);
const debuggerEnable = ConfigProvider.get<boolean>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_ENABLE, null);
if (debuggerEnable != null) {
return debuggerEnable && authToken !== undefined;
} else {
return authToken !== undefined;
}
}
invokeCallback(error: any, result: any): void {
if (typeof this.originalCallback === 'function') {
this.originalCallback(error, result);
}
}
onFinish(error: any, result: any): void {
this.finishDebuggerProxyIfAvailable();
if (error && this.reject) {
this.reject(error);
} else if (this.resolve) {
this.resolve(result);
}
}
initDebugger(): void {
try {
this.inspector = require('inspector');
this.fork = require('child_process').fork;
const debuggerPort =
ConfigProvider.get<number>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_PORT);
const brokerHost =
ConfigProvider.get<string>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_BROKER_HOST);
const brokerPort =
ConfigProvider.get<number>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_BROKER_PORT);
const authToken =
ConfigProvider.get<string>(
ConfigNames.THUNDRA_LAMBDA_DEBUGGER_AUTH_TOKEN,
'');
const sessionName =
ConfigProvider.get<string>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_SESSION_NAME);
const debuggerMaxWaitTime =
ConfigProvider.get<number>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_WAIT_MAX);
const debuggerLogsEnabled =
ConfigProvider.get<boolean>(ConfigNames.THUNDRA_LAMBDA_DEBUGGER_LOGS_ENABLE);
let brokerProtocol = BROKER_WSS_PROTOCOL;
if (brokerHost.startsWith(BROKER_WS_PROTOCOL) || brokerHost.startsWith(BROKER_WSS_PROTOCOL)) {
// If WebSocket protocol is already included in the broker address, do not add protocol string
brokerProtocol = '';
}
if (brokerPort === -1) {
throw new Error(
'For debugging, you must set debug broker port through \
\'thundra_agent_lambda_debug_broker_port\' environment variable');
}
this.debuggerPort = debuggerPort;
this.debuggerMaxWaitTime = debuggerMaxWaitTime;
this.brokerProtocol = brokerProtocol;
this.brokerPort = brokerPort;
this.brokerHost = brokerHost;
this.sessionName = sessionName;
this.sessionTimeout = Date.now() + this.originalContext.getRemainingTimeInMillis();
this.authToken = authToken;
this.debuggerLogsEnabled = debuggerLogsEnabled;
} catch (e) {
this.fork = null;
this.inspector = null;
}
}
getDebuggerProxyIOMetrics(): any {
try {
const ioContent = readFileSync('/proc/' + this.debuggerProxy.pid + '/io', 'utf8');
const ioMetrics = ioContent.split('\n');
return {
rchar: ioMetrics[0],
wchar: ioMetrics[1],
};
} catch (e) {
return null;
}
}
async waitForDebugger() {
let prevRchar = 0;
let prevWchar = 0;
let initCompleted = false;
const logger: ThundraLogger = ThundraLogger.getInstance();
logger.info('Waiting for debugger to handshake ...');
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
const startTime = Date.now();
while ((Date.now() - startTime) < this.debuggerMaxWaitTime) {
try {
const debuggerIoMetrics = this.getDebuggerProxyIOMetrics();
if (!debuggerIoMetrics) {
await sleep(1000);
break;
}
if (prevRchar !== 0 && prevWchar !== 0 &&
debuggerIoMetrics.rchar === prevRchar && debuggerIoMetrics.wchar === prevWchar) {
initCompleted = true;
break;
}
prevRchar = debuggerIoMetrics.rchar;
prevWchar = debuggerIoMetrics.wchar;
} catch (e) {
logger.error(e);
break;
}
await sleep(1000);
}
if (initCompleted) {
logger.info('Completed debugger handshake');
} else {
logger.error('Couldn\'t complete debugger handshake in ' + this.debuggerMaxWaitTime + ' milliseconds.');
}
}
async startDebuggerProxyIfAvailable() {
if (this.debuggerProxy) {
this.finishDebuggerProxyIfAvailable();
}
if (this.fork && this.inspector) {
try {
this.debuggerProxy = this.fork(
path.join(__dirname, DEBUG_BRIDGE_FILE_NAME),
[],
{
detached: true,
env: {
BROKER_HOST: this.brokerHost,
BROKER_PORT: this.brokerPort,
SESSION_NAME: this.sessionName,
SESSION_TIMEOUT: this.sessionTimeout,
AUTH_TOKEN: this.authToken,
DEBUGGER_PORT: this.debuggerPort,
LOGS_ENABLED: this.debuggerLogsEnabled,
BROKER_PROTOCOL: this.brokerProtocol,
},
},
);
this.inspector.open(this.debuggerPort, 'localhost', false);
const waitForBrokerConnection = () => new Promise((resolve) => {
this.debuggerProxy.once('message', (mes: any) => {
if (mes === 'brokerConnect') {
return resolve(false);
}
let errMessage: string;
if (typeof mes === 'string') {
const match = mes.match(BROKER_WS_HTTP_ERROR_PATTERN);
if (match) {
const errCode = Number(match[1]);
errMessage = BROKER_WS_HTTP_ERR_CODE_TO_MSG[errCode];
}
}
// If errMessage is undefined replace it with the raw incoming message
errMessage = errMessage || mes;
ThundraLogger.getInstance().error('Thundra Debugger: ' + errMessage);
return resolve(true);
});
});
const brokerHasErr = await waitForBrokerConnection();
if (brokerHasErr) {
this.finishDebuggerProxyIfAvailable();
return;
}
await this.waitForDebugger();
} catch (e) {
this.debuggerProxy = null;
ThundraLogger.getInstance().error(e);
}
}
}
finishDebuggerProxyIfAvailable(): void {
try {
if (this.inspector) {
this.inspector.close();
this.inspector = null;
}
} catch (e) {
ThundraLogger.getInstance().error(e);
}
if (this.debuggerProxy) {
try {
if (!this.debuggerProxy.killed) {
this.debuggerProxy.kill();
}
} catch (e) {
ThundraLogger.getInstance().error(e);
} finally {
this.debuggerProxy = null;
}
}
}
async invoke() {
this.config.refreshConfig();
await this.startDebuggerProxyIfAvailable();
const beforeInvocationData = {
originalContext: this.originalContext,
originalEvent: this.originalEvent,
reporter: this.reporter,
};
this.resetTime();
InvocationSupport.setErrorenous(false);
this.resolve = undefined;
this.reject = undefined;
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.executeHook('before-invocation', beforeInvocationData, false)
.then(() => {
this.pluginContext.requestCount += 1;
try {
const result = this.originalFunction.call(
this.originalThis,
this.originalEvent,
this.wrappedContext,
this.wrappedCallback,
);
if (result && result.then !== undefined && typeof result.then === 'function') {
result.then(this.wrappedContext.succeed, this.wrappedContext.fail);
}
} catch (error) {
this.report(error, null, null);
}
})
.catch((error) => {
ThundraLogger.getInstance().debug(error);
// There is an error on "before-invocation" phase
// So skip Thundra wrapping and call original function directly
const result = this.originalFunction.call(
this.originalThis,
this.originalEvent,
this.originalContext,
this.originalCallback,
);
resolve(result);
});
});
}
async executeHook(hook: any, data: any, reverse: boolean) {
this.plugins.sort((p1: any, p2: any) => p1.pluginOrder > p2.pluginOrder ? 1 : -1);
if (reverse) {
this.plugins.reverse();
}
await Promise.all(
this.plugins.map((plugin: any) => {
if (plugin.hooks && plugin.hooks[hook]) {
return plugin.hooks[hook](data);
}
}),
);
}
async executeAfterInvocationAndReport(afterInvocationData: any) {
if (this.monitoringDisabled) {
return;
}
afterInvocationData.error ? InvocationSupport.setErrorenous(true) : InvocationSupport.setErrorenous(false);
await this.executeHook('after-invocation', afterInvocationData, true);
this.resetTime();
await this.reporter.sendReports();
InvocationSupport.setErrorenous(false);
}
resetTime() {
this.pluginContext.invocationStartTimestamp = undefined;
this.pluginContext.invocationFinishTimestamp = undefined;
}
async report(error: any, result: any, callback: any) {
if (!this.reported) {
try {
this.reported = true;
let afterInvocationData = {
error,
originalEvent: this.originalEvent,
response: result,
};
if (this.isErrorResponse(result)) {
afterInvocationData = {
error: new HttpError('Lambda returned with error response.'),
originalEvent: this.originalEvent,
response: result,
};
}
await this.executeAfterInvocationAndReport(afterInvocationData);
if (this.timeout) {
clearTimeout(this.timeout);
}
if (typeof callback === 'function') {
callback();
}
} finally {
this.onFinish(error, result);
}
}
}
isErrorResponse(result: any) {
let isError = false;
if (Utils.isValidResponse(result) && result.body) {
if (typeof result.body === 'string') {
if (result.statusCode >= 400 && result.statusCode <= 599) {
isError = true;
}
} else {
isError = true;
}
}
return isError;
}
setupTimeoutHandler(wrapperInstance: any): NodeJS.Timer | undefined {
const { originalContext, pluginContext } = wrapperInstance;
const { getRemainingTimeInMillis = () => 0 } = originalContext;
if (pluginContext.timeoutMargin < 1 || getRemainingTimeInMillis() < 10) {
return undefined;
}
const maxEndTime = 899900;
const configEndTime = Math.max(
0,
getRemainingTimeInMillis() - pluginContext.timeoutMargin,
);
const endTime = Math.min(configEndTime, maxEndTime);
return setTimeout(() => {
if (this.debuggerProxy) {
// Debugger proxy exists, let it know about the timeout
try {
if (!this.debuggerProxy.killed) {
this.debuggerProxy.kill('SIGHUP');
}
} catch (e) {
ThundraLogger.getInstance().error(e);
} finally {
this.debuggerProxy = null;
}
}
wrapperInstance.report(new TimeoutError('Lambda is timed out.'), null, null);
wrapperInstance.reported = false;
}, endTime);
}
}
export default ThundraWrapper;