Skip to content

Commit

Permalink
fix(instrumentation-express): Finish spans after execution
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Dec 13, 2024
1 parent 91c9089 commit 6751ab9
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,6 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
}

let spanHasEnded = false;
if (
metadata.attributes[AttributeNames.EXPRESS_TYPE] !==
ExpressLayerType.MIDDLEWARE
) {
span.end();
spanHasEnded = true;
}
// listener for response.on('finish')
const onResponseFinish = () => {
if (spanHasEnded === false) {
Expand Down Expand Up @@ -269,16 +262,22 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
});
}

if (spanHasEnded === false) {
spanHasEnded = true;
req.res?.removeListener('finish', onResponseFinish);
span.end();
}
if (!(req.route && isError)) {
(req[_LAYERS_STORE_PROPERTY] as string[]).pop();
}
const callback = args[callbackIdx] as Function;
return callback.apply(this, arguments);

try {
return callback.apply(this, arguments);
} catch (anyError) {
throw anyError;
} finally {
if (!spanHasEnded) {
spanHasEnded = true;
req.res?.removeListener('finish', onResponseFinish);
span.end();
}
}
};
}

Expand Down
Loading

0 comments on commit 6751ab9

Please sign in to comment.