8000 Add `optional` parameter to AWSServices integration (#3030) · phstc/sentry-javascript@0a2bfdb · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a2bfdb

Browse files
authored
Add optional parameter to AWSServices integration (getsentry#3030)
Initially, I thought there's no need to catch the error of `require('aws-sdk/global')` because this package is always available on AWS Lambda and even in the `sam local` emulator. However, in some environments it could be missing. Examples of such environments is netlify dev or it could be simply a unit-test environment without any packages. Fixes getsentry#3000.
1 parent 4b20bfb commit 0a2bfdb

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

packages/serverless/src/awslambda.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface WrapperOptions {
4444
timeoutWarningLimit: number;
4545
}
4646

47-
export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices()];
47+
export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices({ optional: true })];
4848

4949
/**
5050
* @see {@link Sentry.init}

packages/serverless/src/awsservices.ts

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,60 @@ export class AWSServices implements Integration {
2727
*/
2828
public name: string = AWSServices.id;
2929

30+
private readonly _optional: boolean;
31+
32+
public constructor(options: { optional?: boolean } = {}) {
33+
this._optional = options.optional || false;
34+
}
35+
3036
/**
3137
* @inheritDoc
3238
*/
3339
public setupOnce(): void {
34-
const awsModule = require('aws-sdk/global') as typeof AWS;
35-
fill(
36-
awsModule.Service.prototype,
37-
'makeRequest',
38-
<TService extends AWSService, TResult>(
39-
orig: MakeRequestFunction<GenericParams, TResult>,
40-
): MakeRequestFunction<GenericParams, TResult> =>
41-
function(this: TService, operation: string, params?: GenericParams, callback?: MakeRequestCallback<TResult>) {
42-
let transaction: Transaction | undefined;
43-
let span: Span | undefined;
44-
const scope = getCurrentHub().getScope();
45-
if (scope) {
46-
transaction = scope.getTransaction();
47-
}
48-
const req = orig.call(this, operation, params);
49-
req.on('afterBuild', () => {
50-
if (transaction) {
51-
span = transaction.startChild({
52-
description: describe(this, operation, params),
53-
op: 'aws.request',
54-
});
55-
}
56-
});
57-
req.on('complete', () => {
58-
if (span) {
59-
span.finish();
60-
}
61-
});
62-
63-
if (callback) {
64-
req.send(callback);
65-
}
66-
return req;
67-
},
68-
);
40+
try {
41+
const awsModule = require('aws-sdk/global') as typeof AWS;
42+
fill(awsModule.Service.prototype, 'makeRequest', wrapMakeRequest);
43+
} catch (e) {
44+
if (!this._optional) {
45+
throw e;
46+
}
47+
}
6948
}
7049
}
7150

51+
/** */
52+
function wrapMakeRequest<TService extends AWSService, TResult>(
53+
orig: MakeRequestFunction<GenericParams, TResult>,
54+
): MakeRequestFunction<GenericParams, TResult> {
55+
return function(this: TService, operation: string, params?: GenericParams, callback?: MakeRequestCallback<TResult>) {
56+
let transaction: Transaction | undefined;
57+
let span: Span | undefined;
58+
const scope = getCurrentHub().getScope();
59+
if (scope) {
60+
transaction = scope.getTransaction();
61+
}
62+
const req = orig.call(this, operation, params);
63+
req.on('afterBuild', () => {
64+
if (transaction) {
65+
span = transaction.startChild({
66+
description: describe(this, operation, params),
67+
op: 'aws.request',
68+
});
69+
}
70+
});
71+
req.on('complete', () => {
72+
if (span) {
73+
span.finish();
74+
}
75+
});
76+
77+
if (callback) {
78+
req.send(callback);
79+
}
80+
return req;
81+
};
82+
}
83+
7284
/** Describes an operation on generic AWS service */
7385
function describe<TService extends AWSService>(service: TService, operation: string, params?: GenericParams): string {
7486
let ret = `aws.${service.serviceIdentifier}.${operation}`;

0 commit comments

Comments
 (0)
0