From ea73329e584f0052ad88cd2badb0b30045efcb83 Mon Sep 17 00:00:00 2001 From: Eduard Schander <66794307+EddeCCC@users.noreply.github.com> Date: Mon, 24 Jul 2023 10:34:53 +0200 Subject: [PATCH] update MultiSpanProcessor (#42) * update MultiSpanProcessor * add semicolon --- src/impl/spanProcessing.ts | 42 ++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/impl/spanProcessing.ts b/src/impl/spanProcessing.ts index 729bbc1..de646a9 100644 --- a/src/impl/spanProcessing.ts +++ b/src/impl/spanProcessing.ts @@ -1,5 +1,6 @@ import { Context } from '@opentelemetry/api'; import { Span, ReadableSpan, SpanProcessor } from '@opentelemetry/sdk-trace-base'; +import { globalErrorHandler } from '@opentelemetry/core'; /** * SpanProcessor for special operations @@ -43,6 +44,7 @@ export class CustomSpanProcessor implements SpanProcessor { /** * Storage to allow the use of multiple SpanProcessors + * Basically, exports the MultiSpanProcessor from @opentelemetry/sdk-trace-base */ export class MultiSpanProcessor implements SpanProcessor { @@ -52,14 +54,22 @@ export class MultiSpanProcessor implements SpanProcessor { } forceFlush(): Promise { - return Promise.all( - this.spanProcessors.map((processor) => { - if (processor.forceFlush) { - return processor.forceFlush(); - } - return Promise.resolve(); - }) - ).then(() => {}); + const promises: Promise[] = []; + for (const spanProcessor of this.spanProcessors) { + promises.push(spanProcessor.forceFlush()); + } + return new Promise((resolve) => { + Promise.all(promises) + .then(() => { + resolve(); + }) + .catch((error) => { + globalErrorHandler( + error || new Error('MultiSpanProcessor: forceFlush failed') + ); + resolve(); + }); + }); } onEnd(span: ReadableSpan): void { @@ -75,9 +85,19 @@ export class MultiSpanProcessor implements SpanProcessor { } shutdown(): Promise { - return Promise.all( - this.spanProcessors.map((processor) => processor.shutdown()) - ).then(() => {}); + const promises: Promise[] = []; + for (const spanProcessor of this.spanProcessors) { + promises.push(spanProcessor.shutdown()); + } + return new Promise((resolve, reject) => { + Promise.all(promises) + .then(() => { + resolve(); + }) + .catch((error) => { + reject(error); + }); + }); } }