@@ -4,14 +4,12 @@ import {
44 flush ,
55 getCurrentHub ,
66 Scope ,
7- SDK_VERSION ,
87 Severity ,
98 startTransaction ,
109 withScope ,
1110} from '@sentry/node' ;
1211import * as Sentry from '@sentry/node' ;
1312import { Integration } from '@sentry/types' ;
14- import { addExceptionMechanism } from '@sentry/utils' ;
1513// 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
1614// eslint-disable-next-line import/no-unresolved
1715import { Context , Handler } from 'aws-lambda' ;
@@ -20,6 +18,7 @@ import { performance } from 'perf_hooks';
2018import { types } from 'util' ;
2119
2220import { AWSServices } from './awsservices' ;
21+ import { serverlessEventProcessor } from './utils' ;
2322
2423export * from '@sentry/node' ;
2524
@@ -54,37 +53,8 @@ export function init(options: Sentry.NodeOptions = {}): void {
5453 if ( options . defaultIntegrations === undefined ) {
5554 options . defaultIntegrations = defaultIntegrations ;
5655 }
57- return Sentry . init ( options ) ;
58- }
59-
60- /**
61- * Add event processor that will override SDK details to point to the serverless SDK instead of Node,
62- * as well as set correct mechanism type, which should be set to `handled: false`.
63- * We do it like this, so that we don't introduce any side-effects in this module, which makes it tree-shakeable.
64- * @param scope Scope that processor should be added to
65- */
66- function addServerlessEventProcessor ( scope : Scope ) : void {
67- scope . addEventProcessor ( event => {
68- event . sdk = {
69- ...event . sdk ,
70- name : 'sentry.javascript.serverless' ,
71- integrations : [ ...( ( event . sdk && event . sdk . integrations ) || [ ] ) , 'AWSLambda' ] ,
72- packages : [
73- ...( ( event . sdk && event . sdk . packages ) || [ ] ) ,
74- {
75- name : 'npm:@sentry/serverless' ,
76- version : SDK_VERSION ,
77- } ,
78- ] ,
79- version : SDK_VERSION ,
80- } ;
81-
82- addExceptionMechanism ( event , {
83- handled : false ,
84- } ) ;
85-
86- return event ;
87- } ) ;
56+ Sentry . init ( options ) ;
57+ Sentry . addGlobalEventProcessor ( serverlessEventProcessor ( 'AWSLambda' ) ) ;
8858}
8959
9060/**
@@ -125,20 +95,6 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context): void {
12595 } ) ;
12696}
12797
128- /**
129- * Capture exception with a a context.
130- *
131- * @param e exception to be captured
132- * @param context Context
133- */
134- function captureExceptionWithContext ( e : unknown , context : Context ) : void {
135- withScope ( scope => {
136- addServerlessEventProcessor ( scope ) ;
137- enhanceScopeWithEnvironmentData ( scope , context ) ;
138- captureException ( e ) ;
139- } ) ;
140- }
141-
14298/**
14399 * Wraps a lambda handler adding it error capture and tracing capabilities.
144100 *
@@ -205,8 +161,6 @@ export function wrapHandler<TEvent, TResult>(
205161
206162 timeoutWarningTimer = setTimeout ( ( ) => {
207163 withScope ( scope => {
208- addServerlessEventProcessor ( scope ) ;
209- enhanceScopeWithEnvironmentData ( scope , context ) ;
210164 scope . setTag ( 'timeout' , humanReadableTimeout ) ;
211165 captureMessage ( `Possible function timeout: ${ context . functionName } ` , Severity . Warning ) ;
212166 } ) ;
@@ -217,22 +171,24 @@ export function wrapHandler<TEvent, TResult>(
217171 name : context . functionName ,
218172 op : 'awslambda.handler' ,
219173 } ) ;
220- // We put the transaction on the scope so users can attach children to it
221- getCurrentHub ( ) . configureScope ( scope => {
222- scope . setSpan ( transaction ) ;
223- } ) ;
224174
175+ const hub = getCurrentHub ( ) ;
176+ const scope = hub . pushScope ( ) ;
225177 let rv : TResult | undefined ;
226178 try {
179+ enhanceScopeWithEnvironmentData ( scope , context ) ;
180+ // We put the transaction on the scope so users can attach children to it
181+ scope . setSpan ( transaction ) ;
227182 rv = await asyncHandler ( event , context ) ;
228183 } catch ( e ) {
229- captureExceptionWithContext ( e , context ) ;
184+ captureException ( e ) ;
230185 if ( options . rethrowAfterCapture ) {
231186 throw e ;
232187 }
233188 } finally {
234189 clearTimeout ( timeoutWarningTimer ) ;
235190 transaction . finish ( ) ;
191+ hub . popScope ( ) ;
236192 await flush ( options . flushTimeout ) ;
237193 }
238194 return rv ;
0 commit comments