@@ -3,11 +3,21 @@ import { hostname } from 'os';
33import { basename , resolve } from 'path' ;
44import { types } from 'util' ;
55/* 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' ;
1121// 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
1222import type { Context , Handler } from 'aws-lambda' ;
1323import { performance } from 'perf_hooks' ;
@@ -55,9 +65,9 @@ export interface WrapperOptions {
5565 startTrace : boolean ;
5666}
5767
58- export const defaultIntegrations : Integration [ ] = [ ...Sentry . defaultIntegrations , new AWSServices ( { optional : true } ) ] ;
68+ export const defaultIntegrations : Integration [ ] = [ ...nodeDefaultIntegrations , new AWSServices ( { optional : true } ) ] ;
5969
60- interface AWSLambdaOptions extends Sentry . NodeOptions {
70+ interface AWSLambdaOptions extends NodeOptions {
6171 /**
6272 * Internal field that is set to `true` when init() is called by the Sentry AWS Lambda layer.
6373 *
@@ -66,7 +76,9 @@ interface AWSLambdaOptions extends Sentry.NodeOptions {
6676}
6777
6878/**
69- * @see {@link Sentry.init }
79+ * Initializes the Sentry AWS Lambda SDK.
80+ *
81+ * @param options Configuration options for the SDK, @see {@link AWSLambdaOptions}.
7082 */
7183export function init ( options : AWSLambdaOptions = { } ) : void {
7284 const opts = {
@@ -81,13 +93,13 @@ export function init(options: AWSLambdaOptions = {}): void {
8193 packages : [
8294 {
8395 name : 'npm:@sentry/serverless' ,
84- version : Sentry . SDK_VERSION ,
96+ version : SDK_VERSION ,
8597 } ,
8698 ] ,
87- version : Sentry . SDK_VERSION ,
99+ version : SDK_VERSION ,
88100 } ;
89101
90- Sentry . init ( opts ) ;
102+ initNode ( opts ) ;
91103}
92104
93105/** */
@@ -290,44 +302,13 @@ export function wrapHandler<TEvent, TResult>(
290302 } , timeoutWarningDelay ) as unknown as NodeJS . Timeout ;
291303 }
292304
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 ( ) ;
321307
322- return withScope ( async scope => {
323308 let rv : TResult ;
324309 try {
325310 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+
331312 rv = await asyncHandler ( event , context ) ;
332313
333314 // We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
@@ -342,12 +323,46 @@ export function wrapHandler<TEvent, TResult>(
342323 throw e ;
343324 } finally {
344325 clearTimeout ( timeoutWarningTimer ) ;
345- transaction ?. end ( ) ;
326+ span ?. end ( ) ;
346327 await flush ( options . flushTimeout ) . catch ( e => {
347328 DEBUG_BUILD && logger . error ( e ) ;
348329 } ) ;
349330 }
350331 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 ) ;
351366 } ) ;
352367 } ;
353368}
0 commit comments