@@ -47,6 +47,12 @@ export interface WrapperOptions {
4747 * @default false
4848 */
4949 captureAllSettledReasons : boolean ;
50+ /**
51+ * Automatically trace all handler invocations.
52+ * You may want to disable this if you use express within Lambda (use tracingHandler instead).
53+ * @default true
54+ */
55+ startTrace : boolean ;
5056}
5157
5258export const defaultIntegrations : Integration [ ] = [ ...Sentry . defaultIntegrations , new AWSServices ( { optional : true } ) ] ;
@@ -178,11 +184,6 @@ function tryGetRemainingTimeInMillis(context: Context): number {
178184 * @param startTime performance.now() when wrapHandler was invoked
179185 */
180186function enhanceScopeWithEnvironmentData ( scope : Scope , context : Context , startTime : number ) : void {
181- scope . setTransactionName ( context . functionName ) ;
182-
183- scope . setTag ( 'server_name' , process . env . _AWS_XRAY_DAEMON_ADDRESS || process . env . SENTRY_NAME || hostname ( ) ) ;
184- scope . setTag ( 'url' , `awslambda:///${ context . functionName } ` ) ;
185-
186187 scope . setContext ( 'aws.lambda' , {
187188 aws_request_id : context . awsRequestId ,
188189 function_name : context . functionName ,
@@ -204,6 +205,18 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
204205 } ) ;
205206}
206207
208+ /**
209+ * Adds additional transaction-related information from the environment and AWS Context to the Sentry Scope.
210+ *
211+ * @param scope Scope that should be enhanced
212+ * @param context AWS Lambda context that will be used to extract some part of the data
213+ */
214+ function enhanceScopeWithTransactionData ( scope : Scope , context : Context ) : void {
215+ scope . setTransactionName ( context . functionName ) ;
216+ scope . setTag ( 'server_name' , process . env . _AWS_XRAY_DAEMON_ADDRESS || process . env . SENTRY_NAME || hostname ( ) ) ;
217+ scope . setTag ( 'url' , `awslambda:///${ context . functionName } ` ) ;
218+ }
219+
207220/**
208221 * Wraps a lambda handler adding it error capture and tracing capabilities.
209222 *
@@ -222,6 +235,7 @@ export function wrapHandler<TEvent, TResult>(
222235 captureTimeoutWarning : true ,
223236 timeoutWarningLimit : 500 ,
224237 captureAllSettledReasons : false ,
238+ startTrace : true ,
225239 ...wrapOptions ,
226240 } ;
227241 let timeoutWarningTimer : NodeJS . Timeout ;
@@ -279,36 +293,42 @@ export function wrapHandler<TEvent, TResult>(
279293
280294 const hub = getCurrentHub ( ) ;
281295
282- const eventWithHeaders = event as { headers ?: { [ key : string ] : string } } ;
283-
284- const sentryTrace =
285- eventWithHeaders . headers && isString ( eventWithHeaders . headers [ 'sentry-trace' ] )
286- ? eventWithHeaders . headers [ 'sentry-trace' ]
287- : undefined ;
288- const baggage = eventWithHeaders . headers ?. baggage ;
289- const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
290- sentryTrace ,
291- baggage ,
292- ) ;
293- hub . getScope ( ) . setPropagationContext ( propagationContext ) ;
294-
295- const transaction = hub . startTransaction ( {
296- name : context . functionName ,
297- op : 'function.aws.lambda' ,
298- origin : 'auto.function.serverless' ,
299- ...traceparentData ,
300- metadata : {
301- dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
302- source : 'component' ,
303- } ,
304- } ) as Sentry . Transaction | undefined ;
296+ let transaction : Sentry . Transaction | undefined ;
297+ if ( options . startTrace ) {
298+ const eventWithHeaders = event as { headers ?: { [ key : string ] : string } } ;
299+
300+ const sentryTrace =
301+ eventWithHeaders . headers && isString ( eventWithHeaders . headers [ 'sentry-trace' ] )
302+ ? eventWithHeaders . headers [ 'sentry-trace' ]
303+ : undefined ;
304+ const baggage = eventWithHeaders . headers ?. baggage ;
305+ const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
306+ sentryTrace ,
307+ baggage ,
308+ ) ;
309+ hub . getScope ( ) . setPropagationContext ( propagationContext ) ;
310+
311+ transaction = hub . startTransaction ( {
312+ name : context . functionName ,
313+ op : 'function.aws.lambda' ,
314+ origin : 'auto.function.serverless' ,
315+ ...traceparentData ,
316+ metadata : {
317+ dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
318+ source : 'component' ,
319+ } ,
320+ } ) ;
321+ }
305322
306323 const scope = hub . pushScope ( ) ;
307324 let rv : TResult ;
308325 try {
309326 enhanceScopeWithEnvironmentData ( scope , context , START_TIME ) ;
310- // We put the transaction on the scope so users can attach children to it
311- scope . setSpan ( transaction ) ;
327+ if ( options . startTrace ) {
328+ enhanceScopeWithTransactionData ( scope , context ) ;
329+ // We put the transaction on the scope so users can attach children to it
330+ scope . setSpan ( transaction ) ;
331+ }
312332 rv = await asyncHandler ( event , context ) ;
313333
314334 // We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
0 commit comments