@@ -47,6 +47,12 @@ export interface WrapperOptions {
47
47
* @default false
48
48
*/
49
49
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
+ startTransaction : boolean ;
50
56
}
51
57
52
58
export const defaultIntegrations : Integration [ ] = [ ...Sentry . defaultIntegrations , new AWSServices ( { optional : true } ) ] ;
@@ -178,11 +184,6 @@ function tryGetRemainingTimeInMillis(context: Context): number {
178
184
* @param startTime performance.now() when wrapHandler was invoked
179
185
*/
180
186
function 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
-
186
187
scope . setContext ( 'aws.lambda' , {
187
188
aws_request_id : context . awsRequestId ,
188
189
function_name : context . functionName ,
@@ -204,6 +205,18 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
204
205
} ) ;
205
206
}
206
207
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
+
207
220
/**
208
221
* Wraps a lambda handler adding it error capture and tracing capabilities.
209
222
*
@@ -222,6 +235,7 @@ export function wrapHandler<TEvent, TResult>(
222
235
captureTimeoutWarning : true ,
223
236
timeoutWarningLimit : 500 ,
224
237
captureAllSettledReasons : false ,
238
+ startTransaction : true ,
225
239
...wrapOptions ,
226
240
} ;
227
241
let timeoutWarningTimer : NodeJS . Timeout ;
@@ -279,36 +293,42 @@ export function wrapHandler<TEvent, TResult>(
279
293
280
294
const hub = getCurrentHub ( ) ;
281
295
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 . startTransaction ) {
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
+ }
305
322
306
323
const scope = hub . pushScope ( ) ;
307
324
let rv : TResult ;
308
325
try {
309
326
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 . startTransaction ) {
328
+ enhanceScopeWithTransactionData ( scope , context ) ;
329
+ // We put the transaction on the scope so users can attach children to it
330
+ scope . setSpan ( transaction ) ;
331
+ }
312
332
rv = await asyncHandler ( event , context ) ;
313
333
314
334
// We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
0 commit comments