Skip to content

Commit

Permalink
perf(otel): Only calculate current timestamp once (#14094)
Browse files Browse the repository at this point in the history
resolves #14067

Avoid calling `Date.now()` for each span in the span exporter. This
should reduce blocking I/O.
  • Loading branch information
AbhiPrasad authored Oct 28, 2024
1 parent e72d31d commit 8e2d7d2
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 8e2d7d2

Please sign in to comment.