Skip to content

Commit 741ab49

Browse files
fix(tracing): use globalErrorHandler when flushing fails
1 parent dc8082a commit 741ab49

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
*/
1616

1717
import { context, suppressInstrumentation } from '@opentelemetry/api';
18-
import { ExportResult, unrefTimer } from '@opentelemetry/core';
18+
import {
19+
ExportResult,
20+
globalErrorHandler,
21+
unrefTimer,
22+
} from '@opentelemetry/core';
1923
import { SpanProcessor } from '../SpanProcessor';
2024
import { BufferConfig } from '../types';
2125
import { ReadableSpan } from './ReadableSpan';
@@ -104,11 +108,12 @@ export class BatchSpanProcessor implements SpanProcessor {
104108
context.with(suppressInstrumentation(context.active()), () => {
105109
this._exporter.export(this._finishedSpans, result => {
106110
this._finishedSpans = [];
107-
if (result === ExportResult.SUCCESS) {
108-
resolve();
109-
} else {
110-
reject(result);
111+
if (result !== ExportResult.SUCCESS) {
112+
globalErrorHandler(
113+
new Error('BatchSpanProcessor: export failed in _flush')
114+
);
111115
}
116+
resolve();
112117
});
113118
});
114119
});

packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { AlwaysOnSampler, ExportResult } from '@opentelemetry/core';
17+
import {
18+
AlwaysOnSampler,
19+
ExportResult,
20+
loggingErrorHandler,
21+
setGlobalErrorHandler,
22+
} from '@opentelemetry/core';
1823
import * as assert from 'assert';
1924
import * as sinon from 'sinon';
2025
import {
@@ -228,6 +233,34 @@ describe('BatchSpanProcessor', () => {
228233
done();
229234
});
230235
});
236+
237+
it('should call globalErrorHandler when exporting fails', async () => {
238+
const expectedError = new Error(
239+
'BatchSpanProcessor: export failed in _flush'
240+
);
241+
let exportedSpans = 0;
242+
sinon.stub(exporter, 'export').callsFake((spans, callback) => {
243+
setTimeout(() => {
244+
exportedSpans = exportedSpans + spans.length;
245+
callback(ExportResult.FAILED_NOT_RETRYABLE);
246+
}, 0);
247+
});
248+
249+
const errorHandlerSpy = sinon.spy();
250+
251+
setGlobalErrorHandler(errorHandlerSpy);
252+
253+
await processor.forceFlush();
254+
255+
assert.strictEqual(errorHandlerSpy.callCount, 1);
256+
257+
const [[error]] = errorHandlerSpy.args;
258+
259+
assert.deepStrictEqual(error, expectedError);
260+
261+
//reset global error handler
262+
setGlobalErrorHandler(loggingErrorHandler());
263+
});
231264
});
232265

233266
describe('flushing spans with exporter triggering instrumentation', () => {

0 commit comments

Comments
 (0)