@@ -45,6 +45,12 @@ export interface WrapperOptions {
45
45
* @default false
46
46
*/
47
47
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 ;
48
54
}
49
55
50
56
export const defaultIntegrations : Integration [ ] = [ ...Sentry . defaultIntegrations , new AWSServices ( { optional : true } ) ] ;
@@ -175,11 +181,6 @@ function tryGetRemainingTimeInMillis(context: Context): number {
175
181
* @param startTime performance.now() when wrapHandler was invoked
176
182
*/
177
183
function 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
-
183
184
scope . setContext ( 'aws.lambda' , {
184
185
aws_request_id : context . awsRequestId ,
185
186
function_name : context . functionName ,
@@ -201,6 +202,18 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
201
202
} ) ;
202
203
}
203
204
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
+
204
217
/**
205
218
* Wraps a lambda handler adding it error capture and tracing capabilities.
206
219
*
@@ -219,6 +232,7 @@ export function wrapHandler<TEvent, TResult>(
219
232
captureTimeoutWarning : true ,
220
233
timeoutWarningLimit : 500 ,
221
234
captureAllSettledReasons : false ,
235
+ startTransaction : true ,
222
236
...wrapOptions ,
223
237
} ;
224
238
let timeoutWarningTimer : NodeJS . Timeout ;
@@ -276,36 +290,42 @@ export function wrapHandler<TEvent, TResult>(
276
290
277
291
const hub = getCurrentHub ( ) ;
278
292
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
+ }
302
319
303
320
const scope = hub . pushScope ( ) ;
304
321
let rv : TResult ;
305
322
try {
306
323
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
+ }
309
329
rv = await asyncHandler ( event , context ) ;
310
330
311
331
// We manage lambdas that use Promise.allSettled by capturing the errors of failed promises
0 commit comments