Skip to content

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

Closed
@xr0master

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
const finishedSpans = this.spanRecorder ? this.spanRecorder.spans
    .reduce((spans, s) => {
        !s.endTimestamp && s.finish();
        s !== this && spans.push(s);

        return spans;
    }, []) : [];

Let me know which option is best for you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions