/
index.ts
563 lines (500 loc) · 19.7 KB
/
index.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import * as path from "path";
import { PromisePool } from "@supercharge/promise-pool";
import * as AdmZip from "adm-zip";
import * as SemVer from "semver";
import Serverless from "serverless";
import Plugin from "serverless/classes/Plugin";
import Service from "serverless/classes/Service";
import Aws from "serverless/plugins/aws/provider/awsProvider";
import * as request from "superagent";
import { v4 as uuid } from "uuid";
import GitRev from "./git-rev";
export type SentryRelease = {
version: string | boolean;
refs?:
| {
repository: string;
commit: string;
previousCommit?: string;
}[]
| false;
};
export type SentryOptions = {
dsn?: string;
environment?: string;
authToken?: string;
organization?: string;
project?: string;
release?: SentryRelease | string | boolean;
/** Specifies whether this SDK should activate and send events to Sentry (defaults to `true`) */
enabled?: boolean;
/** Don't report errors from local environments (defaults to `true`) */
filterLocal?: boolean;
/** Enable source maps (defaults to `false`) */
sourceMaps?:
| boolean
| {
/** Filepath prefix for sourcemaps uploaded to Sentry */
urlPrefix: string;
};
/** Automatically create breadcrumbs (see Sentry SDK docs, default to `true`) */
autoBreadcrumbs?: boolean;
/** Capture Lambda errors (defaults to `true`) */
captureErrors?: boolean;
/** Capture unhandled Promise rejections (defaults to `true`) */
captureUnhandledRejections?: boolean;
/** Capture uncaught exceptions (defaults to `true`) */
captureUncaughtException?: boolean;
/** Monitor memory usage (defaults to `true`) */
captureMemoryWarnings?: boolean;
/** Monitor execution timeouts (defaults to `true`) */
captureTimeoutWarnings?: boolean;
};
/** Short form for encoding URI components */
const _e = encodeURIComponent;
/** Helper type for Serverless functions with an optional `sentry` configuration setting */
type FunctionDefinitionWithSentry = Serverless.FunctionDefinition & {
sentry?: boolean | SentryOptions;
};
/** Required parameters for creating/updating releases in the API */
type ApiParameters = {
authToken: string;
organization: string;
project?: string;
refs?: SentryRelease["refs"];
version: string;
};
/**
* Serverless Plugin forward Lambda exceptions to Sentry (https://sentry.io)
*/
export class SentryPlugin implements Plugin {
sentry: Partial<SentryOptions>;
serverless: Serverless;
options: Serverless.Options;
custom: Service.Custom;
hooks: { [event: string]: (...rest: any[]) => any };
provider: Aws;
validated: boolean;
isInstrumented: boolean;
constructor(serverless: Serverless, options: Serverless.Options) {
this.serverless = serverless;
this.options = options;
this.custom = this.serverless.service.custom;
this.provider = this.serverless.getProvider("aws");
// Create schema for our properties. For reference use https://github.com/ajv-validator/ajv
serverless.configSchemaHandler.defineCustomProperties({
type: "object",
properties: {
sentry: {
type: "object",
properties: {
dsn: { type: "string" },
environment: { type: "string" },
authToken: { type: "string" },
organization: { type: "string" },
project: { type: "string" },
release: {
anyOf: [{ type: "object" }, { type: "string" }, { type: "boolean" }],
},
enabled: { type: "boolean" },
filterLocal: { type: "boolean" },
sourceMaps: {
oneOf: [
{ type: "boolean" },
{
type: "object",
properties: {
urlPrefix: { type: "string" },
},
},
],
},
autoBreadcrumbs: { type: "boolean" },
captureErrors: { type: "boolean" },
captureUnhandledRejections: { type: "boolean" },
captureUncaughtException: { type: "boolean" },
captureMemoryWarnings: { type: "boolean" },
captureTimeoutWarnings: { type: "boolean" },
},
required: ["dsn"],
additionalProperties: false,
},
},
required: ["sentry"],
});
this.hooks = {
"before:package:initialize": async () => this.validate(),
"after:package:initialize": async () => {
await this.setRelease();
await this.instrumentFunctions();
},
"before:deploy:deploy": async () => {
await this.validate();
await this.setRelease();
await this.instrumentFunctions();
},
"after:deploy:deploy": async () => {
await this.createSentryRelease();
await this.uploadSentrySourcemaps();
await this.deploySentryRelease();
},
"before:deploy:function:deploy": async () => {
await this.validate();
await this.setRelease();
await this.instrumentFunctions();
},
"after:deploy:function:deploy": async () => {
await this.createSentryRelease();
await this.uploadSentrySourcemaps();
await this.deploySentryRelease();
},
"before:invoke:local:invoke": async () => {
await this.validate();
await this.setRelease();
await this.instrumentFunctions(true);
},
"before:offline:start": async () => {
await this.validate();
await this.setRelease();
await this.instrumentFunctions();
},
"before:offline:start:init": async () => {
await this.validate();
await this.setRelease();
await this.instrumentFunctions();
},
};
this.configPlugin();
}
configPlugin(): void {
this.sentry = {};
if (typeof this.custom.sentry === "object") {
Object.assign(this.sentry, this.custom.sentry);
}
}
async validate(): Promise<void> {
if (this.validated) {
// Already ran
return;
}
// Check required serverless version
if (SemVer.gt("1.12.0", this.serverless.getVersion())) {
throw new Error("Serverless verion must be >= 1.12.0");
}
// Set configuration
this.validated = true;
this.sentry = { ...(this.serverless.service.custom.sentry || {}) } as Partial<SentryOptions>;
// Validate Sentry options
if (!this.sentry.dsn) {
this.serverless.cli.log("DSN not set. Serverless Sentry plugin is disabled.", "sentry");
}
if (this.sentry.enabled === false) {
this.serverless.cli.log("Serverless Sentry is disabled from provided options.", "sentry");
}
// Set default option values
if (!this.sentry.environment) {
this.sentry.environment = this.options.stage ?? undefined;
}
if (this.sentry.authToken && (!this.sentry.organization || !this.sentry.project)) {
this.serverless.cli.log(
"Sentry: In order to use the Sentry API " +
"make sure to set `organization` and `project` in your `serverless.yml`.",
"sentry",
);
this.sentry.authToken = undefined;
}
}
instrumentFunction(originalDefinition: Serverless.FunctionDefinition, setEnv: boolean): FunctionDefinitionWithSentry {
const newDefinition: FunctionDefinitionWithSentry = { ...originalDefinition };
const sentryConfig = { ...this.sentry };
const localConfig = newDefinition.sentry;
if (typeof localConfig === "object") {
Object.assign(sentryConfig, localConfig);
}
// Environment variables have to be a string in order to be processed properly
newDefinition.environment = newDefinition.environment ?? {};
if (typeof sentryConfig.dsn !== "undefined") {
newDefinition.environment.SENTRY_DSN = String(sentryConfig.dsn);
setEnv && (process.env.SENTRY_DSN = newDefinition.environment.SENTRY_DSN);
}
if (typeof sentryConfig.release === "object" && sentryConfig.release.version) {
newDefinition.environment.SENTRY_RELEASE = String(sentryConfig.release.version);
setEnv && (process.env.SENTRY_RELEASE = newDefinition.environment.SENTRY_RELEASE);
}
if (typeof sentryConfig.environment !== "undefined") {
newDefinition.environment.SENTRY_ENVIRONMENT = String(sentryConfig.environment);
setEnv && (process.env.SENTRY_ENVIRONMENT = newDefinition.environment.SENTRY_ENVIRONMENT);
}
if (typeof sentryConfig.autoBreadcrumbs !== "undefined") {
newDefinition.environment.SENTRY_AUTO_BREADCRUMBS = String(sentryConfig.autoBreadcrumbs);
setEnv && (process.env.SENTRY_AUTO_BREADCRUMBS = newDefinition.environment.SENTRY_AUTO_BREADCRUMBS);
}
if (typeof sentryConfig.sourceMaps !== "undefined") {
newDefinition.environment.SENTRY_SOURCEMAPS = String(sentryConfig.sourceMaps);
setEnv && (process.env.SENTRY_SOURCEMAPS = newDefinition.environment.SENTRY_SOURCEMAPS);
}
if (typeof sentryConfig.filterLocal !== "undefined") {
newDefinition.environment.SENTRY_FILTER_LOCAL = String(sentryConfig.filterLocal);
setEnv && (process.env.SENTRY_FILTER_LOCAL = newDefinition.environment.SENTRY_FILTER_LOCAL);
}
if (typeof sentryConfig.captureErrors !== "undefined") {
newDefinition.environment.SENTRY_CAPTURE_ERRORS = String(sentryConfig.captureErrors);
setEnv && (process.env.SENTRY_CAPTURE_ERRORS = newDefinition.environment.SENTRY_CAPTURE_ERRORS);
}
if (typeof sentryConfig.captureUnhandledRejections !== "undefined") {
newDefinition.environment.SENTRY_CAPTURE_UNHANDLED = String(sentryConfig.captureUnhandledRejections);
setEnv && (process.env.SENTRY_CAPTURE_UNHANDLED = newDefinition.environment.SENTRY_CAPTURE_UNHANDLED);
}
if (typeof sentryConfig.captureUncaughtException !== "undefined") {
newDefinition.environment.SENTRY_CAPTURE_UNCAUGHT = String(sentryConfig.captureUncaughtException);
setEnv && (process.env.SENTRY_CAPTURE_UNCAUGHT = newDefinition.environment.SENTRY_CAPTURE_UNCAUGHT);
}
if (typeof sentryConfig.captureMemoryWarnings !== "undefined") {
newDefinition.environment.SENTRY_CAPTURE_MEMORY = String(sentryConfig.captureMemoryWarnings);
setEnv && (process.env.SENTRY_CAPTURE_MEMORY = newDefinition.environment.SENTRY_CAPTURE_MEMORY);
}
if (typeof sentryConfig.captureTimeoutWarnings !== "undefined") {
newDefinition.environment.SENTRY_CAPTURE_TIMEOUTS = String(sentryConfig.captureTimeoutWarnings);
setEnv && (process.env.SENTRY_CAPTURE_TIMEOUTS = newDefinition.environment.SENTRY_CAPTURE_TIMEOUTS);
}
return newDefinition;
}
/**
*
* @param setEnv set to `true` to set `process.env`. Useful when invoking the Lambda locally
*/
async instrumentFunctions(setEnv: boolean = false): Promise<void> {
if (!this.sentry.dsn || this.sentry.enabled === false) {
return; // Sentry not enabled
}
if (this.isInstrumented && !setEnv) {
return; // already instrumented in a previous step; no need to run again
}
const functionNames = this.serverless.service.getAllFunctions();
const functions = functionNames.reduce((functions, functionName) => {
const functionObject: FunctionDefinitionWithSentry = this.serverless.service.getFunction(functionName);
if ((functionObject.sentry ?? true) !== false) {
process.env.SLS_DEBUG && this.serverless.cli.log(`Instrumenting ${String(functionObject.name)}`, "sentry");
functions[functionName] = this.instrumentFunction(functionObject, setEnv);
} else {
process.env.SLS_DEBUG && this.serverless.cli.log(`Skipping ${String(functionObject.name)}`, "sentry");
}
return functions;
}, {} as { [key: string]: FunctionDefinitionWithSentry });
this.serverless.service.update({ functions });
this.isInstrumented = true;
}
async _resolveGitRefs(gitRev: GitRev, release: SentryRelease): Promise<SentryRelease> {
const origin = await gitRev.origin();
const commit = await gitRev.long();
let repository = /[:/]([^/]+\/[^/]+?)(?:\.git)?$/i.exec(origin)?.[1];
if (repository && origin.includes("gitlab")) {
// GitLab uses spaces around the slashes in the repository name
repository = repository.replace(/\//g, " / ");
}
if (Array.isArray(release.refs)) {
const refs = release.refs;
refs.forEach((ref) => {
if (ref && ref.repository === "git") {
ref.repository = repository ?? "";
}
if (ref && ref.commit === "git") {
ref.commit = commit;
}
if (ref && ref.previousCommit === "git") {
delete ref.previousCommit; // not available via git
}
});
return { ...release, refs };
} else {
return { ...release, refs: undefined };
}
}
async setRelease(): Promise<void> {
let release: SentryRelease | undefined;
if (this.sentry.release && typeof this.sentry.release === "string") {
// Expand to the long form
release = {
version: this.sentry.release,
};
} else {
release = this.sentry.release as SentryRelease;
}
const version = release?.version;
if (typeof version === "undefined" || String(version) === "false") {
// nothing to do
this.sentry.release = undefined;
return;
}
if (version === true || version === "true" || version === "git") {
try {
const gitRev = new GitRev({ cwd: this.serverless.config.servicePath });
const version = (await gitRev.exactTag()) ?? (await gitRev.short());
release.version = version;
if (!release.refs) {
// By default use git to resolve repository and commit hash
release.refs = [
{
repository: "git",
commit: "git",
},
];
}
release = await this._resolveGitRefs(gitRev, release);
} catch (err) {
// No git available.
if (version === "git") {
// Error out
throw new Error(`Sentry: No Git available - ${(err as Error).toString()}`);
}
// Fall back to use a random number instead.
process.env.SLS_DEBUG &&
this.serverless.cli.log("No Git available. Creating a random release version...", "sentry");
release.version = this.getRandomVersion();
release.refs = undefined;
}
} else if (version === "random") {
process.env.SLS_DEBUG && this.serverless.cli.log("Creating a random release version...", "sentry");
release.version = this.getRandomVersion();
} else {
const str = String(version).trim();
process.env.SLS_DEBUG && this.serverless.cli.log(`Setting release version to "${str}"...`, "sentry");
release.version = str;
}
this.sentry.release = release;
}
async createSentryRelease(): Promise<void> {
const apiParameters = this._apiParameters();
if (!apiParameters) {
// Nothing to do
return;
}
const { authToken, project, refs, version, organization } = apiParameters;
const payload = {
projects: [project],
refs,
version,
};
this.serverless.cli.log(`Creating new release "${version}"...: ${JSON.stringify(payload)}`, "sentry");
try {
await request
.post(`https://sentry.io/api/0/organizations/${_e(organization)}/releases/`)
.set("Authorization", `Bearer ${authToken}`)
.send(payload);
} catch (err) {
if ((err as request.ResponseError)?.response?.text) {
this.serverless.cli.log(
`Received error response from Sentry:\n${String((err as request.ResponseError)?.response?.text)}`,
"sentry",
);
}
throw new Error(`Sentry: Error uploading release - ${(err as Error).toString()}`);
}
}
async uploadSentrySourcemaps(): Promise<void> {
const apiParameters = this._apiParameters();
if (!apiParameters || !this.sentry.sourceMaps) {
// Nothing to do
return;
}
this.serverless.cli.log("Uploading source maps to Sentry", "sentry");
const artifacts = new Set(
this.serverless.service
.getAllFunctions()
.map((name) => this.serverless.service.getFunction(name).package?.artifact)
.filter((artifact): artifact is string => typeof artifact === "string"),
);
const results: (() => Promise<void>)[] = [];
artifacts.forEach((artifact) => {
const zip = new AdmZip(artifact);
zip.getEntries().forEach((entry) => {
if ((!entry.isDirectory && entry.name.endsWith(".js")) || entry.name.endsWith(".js.map")) {
results.push(() => this._uploadSourceMap(entry, apiParameters));
}
});
});
// Upload artifacts in parallel. 50 parallel web sockets is the default of the AWS JavaScript SDK.
// see https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-configuring-maxsockets.html
await PromisePool.withConcurrency(50)
.for(results)
.process(async (nextArtifact) => await nextArtifact());
}
async _uploadSourceMap(entry: AdmZip.IZipEntry, params: ApiParameters): Promise<void> {
const prefix = typeof this.sentry.sourceMaps === "object" && this.sentry.sourceMaps.urlPrefix;
const filePath = prefix ? path.join(prefix, entry.entryName) : entry.entryName;
const data = entry.getData();
try {
await request
.post(`https://sentry.io/api/0/organizations/${_e(params.organization)}/releases/${_e(params.version)}/files/`)
.set("Authorization", `Bearer ${params.authToken}`)
.field("name", filePath)
.attach("file", data, { filename: entry.name });
} catch (err) {
const responseError = err as request.ResponseError;
if (responseError?.response?.status === 409) {
process.env.SLS_DEBUG && this.serverless.cli.log(`Skipping already uploaded file: ${entry.name}`, "sentry");
return;
} else if (responseError?.response?.text) {
this.serverless.cli.log(
`Received error response from Sentry:\n${String((err as request.ResponseError)?.response?.text)}`,
"sentry",
);
}
throw new Error(`Sentry: Error uploading sourcemap file - ${(err as Error).toString()}`);
}
}
async deploySentryRelease(): Promise<void> {
const apiParameters = this._apiParameters();
if (!apiParameters || !this.sentry.sourceMaps) {
// Nothing to do
return;
}
this.serverless.cli.log(`Deploying release "${String(apiParameters.version)}"...`, "sentry");
try {
await request
.post(
`https://sentry.io/api/0/organizations/${_e(apiParameters.organization)}/releases/${_e(
apiParameters.version,
)}/deploys/`,
)
.set("Authorization", `Bearer ${apiParameters.authToken}`)
.send({
environment: this.sentry.environment,
name: `Deployed ${this.serverless.service.getServiceName()}`,
});
} catch (err) {
if ((err as request.ResponseError)?.response?.text) {
this.serverless.cli.log(
`Received error response from Sentry:\n${String((err as request.ResponseError)?.response?.text)}`,
"sentry",
);
}
throw new Error(`Sentry: Error deploying release - ${(err as Error).toString()}`);
}
}
_apiParameters(): ApiParameters | undefined {
if (!this.sentry.dsn || !this.sentry.authToken || !this.sentry.release) {
// Not configured for API access
return;
}
const organization = this.sentry.organization;
const release = this.sentry.release;
if (!organization) {
throw new Error("Organization not set");
}
if (typeof release !== "object" || typeof release?.version !== "string") {
throw new Error("Release version not set");
}
return {
authToken: this.sentry.authToken,
organization,
project: this.sentry.project,
refs: release.refs,
version: release.version,
};
}
getRandomVersion(): string {
return uuid().replace(/-/g, "");
}
}
module.exports = SentryPlugin;