Skip to content

[@sentry/tracing][express] Span is lost for last middleware #3001

Description

@xr0master

issue

It happens because of event finish order.
In handler:
https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/handlers.ts#L65
In express
https://github.com/getsentry/sentry-javascript/blob/master/packages/tracing/src/integrations/express.ts#L121

The handlers listener fires before express, as result the last spin has been filtered in:
https://github.com/getsentry/sentry-javascript/blob/master/packages/tracing/src/transaction.ts#L90
because the last span still doesn't have the endTimestamp

2 options to fix this issue:

  1. use close listener instead of finish in handlers.ts. The close event runs after finish.
res.once('close',()=>{transaction.setHttpStatus(res.statusCode);transaction.finish();});
  1. finish all spans if transaction.finish(); runs
constfinishedSpans=this.spanRecorder ? this.spanRecorder.spans.reduce((spans,s)=>{!s.endTimestamp&&s.finish();s!==this&&spans.push(s);returnspans;},[]) : [];

Let me know which option is best for you.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions