From c5b52e270bac95f74f970283ecc875070b49c71c Mon Sep 17 00:00:00 2001 From: Samuron Date: Sun, 17 Mar 2024 08:14:33 +0200 Subject: [PATCH 1/3] 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); }); From a4f45c723f5ed7b18e1c7e21c52e08a7e0b9701e Mon Sep 17 00:00:00 2001 From: Samuron Date: Sun, 17 Mar 2024 13:58:59 +0200 Subject: [PATCH 2/3] perf(export): do not use splice if fit in one batch --- CHANGELOG.md | 2 ++ .../src/export/BatchSpanProcessorBase.ts | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2980539eb..9f9162e86a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :rocket: (Enhancement) +perf(sdk-trace-base): do not allocate arrays if resource has no pending async attributes + ### :bug: (Bug Fix) * fix(sdk-metrics): increase the depth of the output to the console such that objects in the metric are printed fully to the console [#4522](https://github.com/open-telemetry/opentelemetry-js/pull/4522) @JacksonWeber diff --git a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts index 776b7bea21..19e616dc08 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts @@ -181,7 +181,13 @@ export abstract class BatchSpanProcessorBase // Reset the finished spans buffer here because the next invocations of the _flush method // could pass the same finished spans to the exporter if the buffer is cleared // outside the execution of this callback. - const spans = this._finishedSpans.splice(0, this._maxExportBatchSize); + let spans: ReadableSpan[]; + if (this._finishedSpans.length <= this._maxExportBatchSize) { + spans = this._finishedSpans; + this._finishedSpans = []; + } else { + spans = this._finishedSpans.splice(0, this._maxExportBatchSize); + } const doExport = () => this._exporter.export(spans, result => { From fee30fde6bc473a2efd6ff79ca77179159224c72 Mon Sep 17 00:00:00 2001 From: Ievgen Makukh Date: Fri, 22 Mar 2024 17:48:13 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md Co-authored-by: Marc Pichler --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f9162e86a..3c68dec71a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :rocket: (Enhancement) -perf(sdk-trace-base): do not allocate arrays if resource has no pending async attributes +* perf(sdk-trace-base): do not allocate arrays if resource has no pending async attributes ### :bug: (Bug Fix)