Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix awaiting for a flush #20

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
event: any,
context: Context,
callback: Callback
) {
): void {

self._before_execution(event, context).then(
(instrCtx) => {
Expand All @@ -140,25 +140,26 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
} catch (err: any) {
// Catching synchronous failures
diag.debug('handler threw synchronously');
void self._after_execution(instrCtx, err, undefined);
context.callbackWaitsForEmptyEventLoop = false;
callback(err, undefined);
self._after_execution(instrCtx, err, undefined).then(() => {
context.callbackWaitsForEmptyEventLoop = false;
callback(err, undefined);
});
return;
}

if (typeof maybePromise?.then === 'function') {
diag.debug('handler returned a promise');
// Promise based async handler
maybePromise.then(
(value: any) => {
async (value: any) => {
diag.debug('handler promise completed');
self._after_execution(instrCtx, undefined, value);
await self._after_execution(instrCtx, undefined, value);
context.callbackWaitsForEmptyEventLoop = false;
callback(undefined, value);
},
(err: Error | string) => {
async (err: Error | string) => {
diag.debug('handler promise failed');
self._after_execution(instrCtx, err, undefined);
await self._after_execution(instrCtx, err, undefined);
context.callbackWaitsForEmptyEventLoop = false;
callback(err, undefined);
}
Expand All @@ -169,13 +170,14 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
}
);
},
(err) => {
async (err) => {
diag.error('_before_execution failed', err);
self._after_execution(undefined, err, undefined);
await self._after_execution(undefined, err, undefined);
context.callbackWaitsForEmptyEventLoop = false;
callback(err, undefined);
}
)

}
}

Expand All @@ -197,8 +199,6 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {

const invocationSpan = this._startInvocationSpan(event, context, invocationParentContext);

diag.info(`upstream: ${trace.getSpan(upstreamContext)?.spanContext().spanId} trigger: ${triggerSpan?.spanContext().spanId} invocationSpan: ${invocationSpan.spanContext().spanId}`)

await this._sendEarlySpans(upstreamContext, triggerSpan, invocationParentContext, invocationSpan);

return {triggerOrigin, triggerSpan, invocationSpan, invocationParentContext}
Expand Down
Loading