@@ -67,13 +67,15 @@ export abstract class BaseInjector implements Injector {
6767 requireParentSpan = false ,
6868 dynamicAttributesHook ?: DynamicAttributesHook ,
6969 ) : Function {
70- const method = {
71- [ func . name ] ( ...args : any [ ] ) {
70+ const method = new Proxy ( func , {
71+ apply : ( target , thisArg , args : any [ ] ) => {
72+ const stackObject = { }
73+ Error . captureStackTrace ( stackObject )
7274 const tracer = trace . getTracer ( 'default' )
7375 const ctx = context . active ( )
7476 const parentSpan = trace . getSpan ( ctx )
7577 if ( requireParentSpan && ( parentSpan == null || parentSpan . spanContext ( ) === INVALID_SPAN_CONTEXT ) )
76- return func . apply ( this , args )
78+ return Reflect . apply ( target , thisArg , args )
7779 const span = tracer . startSpan (
7880 traceName ,
7981 spanOptions ,
@@ -82,27 +84,29 @@ export abstract class BaseInjector implements Injector {
8284 const contextWithSpan = trace . setSpan ( ctx , span )
8385 return context . with ( contextWithSpan , ( currentSpan ) => {
8486 if ( dynamicAttributesHook )
85- currentSpan . setAttributes ( dynamicAttributesHook ( { args, thisArg : this , parentSpan } ) )
87+ currentSpan . setAttributes ( dynamicAttributesHook ( { args, thisArg, parentSpan } ) )
8688
89+ const beforeApplyStackObject = { }
90+ Error . captureStackTrace ( beforeApplyStackObject )
8791 try {
88- const result = func . apply ( this , args )
92+ const result = Reflect . apply ( target , thisArg , args )
8993 if ( result instanceof Promise ) {
9094 return result
9195 . then ( ( res ) => {
9296 currentSpan . end ( )
9397 return res
9498 } )
95- . catch ( error => BaseInjector . recordException ( error , currentSpan ) )
99+ . catch ( error => BaseInjector . recordException ( error , [ stackObject , beforeApplyStackObject ] , currentSpan ) )
96100 }
97101 currentSpan . end ( )
98102 return result
99103 }
100104 catch ( error ) {
101- BaseInjector . recordException ( error as Error , currentSpan )
105+ BaseInjector . recordException ( error as Error , [ stackObject , beforeApplyStackObject ] , currentSpan )
102106 }
103107 } , undefined , span )
104108 } ,
105- } [ func . name ]
109+ } )
106110
107111 Reflect . defineMetadata ( TRACE_METADATA , {
108112 ...spanOptions ,
@@ -114,7 +118,20 @@ export abstract class BaseInjector implements Injector {
114118 return method
115119 }
116120
117- protected static recordException ( error : Error , span : Span ) : never {
121+ protected static recordException ( error : Error , [ stackObject , beforeApplyStackObject ] : [ { stack ?: string } , { stack ?: string } ] , span : Span ) : never {
122+ if ( stackObject . stack && beforeApplyStackObject . stack ) {
123+ const preCallSites = stackObject . stack . split ( '\n' )
124+ const beforeCallSites = beforeApplyStackObject . stack . split ( '\n' )
125+ const callSites = error . stack ?. split ( '\n' ) ?? [ ]
126+ if ( callSites . length > 1 ) {
127+ const startLine = callSites . findIndex ( s => s . startsWith ( beforeCallSites [ 1 ] . split ( ':' ) [ 0 ] ) )
128+ const endLine = callSites . findIndex ( s => s . startsWith ( preCallSites [ 1 ] . split ( ':' ) [ 0 ] ) )
129+ if ( startLine !== - 1 && endLine !== - 1 ) {
130+ const newCallSites = callSites . slice ( 0 , startLine ) . concat ( callSites . slice ( endLine + 1 ) )
131+ error . stack = newCallSites . join ( '\n' )
132+ }
133+ }
134+ }
118135 span . recordException ( error )
119136 span . setStatus ( { code : SpanStatusCode . ERROR , message : error . message } )
120137 span . end ( )
0 commit comments