Skip to content

Commit b6d2752

Browse files
authored
Remove Lambda Metric ForceFlush error msg (#110)
*Description of changes:* Remove the unnecessary Lambda Metric ForceFlush warning message which is not applicable to ADOT Lambda case By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 8cb2245 commit b6d2752

File tree

1 file changed

+37
-1
lines changed
  • aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services

1 file changed

+37
-1
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services/aws-lambda.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
1517
export 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

Comments
 (0)