@@ -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,38 @@ export class AwsLambdaInstrumentationPatch extends AwsLambdaInstrumentation {
117119 ] ;
118120 }
119121 }
122+
123+ // Override the upstream private _endSpan method to remove the unnecessary metric force-flush error message
124+ // https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts#L358-L398
125+ override _endSpan ( span : Span , err : string | Error | null | undefined , callback : ( ) => void ) {
126+ if ( err ) {
127+ span . recordException ( err ) ;
128+ }
129+
130+ let errMessage ;
131+ if ( typeof err === 'string' ) {
132+ errMessage = err ;
133+ } else if ( err ) {
134+ errMessage = err . message ;
135+ }
136+ if ( errMessage ) {
137+ span . setStatus ( {
138+ code : SpanStatusCode . ERROR ,
139+ message : errMessage ,
140+ } ) ;
141+ }
142+
143+ span . end ( ) ;
144+
145+ const flushers = [ ] ;
146+ if ( ( this as any ) . _traceForceFlusher ) {
147+ flushers . push ( ( this as any ) . _traceForceFlusher ( ) ) ;
148+ } else {
149+ diag . error (
150+ 'Spans may not be exported for the lambda function because we are not force flushing before callback.'
151+ ) ;
152+ }
153+
154+ Promise . all ( flushers ) . then ( callback , callback ) ;
155+ }
120156}
0 commit comments