Repository navigation
Expand file tree
/
Copy pathawslambda.ts
More file actions
198 lines (175 loc) · 6.78 KB
/
Copy pathawslambda.ts
File metadata and controls
198 lines (175 loc) · 6.78 KB
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
import {
captureException,
captureMessage,
flush,
getCurrentHub,
Scope,
Severity,
startTransaction,
withScope,
} from '@sentry/node';
import * as Sentry from '@sentry/node';
import { Integration } from '@sentry/types';
// NOTE: I have no idea how to fix this right now, and don't want to waste more time, as it builds just fine — Kamil
// eslint-disable-next-line import/no-unresolved
import { Context, Handler } from 'aws-lambda';
import { hostname } from 'os';
import { performance } from 'perf_hooks';
import { types } from 'util';
import { AWSServices } from './awsservices';
import { serverlessEventProcessor } from './utils';
export * from '@sentry/node';
const { isPromise } = types;
// https://www.npmjs.com/package/aws-lambda-consumer
type SyncHandler<T extends Handler> = (
event: Parameters<T>[0],
context: Parameters<T>[1],
callback: Parameters<T>[2],
) => void;
export type AsyncHandler<T extends Handler> = (
event: Parameters<T>[0],
context: Parameters<T>[1],
) => Promise<NonNullable<Parameters<Parameters<T>[2]>[1]>>;
export interface WrapperOptions {
flushTimeout: number;
rethrowAfterCapture: boolean;
callbackWaitsForEmptyEventLoop: boolean;
captureTimeoutWarning: boolean;
timeoutWarningLimit: number;
}
export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices({ optional: true })];
/**
* @see {@link Sentry.init}
*/
export function init(options: Sentry.NodeOptions = {}): void {
if (options.defaultIntegrations === undefined) {
options.defaultIntegrations = defaultIntegrations;
}
Sentry.init(options);
Sentry.addGlobalEventProcessor(serverlessEventProcessor('AWSLambda'));
}
/**
* Adds additional information from the environment and AWS Context to the Sentry Scope.
*
* @param scope Scope that should be enhanced
* @param context AWS Lambda context that will be used to extract some part of the data
* @param startTime performance.now() when wrapHandler was invoked
*/
function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTime: number): void {
scope.setTransactionName(context.functionName);
scope.setTag('server_name', process.env._AWS_XRAY_DAEMON_ADDRESS || process.env.SENTRY_NAME || hostname());
scope.setTag('url', `awslambda:///${context.functionName}`);
scope.setContext('runtime', {
name: 'node',
version: global.process.version,
});
scope.setContext('aws.lambda', {
aws_request_id: context.awsRequestId,
function_name: context.functionName,
function_version: context.functionVersion,
invoked_function_arn: context.invokedFunctionArn,
execution_duration_in_millis: performance.now() - startTime,
remaining_time_in_millis: context.getRemainingTimeInMillis(),
'sys.argv': process.argv,
});
scope.setContext('aws.cloudwatch.logs', {
log_group: context.logGroupName,
log_stream: context.logStreamName,
url: `https://console.aws.amazon.com/cloudwatch/home?region=${
process.env.AWS_REGION
}#logsV2:log-groups/log-group/${encodeURIComponent(context.logGroupName)}/log-events/${encodeURIComponent(
context.logStreamName,
)}`,
});
}
/**
* Wraps a lambda handler adding it error capture and tracing capabilities.
*
* @param handler Handler
* @param options Options
* @returns Handler
*/
export function wrapHandler<TEvent, TResult>(
handler: Handler<TEvent, TResult>,
wrapOptions: Partial<WrapperOptions> = {},
): Handler<TEvent, TResult | undefined> {
const START_TIME = performance.now();
const options: WrapperOptions = {
flushTimeout: 2000,
rethrowAfterCapture: true,
callbackWaitsForEmptyEventLoop: false,
captureTimeoutWarning: true,
timeoutWarningLimit: 500,
...wrapOptions,
};
let timeoutWarningTimer: NodeJS.Timeout;
// AWSLambda is like Express. It makes a distinction about handlers based on it's last argument
// async (event) => async handler
// async (event, context) => async handler
// (event, context, callback) => sync handler
// Nevertheless whatever option is chosen by user, we convert it to async handler.
const asyncHandler: AsyncHandler<typeof handler> =
handler.length > 2
? (event, context) =>
new Promise((resolve, reject) => {
const rv = (handler as SyncHandler<typeof handler>)(event, context, (error, result) => {
if (error === null || error === undefined) {
resolve(result!); // eslint-disable-line @typescript-eslint/no-non-null-assertion
} else {
reject(error);
}
}) as unknown;
// This should never happen, but still can if someone writes a handler as
// `async (event, context, callback) => {}`
if (isPromise(rv)) {
(rv as Promise<NonNullable<TResult>>).then(resolve, reject);
}
})
: (handler as AsyncHandler<typeof handler>);
return async (event, context) => {
context.callbackWaitsForEmptyEventLoop = options.callbackWaitsForEmptyEventLoop;
// In seconds. You cannot go any more granular than this in AWS Lambda.
const configuredTimeout = Math.ceil(context.getRemainingTimeInMillis() / 1000);
const configuredTimeoutMinutes = Math.floor(configuredTimeout / 60);
const configuredTimeoutSeconds = configuredTimeout % 60;
const humanReadableTimeout =
configuredTimeoutMinutes > 0
? `${configuredTimeoutMinutes}m${configuredTimeoutSeconds}s`
: `${configuredTimeoutSeconds}s`;
// When `callbackWaitsForEmptyEventLoop` is set to false, which it should when using `captureTimeoutWarning`,
// we don't have a guarantee that this message will be delivered. Because of that, we don't flush it.
if (options.captureTimeoutWarning) {
const timeoutWarningDelay = context.getRemainingTimeInMillis() - options.timeoutWarningLimit;
timeoutWarningTimer = setTimeout(() => {
withScope(scope => {
scope.setTag('timeout', humanReadableTimeout);
captureMessage(`Possible function timeout: ${context.functionName}`, Severity.Warning);
});
}, timeoutWarningDelay);
}
const transaction = startTransaction({
name: context.functionName,
op: 'awslambda.handler',
});
const hub = getCurrentHub();
const scope = hub.pushScope();
let rv: TResult | undefined;
try {
enhanceScopeWithEnvironmentData(scope, context, START_TIME);
// We put the transaction on the scope so users can attach children to it
scope.setSpan(transaction);
rv = await asyncHandler(event, context);
} catch (e) {
captureException(e);
if (options.rethrowAfterCapture) {
throw e;
}
} finally {
clearTimeout(timeoutWarningTimer);
transaction.finish();
hub.popScope();
await flush(options.flushTimeout);
}
return rv;
};
}