11import { Context } from '@opentelemetry/api' ;
22import { Span , ReadableSpan , SpanProcessor } from '@opentelemetry/sdk-trace-base' ;
3+ import { globalErrorHandler } from '@opentelemetry/core' ;
34
45/**
56 * SpanProcessor for special operations
@@ -43,6 +44,7 @@ export class CustomSpanProcessor implements SpanProcessor {
4344
4445/**
4546 * Storage to allow the use of multiple SpanProcessors
47+ * Basically, exports the MultiSpanProcessor from @opentelemetry/sdk-trace-base
4648 */
4749export class MultiSpanProcessor implements SpanProcessor {
4850
@@ -52,14 +54,22 @@ export class MultiSpanProcessor implements SpanProcessor {
5254 }
5355
5456 forceFlush ( ) : Promise < void > {
55- return Promise . all (
56- this . spanProcessors . map ( ( processor ) => {
57- if ( processor . forceFlush ) {
58- return processor . forceFlush ( ) ;
59- }
60- return Promise . resolve ( ) ;
61- } )
62- ) . then ( ( ) => { } ) ;
57+ const promises : Promise < void > [ ] = [ ] ;
58+ for ( const spanProcessor of this . spanProcessors ) {
59+ promises . push ( spanProcessor . forceFlush ( ) ) ;
60+ }
61+ return new Promise < void > ( ( resolve ) => {
62+ Promise . all ( promises )
63+ . then ( ( ) => {
64+ resolve ( ) ;
65+ } )
66+ . catch ( ( error ) => {
67+ globalErrorHandler (
68+ error || new Error ( 'MultiSpanProcessor: forceFlush failed' )
69+ ) ;
70+ resolve ( ) ;
71+ } ) ;
72+ } ) ;
6373 }
6474
6575 onEnd ( span : ReadableSpan ) : void {
@@ -75,9 +85,19 @@ export class MultiSpanProcessor implements SpanProcessor {
7585 }
7686
7787 shutdown ( ) : Promise < void > {
78- return Promise . all (
79- this . spanProcessors . map ( ( processor ) => processor . shutdown ( ) )
80- ) . then ( ( ) => { } ) ;
88+ const promises : Promise < void > [ ] = [ ] ;
89+ for ( const spanProcessor of this . spanProcessors ) {
90+ promises . push ( spanProcessor . shutdown ( ) ) ;
91+ }
92+ return new Promise < void > ( ( resolve , reject ) => {
93+ Promise . all ( promises )
94+ . then ( ( ) => {
95+ resolve ( ) ;
96+ } )
97+ . catch ( ( error ) => {
98+ reject ( error ) ;
99+ } ) ;
100+ } ) ;
81101 }
82102}
83103
0 commit comments