@@ -10,8 +10,10 @@ import {
1010 InstrumentationNodeModuleFile ,
1111 isWrapped ,
1212} from '@opentelemetry/instrumentation' ;
13- import { diag } from '@opentelemetry/api' ;
13+ import { diag , Span , SpanStatusCode } from '@opentelemetry/api' ;
1414
15+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
16+ // @ts -ignore
1517export class AwsLambdaInstrumentationPatch extends AwsLambdaInstrumentation {
1618 override init ( ) {
1719 // Custom logic before calling the original implementation
@@ -117,4 +119,39 @@ export class AwsLambdaInstrumentationPatch extends AwsLambdaInstrumentation {
117119 ] ;
118120 }
119121 }
122+
123+ // Override the upstream private _endSpan method to not print Metric not flush error message which is not applicable to ADOT
124+ // Lambda Layer
125+ // https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts#L358-L398
126+ override _endSpan ( span : Span , err : string | Error | null | undefined , callback : ( ) => void ) {
127+ if ( err ) {
128+ span . recordException ( err ) ;
129+ }
130+
131+ let errMessage ;
132+ if ( typeof err === 'string' ) {
133+ errMessage = err ;
134+ } else if ( err ) {
135+ errMessage = err . message ;
136+ }
137+ if ( errMessage ) {
138+ span . setStatus ( {
139+ code : SpanStatusCode . ERROR ,
140+ message : errMessage ,
141+ } ) ;
142+ }
143+
144+ span . end ( ) ;
145+
146+ const flushers = [ ] ;
147+ if ( ( this as any ) . _traceForceFlusher ) {
148+ flushers . push ( ( this as any ) . _traceForceFlusher ( ) ) ;
149+ } else {
150+ diag . error (
151+ 'Spans may not be exported for the lambda function because we are not force flushing before callback.'
152+ ) ;
153+ }
154+
155+ Promise . all ( flushers ) . then ( callback , callback ) ;
156+ }
120157}
0 commit comments