Skip to content

Commit

Permalink
perf(eport): do not allocate arrays if resource has no pending async …
Browse files Browse the repository at this point in the history
…attributes
  • Loading branch information
Samuron authored and Ievgen Makukh committed Mar 17, 2024
1 parent 3a426e8 commit c5b52e2
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,24 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig>
);
}
});
const pendingResources = spans
.map(span => span.resource)
.filter(resource => resource.asyncAttributesPending);

let pendingResources: Array<Promise<void>> | 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);
});
Expand Down

0 comments on commit c5b52e2

Please sign in to comment.