Skip to content

Commit

Permalink
refactor: log error instead of reject in MultiSpanProcessor#forceFlush
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Oct 7, 2020
1 parent 9890c3b commit f9820f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/opentelemetry-tracing/src/MultiSpanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { globalErrorHandler } from '@opentelemetry/core';
import { SpanProcessor } from './SpanProcessor';
import { ReadableSpan } from './export/ReadableSpan';

Expand All @@ -30,10 +31,17 @@ export class MultiSpanProcessor implements SpanProcessor {
for (const spanProcessor of this._spanProcessors) {
promises.push(spanProcessor.forceFlush());
}
return new Promise((resolve, reject) => {
Promise.all(promises).then(() => {
resolve();
}, reject);
return new Promise(resolve => {
Promise.all(promises)
.then(() => {
resolve();
})
.catch(error => {
globalErrorHandler(
error || new Error('MultiSpanProcessor: forceFlush failed')
);
resolve();
});
});
}

Expand Down
27 changes: 27 additions & 0 deletions packages/opentelemetry-tracing/test/MultiSpanProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import {
Span,
SpanProcessor,
} from '../src';
import {
setGlobalErrorHandler,
loggingErrorHandler,
} from '@opentelemetry/core';
import { MultiSpanProcessor } from '../src/MultiSpanProcessor';

class TestProcessor implements SpanProcessor {
Expand Down Expand Up @@ -179,4 +183,27 @@ describe('MultiSpanProcessor', () => {
done();
});
});

it('should call globalErrorHandler in forceFlush', async () => {
const expectedError = new Error('whoops');
const testProcessor = new TestProcessor();
const forceFlush = Sinon.stub(testProcessor, 'forceFlush');
forceFlush.rejects(expectedError);

const multiSpanProcessor = new MultiSpanProcessor([testProcessor]);
const errorHandlerSpy = Sinon.spy();

setGlobalErrorHandler(errorHandlerSpy);

await multiSpanProcessor.forceFlush();

forceFlush.restore();
const [[error]] = errorHandlerSpy.args;

assert.strictEqual(error, expectedError);
assert.strictEqual(errorHandlerSpy.callCount, 1);

//reset global error handler
setGlobalErrorHandler(loggingErrorHandler());
});
});

0 comments on commit f9820f3

Please sign in to comment.