diff --git a/packages/opentelemetry/src/spanExporter.ts b/packages/opentelemetry/src/spanExporter.ts index d00319ec2c98..5ba28f8b1607 100644 --- a/packages/opentelemetry/src/spanExporter.ts +++ b/packages/opentelemetry/src/spanExporter.ts @@ -105,8 +105,9 @@ export class SentrySpanExporter { * We do this to avoid leaking memory. */ private _cleanupOldSpans(spans = this._finishedSpans): void { + const currentTimeSeconds = Date.now() / 1000; this._finishedSpans = spans.filter(span => { - const shouldDrop = shouldCleanupSpan(span, this._timeout); + const shouldDrop = shouldCleanupSpan(span, currentTimeSeconds, this._timeout); DEBUG_BUILD && shouldDrop && logger.log( @@ -174,8 +175,8 @@ function getCompletedRootNodes(nodes: SpanNode[]): SpanNodeCompleted[] { return nodes.filter(nodeIsCompletedRootNode); } -function shouldCleanupSpan(span: ReadableSpan, maxStartTimeOffsetSeconds: number): boolean { - const cutoff = Date.now() / 1000 - maxStartTimeOffsetSeconds; +function shouldCleanupSpan(span: ReadableSpan, currentTimeSeconds: number, maxStartTimeOffsetSeconds: number): boolean { + const cutoff = currentTimeSeconds - maxStartTimeOffsetSeconds; return spanTimeInputToSeconds(span.startTime) < cutoff; }