Skip to content

Commit

Permalink
fix(plugin-express): fix double span end open-telemetry#908
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Apr 5, 2020
1 parent 4978047 commit bd7bbdd
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/opentelemetry-plugin-express/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,21 @@ export class ExpressPlugin extends BasePlugin<typeof express> {
* - however it will not represent the actual time that the span took
* - we would need to hook into the http response's `end` event.
*
* The fix for case 4) is to hook into the `end` event, but it would apply
* to case 2) and 3), which would cost quite a lot of memory for big express apps.
*
* The workaround is to schedule a callback to run on next tick and check if the
* callback has been called. If not we close the span because we assume being
* in the case 2) or 4).
* The workaround is to schedule a callback to run on next tick which
* would ensure that the layer was async, and then add a listener on the
* 'end' event to end the span.
*/
const result = original.apply(this, arguments);
setImmediate(() => {
if (callbackHasBeenCalled === false) {
span.end();
res.once('end', function() {
// its possible that the layer was async but called its callback
// between the setImmediate and the response was sent to the client
if (callbackHasBeenCalled === true) return;
span.end();
});
}
});
}).unref();
return result;
};
});
Expand Down

0 comments on commit bd7bbdd

Please sign in to comment.