@@ -45,6 +45,12 @@ export interface WrapperOptions {
4545 * @default false
4646 */
4747 captureAllSettledReasons : boolean ;
48+ /**
49+ * Automatically trace all handler invocations.
50+ * You may want to disable this if you use express within Lambda (use tracingHandler instead).
51+ * @default true
52+ */
53+ startTransaction : boolean ;
4854}
4955
5056export const defaultIntegrations : Integration [ ] = [ ...Sentry . defaultIntegrations , new AWSServices ( { optional : true } ) ] ;
@@ -175,11 +181,6 @@ function tryGetRemainingTimeInMillis(context: Context): number {
175181 * @param startTime performance.now() when wrapHandler was invoked
176182 */
177183function enhanceScopeWithEnvironmentData ( scope : Scope , context : Context , startTime : number ) : void {
178- scope . setTransactionName ( context . functionName ) ;
179-
180- scope . setTag ( 'server_name' , process . env . _AWS_XRAY_DAEMON_ADDRESS || process . env . SENTRY_NAME || hostname ( ) ) ;
181- scope . setTag ( 'url' , `awslambda:///${ context . functionName } ` ) ;
182-
183184 scope . setContext ( 'aws.lambda' , {
184185 aws_request_id : context . awsRequestId ,
185186 function_name : context . functionName ,
@@ -201,6 +202,18 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
201202 } ) ;
202203}
203204
205+ /**
206+ * Adds additional transaction-related information from the environment and AWS Context to the Sentry Scope.
207+ *
208+ * @param scope Scope that should be enhanced
209+ * @param context AWS Lambda context that will be used to extract some part of the data
210+ */
211+ function enhanceScopeWithTransactionData ( scope : Scope , context : Context ) : void {
212+ scope . setTransactionName ( context . functionName ) ;
213+ scope . setTag ( 'server_name' , process . env . _AWS_XRAY_DAEMON_ADDRESS || process . env . SENTRY_NAME || hostname ( ) ) ;
214+ scope . setTag ( 'url' , `awslambda:///${ context . functionName } ` ) ;
215+ }
216+
204217/**
205218 * Wraps a lambda handler adding it error capture and tracing capabilities.
206219 *
@@ -219,6 +232,7 @@ export function wrapHandler<TEvent, TResult>(
219232 captureTimeoutWarning : true ,
220233 timeoutWarningLimit : 500 ,
221234 captureAllSettledReasons : false ,
235+ startTransaction : true ,
222236 ...wrapOptions ,
223237 } ;
224238 let timeoutWarningTimer : NodeJS . Timeout ;
@@ -276,36 +290,42 @@ export function wrapHandler<TEvent, TResult>(
276290
277291 const hub = getCurrentHub ( ) ;
278292
279- const eventWithHeaders = event as { headers ?: { [ key : string ] : string } } ;
280-
281- const sentryTrace =
282- eventWithHeaders . headers && isString ( eventWithHeaders . headers [ 'sentry-trace' ] )
283- ? eventWithHeaders . headers [ 'sentry-trace' ]
284- : undefined ;
285- const baggage = eventWithHeaders . headers ?. baggage ;
286- const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
287- sentryTrace ,
288- baggage ,
289- ) ;
290- hub . getScope ( ) . setPropagationContext ( propagationContext ) ;
291-
292- const transaction = hub . startTransaction ( {
293- name : context . functionName ,
294- op : 'function.aws.lambda' ,
295- origin : 'auto.function.serverless' ,
296- ...traceparentData ,
297- metadata : {
298- dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
299- source : 'component' ,
300- } ,
301- } ) as Sentry . Transaction | undefined ;
293+ let transaction : Sentry . Transaction | undefined ;
294+ if ( options . startTransaction ) {
295+ const eventWithHeaders = event as { headers ?: { [ key : string ] : string } } ;
296+
297+ const sentryTrace =
298+ eventWithHeaders . headers && isString ( eventWithHeaders . headers [ 'sentry-trace' ] )
299+ ? eventWithHeaders . headers [ 'sentry-trace' ]
300+ : undefined ;
301+ const baggage = eventWithHeaders . headers ?. baggage ;
302+ const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
303+ sentryTrace ,
304+ baggage ,
305+ ) ;
306+ hub . getScope ( ) . setPropagationContext ( propagationContext ) ;
307+
308+ transaction = hub . startTransaction ( {
309+ name : context . functionName ,
310+ op : 'function.aws.lambda' ,
311+ origin : 'auto.function.serverless' ,
312+ ...traceparentData ,
313+ metadata : {
314+ dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
315+ source : 'component' ,
316+ } ,
317+ } ) ;
318+ }
302319
303320 const scope = hub . pushScope ( ) ;
304321 let rv : TResult ;
305322 try {
306323 enhanceScopeWithEnvironmentData ( scope , context , START_TIME ) ;
307- // We put the transaction on the scope so users can attach children to it
308- scope . setSpan ( transaction ) ;
324+ if ( options . startTransaction ) {
325+ enhanceScopeWithTransactionData ( scope , context ) ;
326+ // We put the transaction on the scope so users can attach children to it
327+ scope . setSpan ( transaction ) ;
328+ }
309329 rv = await asyncHandler ( event , context ) ;
310330
311331 // We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
0 commit comments