From c5b52e270bac95f74f970283ecc875070b49c71c Mon Sep 17 00:00:00 2001 From: Samuron Date: Sun, 17 Mar 2024 08:14:33 +0200 Subject: [PATCH] perf(eport): do not allocate arrays if resource has no pending async attributes --- .../src/export/BatchSpanProcessorBase.ts | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts index f069aac0c7..776b7bea21 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts @@ -195,19 +195,24 @@ export abstract class BatchSpanProcessorBase ); } }); - const pendingResources = spans - .map(span => span.resource) - .filter(resource => resource.asyncAttributesPending); + + let pendingResources: Array> | null = null; + for (let i = 0, len = spans.length; i < len; i++) { + const span = spans[i]; + if ( + span.resource.asyncAttributesPending && + span.resource.waitForAsyncAttributes + ) { + pendingResources ??= []; + pendingResources.push(span.resource.waitForAsyncAttributes()); + } + } // Avoid scheduling a promise to make the behavior more predictable and easier to test - if (pendingResources.length === 0) { + if (pendingResources === null) { doExport(); } else { - Promise.all( - pendingResources.map( - resource => resource.waitForAsyncAttributes?.() - ) - ).then(doExport, err => { + Promise.all(pendingResources).then(doExport, err => { globalErrorHandler(err); reject(err); });