Skip to content

Commit 7533d43

Browse files
committed
Remove Lambda Metric ForceFlush error msg
1 parent 8cb2245 commit 7533d43

File tree

1 file changed

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

1 file changed

+38
-1
lines changed

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

Lines changed: 38 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,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

Comments
 (0)