Skip to content

Commit 8e2d7d2

Browse files
authored
perf(otel): Only calculate current timestamp once (#14094)
resolves #14067 Avoid calling `Date.now()` for each span in the span exporter. This should reduce blocking I/O.
1 parent e72d31d commit 8e2d7d2

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

packages/opentelemetry/src/spanExporter.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ export class SentrySpanExporter {
105105
* We do this to avoid leaking memory.
106106
*/
107107
private _cleanupOldSpans(spans = this._finishedSpans): void {
108+
const currentTimeSeconds = Date.now() / 1000;
108109
this._finishedSpans = spans.filter(span => {
109-
const shouldDrop = shouldCleanupSpan(span, this._timeout);
110+
const shouldDrop = shouldCleanupSpan(span, currentTimeSeconds, this._timeout);
110111
DEBUG_BUILD &&
111112
shouldDrop &&
112113
logger.log(
@@ -174,8 +175,8 @@ function getCompletedRootNodes(nodes: SpanNode[]): SpanNodeCompleted[] {
174175
return nodes.filter(nodeIsCompletedRootNode);
175176
}
176177

177-
function shouldCleanupSpan(span: ReadableSpan, maxStartTimeOffsetSeconds: number): boolean {
178-
const cutoff = Date.now() / 1000 - maxStartTimeOffsetSeconds;
178+
function shouldCleanupSpan(span: ReadableSpan, currentTimeSeconds: number, maxStartTimeOffsetSeconds: number): boolean {
179+
const cutoff = currentTimeSeconds - maxStartTimeOffsetSeconds;
179180
return spanTimeInputToSeconds(span.startTime) < cutoff;
180181
}
181182

0 commit comments

Comments
 (0)