1- import type { Integration , Options , Scope , Span } from '@sentry/core' ;
2- import {
3- applySdkMetadata ,
4- debug ,
5- getSDKSource ,
6- SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
7- SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
8- } from '@sentry/core' ;
1+ import type { Integration , Options , Scope } from '@sentry/core' ;
2+ import { applySdkMetadata , debug , getSDKSource } from '@sentry/core' ;
93import type { NodeClient , NodeOptions } from '@sentry/node' ;
104import {
115 captureException ,
126 captureMessage ,
13- continueTrace ,
147 flush ,
158 getCurrentScope ,
169 getDefaultIntegrationsWithoutPerformance ,
1710 initWithoutDefaultIntegrations ,
18- startSpanManual ,
1911 withScope ,
2012} from '@sentry/node' ;
2113import type { Context , Handler } from 'aws-lambda' ;
2214import { existsSync } from 'fs' ;
23- import { hostname } from 'os' ;
2415import { basename , resolve } from 'path' ;
2516import { performance } from 'perf_hooks' ;
2617import { types } from 'util' ;
2718import { DEBUG_BUILD } from './debug-build' ;
2819import { awsIntegration } from './integration/aws' ;
2920import { awsLambdaIntegration } from './integration/awslambda' ;
30- import { getAwsTraceData , markEventUnhandled } from './utils' ;
21+ import { markEventUnhandled } from './utils' ;
3122
3223const { isPromise } = types ;
3324
@@ -54,12 +45,6 @@ export interface WrapperOptions {
5445 * @default false
5546 */
5647 captureAllSettledReasons : boolean ;
57- /**
58- * Automatically trace all handler invocations.
59- * You may want to disable this if you use express within Lambda.
60- * @default true
61- */
62- startTrace : boolean ;
6348}
6449
6550/**
@@ -207,18 +192,6 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
207192 } ) ;
208193}
209194
210- /**
211- * Adds additional transaction-related information from the environment and AWS Context to the Sentry Scope.
212- *
213- * @param scope Scope that should be enhanced
214- * @param context AWS Lambda context that will be used to extract some part of the data
215- */
216- function enhanceScopeWithTransactionData ( scope : Scope , context : Context ) : void {
217- scope . setTransactionName ( context . functionName ) ;
218- scope . setTag ( 'server_name' , process . env . _AWS_XRAY_DAEMON_ADDRESS || process . env . SENTRY_NAME || hostname ( ) ) ;
219- scope . setTag ( 'url' , `awslambda:///${ context . functionName } ` ) ;
220- }
221-
222195/**
223196 * Wraps a lambda handler adding it error capture and tracing capabilities.
224197 *
@@ -237,7 +210,6 @@ export function wrapHandler<TEvent, TResult>(
237210 captureTimeoutWarning : true ,
238211 timeoutWarningLimit : 500 ,
239212 captureAllSettledReasons : false ,
240- startTrace : true ,
241213 ...wrapOptions ,
242214 } ;
243215 let timeoutWarningTimer : NodeJS . Timeout ;
@@ -293,7 +265,7 @@ export function wrapHandler<TEvent, TResult>(
293265 } , timeoutWarningDelay ) as unknown as NodeJS . Timeout ;
294266 }
295267
296- async function processResult ( span ?: Span ) : Promise < TResult > {
268+ async function processResult ( ) : Promise < TResult > {
297269 const scope = getCurrentScope ( ) ;
298270
299271 let rv : TResult ;
@@ -314,60 +286,15 @@ export function wrapHandler<TEvent, TResult>(
314286 throw e ;
315287 } finally {
316288 clearTimeout ( timeoutWarningTimer ) ;
317- if ( span ?. isRecording ( ) ) {
318- span . end ( ) ;
319- }
320289 await flush ( options . flushTimeout ) . catch ( e => {
321290 DEBUG_BUILD && debug . error ( e ) ;
322291 } ) ;
323292 }
324293 return rv ;
325294 }
326295
327- // Only start a trace and root span if the handler is not already wrapped by Otel instrumentation
328- // Otherwise, we create two root spans (one from otel, one from our wrapper).
329- // If Otel instrumentation didn't work or was filtered by users, we still want to trace the handler.
330- // TODO: Since bumping the OTEL Instrumentation, this is likely not needed anymore, we can possibly remove this (can be done whenever since it would be non-breaking)
331- if ( options . startTrace && ! isWrappedByOtel ( handler ) ) {
332- const traceData = getAwsTraceData ( event as { headers ?: Record < string , string > } , context ) ;
333-
334- return continueTrace ( { sentryTrace : traceData [ 'sentry-trace' ] , baggage : traceData . baggage } , ( ) => {
335- return startSpanManual (
336- {
337- name : context . functionName ,
338- op : 'function.aws.lambda' ,
339- attributes : {
340- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'component' ,
341- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.function.serverless' ,
342- } ,
343- } ,
344- span => {
345- enhanceScopeWithTransactionData ( getCurrentScope ( ) , context ) ;
346-
347- return processResult ( span ) ;
348- } ,
349- ) ;
350- } ) ;
351- }
352-
353296 return withScope ( async ( ) => {
354- return processResult ( undefined ) ;
297+ return processResult ( ) ;
355298 } ) ;
356299 } ;
357300}
358-
359- /**
360- * Checks if Otel's AWSLambda instrumentation successfully wrapped the handler.
361- * Check taken from @opentelemetry/core
362- */
363- function isWrappedByOtel (
364- // eslint-disable-next-line @typescript-eslint/ban-types
365- handler : Function & { __original ?: unknown ; __unwrap ?: unknown ; __wrapped ?: boolean } ,
366- ) : boolean {
367- return (
368- typeof handler === 'function' &&
369- typeof handler . __original === 'function' &&
370- typeof handler . __unwrap === 'function' &&
371- handler . __wrapped === true
372- ) ;
373- }
0 commit comments