@@ -3,11 +3,21 @@ import { hostname } from 'os';
3
3
import { basename , resolve } from 'path' ;
4
4
import { types } from 'util' ;
5
5
/* eslint-disable max-lines */
6
- import type { Scope } from '@sentry/node' ;
7
- import * as Sentry from '@sentry/node' ;
8
- import { captureException , captureMessage , flush , getCurrentHub , withScope } from '@sentry/node' ;
9
- import type { Integration , SdkMetadata } from '@sentry/types' ;
10
- import { isString , logger , tracingContextFromHeaders } from '@sentry/utils' ;
6
+ import type { NodeOptions , Scope } from '@sentry/node' ;
7
+ import { SDK_VERSION } from '@sentry/node' ;
8
+ import {
9
+ captureException ,
10
+ captureMessage ,
11
+ continueTrace ,
12
+ defaultIntegrations as nodeDefaultIntegrations ,
13
+ flush ,
14
+ getCurrentScope ,
15
+ init as initNode ,
16
+ startSpanManual ,
17
+ withScope ,
18
+ } from '@sentry/node' ;
19
+ import type { Integration , SdkMetadata , Span } from '@sentry/types' ;
20
+ import { isString , logger } from '@sentry/utils' ;
11
21
// 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
12
22
import type { Context , Handler } from 'aws-lambda' ;
13
23
import { performance } from 'perf_hooks' ;
@@ -55,9 +65,9 @@ export interface WrapperOptions {
55
65
startTrace : boolean ;
56
66
}
57
67
58
- export const defaultIntegrations : Integration [ ] = [ ...Sentry . defaultIntegrations , new AWSServices ( { optional : true } ) ] ;
68
+ export const defaultIntegrations : Integration [ ] = [ ...nodeDefaultIntegrations , new AWSServices ( { optional : true } ) ] ;
59
69
60
- interface AWSLambdaOptions extends Sentry . NodeOptions {
70
+ interface AWSLambdaOptions extends NodeOptions {
61
71
/**
62
72
* Internal field that is set to `true` when init() is called by the Sentry AWS Lambda layer.
63
73
*
@@ -66,7 +76,9 @@ interface AWSLambdaOptions extends Sentry.NodeOptions {
66
76
}
67
77
68
78
/**
69
- * @see {@link Sentry.init }
79
+ * Initializes the Sentry AWS Lambda SDK.
80
+ *
81
+ * @param options Configuration options for the SDK, @see {@link AWSLambdaOptions}.
70
82
*/
71
83
export function init ( options : AWSLambdaOptions = { } ) : void {
72
84
const opts = {
@@ -81,13 +93,13 @@ export function init(options: AWSLambdaOptions = {}): void {
81
93
packages : [
82
94
{
83
95
name : 'npm:@sentry/serverless' ,
84
- version : Sentry . SDK_VERSION ,
96
+ version : SDK_VERSION ,
85
97
} ,
86
98
] ,
87
- version : Sentry . SDK_VERSION ,
99
+ version : SDK_VERSION ,
88
100
} ;
89
101
90
- Sentry . init ( opts ) ;
102
+ initNode ( opts ) ;
91
103
}
92
104
93
105
/** */
@@ -290,44 +302,13 @@ export function wrapHandler<TEvent, TResult>(
290
302
} , timeoutWarningDelay ) as unknown as NodeJS . Timeout ;
291
303
}
292
304
293
- const hub = getCurrentHub ( ) ;
294
-
295
- let transaction : Sentry . Transaction | undefined ;
296
- if ( options . startTrace ) {
297
- const eventWithHeaders = event as { headers ?: { [ key : string ] : string } } ;
298
-
299
- const sentryTrace =
300
- eventWithHeaders . headers && isString ( eventWithHeaders . headers [ 'sentry-trace' ] )
301
- ? eventWithHeaders . headers [ 'sentry-trace' ]
302
- : undefined ;
303
- const baggage = eventWithHeaders . headers ?. baggage ;
304
- const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
305
- sentryTrace ,
306
- baggage ,
307
- ) ;
308
- Sentry . getCurrentScope ( ) . setPropagationContext ( propagationContext ) ;
309
-
310
- transaction = hub . startTransaction ( {
311
- name : context . functionName ,
312
- op : 'function.aws.lambda' ,
313
- origin : 'auto.function.serverless' ,
314
- ...traceparentData ,
315
- metadata : {
316
- dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
317
- source : 'component' ,
318
- } ,
319
- } ) ;
320
- }
305
+ async function processResult ( span ?: Span ) : Promise < TResult > {
306
+ const scope = getCurrentScope ( ) ;
321
307
322
- return withScope ( async scope => {
323
308
let rv : TResult ;
324
309
try {
325
310
enhanceScopeWithEnvironmentData ( scope , context , START_TIME ) ;
326
- if ( options . startTrace ) {
327
- enhanceScopeWithTransactionData ( scope , context ) ;
328
- // We put the transaction on the scope so users can attach children to it
329
- scope . setSpan ( transaction ) ;
330
- }
311
+
331
312
rv = await asyncHandler ( event , context ) ;
332
313
333
314
// We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
@@ -342,12 +323,46 @@ export function wrapHandler<TEvent, TResult>(
342
323
throw e ;
343
324
} finally {
344
325
clearTimeout ( timeoutWarningTimer ) ;
345
- transaction ?. end ( ) ;
326
+ span ?. end ( ) ;
346
327
await flush ( options . flushTimeout ) . catch ( e => {
347
328
DEBUG_BUILD && logger . error ( e ) ;
348
329
} ) ;
349
330
}
350
331
return rv ;
332
+ }
333
+
334
+ if ( options . startTrace ) {
335
+ const eventWithHeaders = event as { headers ?: { [ key : string ] : string } } ;
336
+
337
+ const sentryTrace =
338
+ eventWithHeaders . headers && isString ( eventWithHeaders . headers [ 'sentry-trace' ] )
339
+ ? eventWithHeaders . headers [ 'sentry-trace' ]
340
+ : undefined ;
341
+ const baggage = eventWithHeaders . headers ?. baggage ;
342
+
343
+ const continueTraceContext = continueTrace ( { sentryTrace, baggage } ) ;
344
+
345
+ return startSpanManual (
346
+ {
347
+ name : context . functionName ,
348
+ op : 'function.aws.lambda' ,
349
+ origin : 'auto.function.serverless' ,
350
+ ...continueTraceContext ,
351
+ metadata : {
352
+ ...continueTraceContext . metadata ,
353
+ source : 'component' ,
354
+ } ,
355
+ } ,
356
+ span => {
357
+ enhanceScopeWithTransactionData ( getCurrentScope ( ) , context ) ;
358
+
359
+ return processResult ( span ) ;
360
+ } ,
361
+ ) ;
362
+ }
363
+
364
+ return withScope ( async ( ) => {
365
+ return processResult ( undefined ) ;
351
366
} ) ;
352
367
} ;
353
368
}
0 commit comments