Skip to content

Reduce blocking main thread during span logic #14067

Closed as not planned
Closed as not planned
@theguacamoleking

Description

@theguacamoleking

Problem Statement

Hi there, I wanted to flag an issue where my deeper GraphQL queries take exponentially longer when using Sentry. This looks to be because Sentry logic ends up blocking the main thread

Image

As you can see above, logic within @sentry/opentelemetry is taking up more than 50% of time on the main thread over the period of over 15s. With Sentry disabled, the same GraphQL request takes about 2 seconds.

Solution Brainstorm

System clock lookups

The following code is the worst offender

function shouldCleanupSpan(span, maxStartTimeOffsetSeconds) {
  const cutoff = Date.now() / 1000 - maxStartTimeOffsetSeconds;
  return core.spanTimeInputToSeconds(span.startTime) < cutoff;
}

Specifically the cutoff assignment. I'd propose something like the following where there is only one system clock read for each call of _cleanupOldSpans.

   _cleanupOldSpans(spans = this._finishedSpans) {
    // Get current time (once)
    const currentEpoch = Math.floor(Date.now() / 1000);

    this._finishedSpans = spans.filter(span => {
      const shouldDrop = core.spanTimeInputToSeconds(span.startTime) < currentEpoch - this._timeout);
      DEBUG_BUILD &&
        shouldDrop &&
        utils.logger.log(
          `SpanExporter dropping span ${span.name} (${
            span.spanContext().spanId
          }) because it is pending for more than 5 minutes.`,
        );
      return !shouldDrop;
    });
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nodeIssues related to the Sentry Node SDKStale

    Projects

    Status

    Waiting for: Community

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions