@@ -4,14 +4,12 @@ import {
4
4
flush ,
5
5
getCurrentHub ,
6
6
Scope ,
7
- SDK_VERSION ,
8
7
Severity ,
9
8
startTransaction ,
10
9
withScope ,
11
10
} from '@sentry/node' ;
12
11
import * as Sentry from '@sentry/node' ;
13
12
import { Integration } from '@sentry/types' ;
14
- import { addExceptionMechanism } from '@sentry/utils' ;
15
13
// 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
16
14
// eslint-disable-next-line import/no-unresolved
17
15
import { Context , Handler } from 'aws-lambda' ;
@@ -20,6 +18,7 @@ import { performance } from 'perf_hooks';
20
18
import { types } from 'util' ;
21
19
22
20
import { AWSServices } from './awsservices' ;
21
+ import { serverlessEventProcessor } from './utils' ;
23
22
24
23
export * from '@sentry/node' ;
25
24
@@ -54,37 +53,8 @@ export function init(options: Sentry.NodeOptions = {}): void {
54
53
if ( options . defaultIntegrations === undefined ) {
55
54
options . defaultIntegrations = defaultIntegrations ;
56
55
}
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' ) ) ;
88
58
}
89
59
90
60
/**
@@ -125,20 +95,6 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context): void {
125
95
} ) ;
126
96
}
127
97
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
-
142
98
/**
143
99
* Wraps a lambda handler adding it error capture and tracing capabilities.
144
100
*
@@ -205,8 +161,6 @@ export function wrapHandler<TEvent, TResult>(
205
161
206
162
timeoutWarningTimer = setTimeout ( ( ) => {
207
163
withScope ( scope => {
208
- addServerlessEventProcessor ( scope ) ;
209
- enhanceScopeWithEnvironmentData ( scope , context ) ;
210
164
scope . setTag ( 'timeout' , humanReadableTimeout ) ;
211
165
captureMessage ( `Possible function timeout: ${ context . functionName } ` , Severity . Warning ) ;
212
166
} ) ;
@@ -217,22 +171,24 @@ export function wrapHandler<TEvent, TResult>(
217
171
name : context . functionName ,
218
172
op : 'awslambda.handler' ,
219
173
} ) ;
220
- // We put the transaction on the scope so users can attach children to it
221
- getCurrentHub ( ) . configureScope ( scope => {
222
- scope . setSpan ( transaction ) ;
223
- } ) ;
224
174
175
+ const hub = getCurrentHub ( ) ;
176
+ const scope = hub . pushScope ( ) ;
225
177
let rv : TResult | undefined ;
226
178
try {
179
+ enhanceScopeWithEnvironmentData ( scope , context ) ;
180
+ // We put the transaction on the scope so users can attach children to it
181
+ scope . setSpan ( transaction ) ;
227
182
rv = await asyncHandler ( event , context ) ;
228
183
} catch ( e ) {
229
- captureExceptionWithContext ( e , context ) ;
184
+ captureException ( e ) ;
230
185
if ( options . rethrowAfterCapture ) {
231
186
throw e ;
232
187
}
233
188
} finally {
234
189
clearTimeout ( timeoutWarningTimer ) ;
235
190
transaction . finish ( ) ;
191
+ hub . popScope ( ) ;
236
192
await flush ( options . flushTimeout ) ;
237
193
}
238
194
return rv ;
0 commit comments