diff --git a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts index 776b7bea212..f8de35743bd 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts @@ -34,8 +34,7 @@ import { SpanExporter } from './SpanExporter'; * the SDK then pushes them to the exporter pipeline. */ export abstract class BatchSpanProcessorBase - implements SpanProcessor -{ + implements SpanProcessor { private readonly _maxExportBatchSize: number; private readonly _maxQueueSize: number; private readonly _scheduledDelayMillis: number; @@ -87,7 +86,7 @@ export abstract class BatchSpanProcessorBase } // does nothing. - onStart(_span: Span, _parentContext: Context): void {} + onStart(_span: Span, _parentContext: Context): void { } onEnd(span: ReadableSpan): void { if (this._shutdownOnce.isCalled) { @@ -181,7 +180,14 @@ export abstract class BatchSpanProcessorBase // Reset the finished spans buffer here because the next invocations of the _flush method // could pass the same finished spans to the exporter if the buffer is cleared // outside the execution of this callback. - const spans = this._finishedSpans.splice(0, this._maxExportBatchSize); + let spans: ReadableSpan[]; + if (this._finishedSpans.length <= this._maxExportBatchSize) { + spans = this._finishedSpans; + this._finishedSpans = []; + } + else { + spans = this._finishedSpans.splice(0, this._maxExportBatchSize); + } const doExport = () => this._exporter.export(spans, result => { @@ -191,7 +197,7 @@ export abstract class BatchSpanProcessorBase } else { reject( result.error ?? - new Error('BatchSpanProcessor: span export failed') + new Error('BatchSpanProcessor: span export failed') ); } });