diff --git a/packages/serverless/src/awslambda.ts b/packages/serverless/src/awslambda.ts index be6350b04fae..97d7c88b2d7c 100644 --- a/packages/serverless/src/awslambda.ts +++ b/packages/serverless/src/awslambda.ts @@ -44,7 +44,7 @@ export interface WrapperOptions { timeoutWarningLimit: number; } -export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices()]; +export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices({ optional: true })]; /** * @see {@link Sentry.init} diff --git a/packages/serverless/src/awsservices.ts b/packages/serverless/src/awsservices.ts index 3c0500755178..2869c8a29b65 100644 --- a/packages/serverless/src/awsservices.ts +++ b/packages/serverless/src/awsservices.ts @@ -27,48 +27,60 @@ export class AWSServices implements Integration { */ public name: string = AWSServices.id; + private readonly _optional: boolean; + + public constructor(options: { optional?: boolean } = {}) { + this._optional = options.optional || false; + } + /** * @inheritDoc */ public setupOnce(): void { - const awsModule = require('aws-sdk/global') as typeof AWS; - fill( - awsModule.Service.prototype, - 'makeRequest', - ( - orig: MakeRequestFunction, - ): MakeRequestFunction => - function(this: TService, operation: string, params?: GenericParams, callback?: MakeRequestCallback) { - let transaction: Transaction | undefined; - let span: Span | undefined; - const scope = getCurrentHub().getScope(); - if (scope) { - transaction = scope.getTransaction(); - } - const req = orig.call(this, operation, params); - req.on('afterBuild', () => { - if (transaction) { - span = transaction.startChild({ - description: describe(this, operation, params), - op: 'aws.request', - }); - } - }); - req.on('complete', () => { - if (span) { - span.finish(); - } - }); - - if (callback) { - req.send(callback); - } - return req; - }, - ); + try { + const awsModule = require('aws-sdk/global') as typeof AWS; + fill(awsModule.Service.prototype, 'makeRequest', wrapMakeRequest); + } catch (e) { + if (!this._optional) { + throw e; + } + } } } +/** */ +function wrapMakeRequest( + orig: MakeRequestFunction, +): MakeRequestFunction { + return function(this: TService, operation: string, params?: GenericParams, callback?: MakeRequestCallback) { + let transaction: Transaction | undefined; + let span: Span | undefined; + const scope = getCurrentHub().getScope(); + if (scope) { + transaction = scope.getTransaction(); + } + const req = orig.call(this, operation, params); + req.on('afterBuild', () => { + if (transaction) { + span = transaction.startChild({ + description: describe(this, operation, params), + op: 'aws.request', + }); + } + }); + req.on('complete', () => { + if (span) { + span.finish(); + } + }); + + if (callback) { + req.send(callback); + } + return req; + }; +} + /** Describes an operation on generic AWS service */ function describe(service: TService, operation: string, params?: GenericParams): string { let ret = `aws.${service.serviceIdentifier}.${operation}`;