Skip to content

Commit

Permalink
fix: clear BatchSpanProcessor internal spans buffer before exporting (#…
Browse files Browse the repository at this point in the history
…1666)

Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
TsvetanMilanov and dyladan authored Nov 10, 2020
1 parent 97736be commit 25a2f65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ export class BatchSpanProcessor implements SpanProcessor {
return new Promise((resolve, reject) => {
// prevent downstream exporter calls from generating spans
context.with(suppressInstrumentation(context.active()), () => {
this._exporter.export(this._finishedSpans, result => {
this._finishedSpans = [];
// 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 of the execution of this callback.
this._exporter.export(this._finishedSpans.splice(0), result => {
if (result.code === ExportResultCode.SUCCESS) {
resolve();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ describe('BatchSpanProcessor', () => {

clock.restore();
});

it('should export each sampled span exactly once with buffer size reached multiple times', async () => {
const processor = new BatchSpanProcessor(exporter, defaultBufferConfig);
const totalSpans = defaultBufferConfig.bufferSize * 2;
for (let i = 0; i <= totalSpans; i++) {
const span = createSampledSpan(`${name}_${i}`);

processor.onEnd(span);
}
// Now we should start seeing the spans in exporter
const span = createSampledSpan(`${name}_last`);
processor.onEnd(span);
assert.strictEqual(exporter.getFinishedSpans().length, totalSpans + 2);

await processor.shutdown();
assert.strictEqual(exporter.getFinishedSpans().length, 0);
});
});

describe('force flush', () => {
Expand Down

0 comments on commit 25a2f65

Please sign in to comment.