Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear BatchSpanProcessor internal spans buffer before exporting #1666

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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