Skip to content

Commit 79ac167

Browse files
committed
add tests for positive case
1 parent 67c4095 commit 79ac167

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

dev-packages/opentelemetry-v2-tests/test/integration/transactions.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,58 @@ describe('Integration | Transactions', () => {
548548
expect(finishedSpans.length).toBe(0);
549549
});
550550

551+
it('collects child spans that are finished within 5 minutes their parent span has been sent', async () => {
552+
const timeout = 5 * 60 * 1000;
553+
const now = Date.now();
554+
vi.useFakeTimers();
555+
vi.setSystemTime(now);
556+
557+
const logs: unknown[] = [];
558+
vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));
559+
560+
const transactions: Event[] = [];
561+
562+
mockSdkInit({
563+
tracesSampleRate: 1,
564+
beforeSendTransaction: event => {
565+
transactions.push(event);
566+
return null;
567+
},
568+
});
569+
570+
const provider = getProvider();
571+
const spanProcessor = getSpanProcessor();
572+
573+
const exporter = spanProcessor ? spanProcessor['_exporter'] : undefined;
574+
575+
if (!exporter) {
576+
throw new Error('No exporter found, aborting test...');
577+
}
578+
579+
startSpanManual({ name: 'test name' }, async span => {
580+
const subSpan = startInactiveSpan({ name: 'inner span 1' });
581+
subSpan.end();
582+
583+
const subSpan2 = startInactiveSpan({ name: 'inner span 2' });
584+
585+
span.end();
586+
587+
setTimeout(() => {
588+
subSpan2.end();
589+
}, timeout - 2);
590+
});
591+
592+
vi.advanceTimersByTime(timeout - 1);
593+
594+
expect(transactions).toHaveLength(2);
595+
expect(transactions[0]?.spans).toHaveLength(1);
596+
597+
const finishedSpans: any = exporter['_finishedSpanBuckets'].flatMap(bucket =>
598+
bucket ? Array.from(bucket.spans) : [],
599+
);
600+
expect(finishedSpans.length).toBe(0);
601+
});
602+
551603
it('discards child spans that are finished after 5 minutes their parent span has been sent', async () => {
552604
const timeout = 5 * 60 * 1000;
553605
const now = Date.now();

packages/opentelemetry/test/integration/transactions.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,63 @@ describe('Integration | Transactions', () => {
561561
expect(finishedSpans.length).toBe(0);
562562
});
563563

564+
it('collects child spans that are finished within 5 minutes their parent span has been sent', async () => {
565+
const timeout = 5 * 60 * 1000;
566+
const now = Date.now();
567+
vi.useFakeTimers();
568+
vi.setSystemTime(now);
569+
570+
const logs: unknown[] = [];
571+
vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));
572+
573+
const transactions: Event[] = [];
574+
575+
mockSdkInit({
576+
tracesSampleRate: 1,
577+
beforeSendTransaction: event => {
578+
transactions.push(event);
579+
return null;
580+
},
581+
});
582+
583+
const provider = getProvider();
584+
const multiSpanProcessor = provider?.activeSpanProcessor as
585+
| (SpanProcessor & { _spanProcessors?: SpanProcessor[] })
586+
| undefined;
587+
const spanProcessor = multiSpanProcessor?.['_spanProcessors']?.find(
588+
spanProcessor => spanProcessor instanceof SentrySpanProcessor,
589+
) as SentrySpanProcessor | undefined;
590+
591+
const exporter = spanProcessor ? spanProcessor['_exporter'] : undefined;
592+
593+
if (!exporter) {
594+
throw new Error('No exporter found, aborting test...');
595+
}
596+
597+
startSpanManual({ name: 'test name' }, async span => {
598+
const subSpan = startInactiveSpan({ name: 'inner span 1' });
599+
subSpan.end();
600+
601+
const subSpan2 = startInactiveSpan({ name: 'inner span 2' });
602+
603+
span.end();
604+
605+
setTimeout(() => {
606+
subSpan2.end();
607+
}, timeout - 2);
608+
});
609+
610+
vi.advanceTimersByTime(timeout - 1);
611+
612+
expect(transactions).toHaveLength(2);
613+
expect(transactions[0]?.spans).toHaveLength(1);
614+
615+
const finishedSpans: any = exporter['_finishedSpanBuckets'].flatMap(bucket =>
616+
bucket ? Array.from(bucket.spans) : [],
617+
);
618+
expect(finishedSpans.length).toBe(0);
619+
});
620+
564621
it('discards child spans that are finished after 5 minutes their parent span has been sent', async () => {
565622
const timeout = 5 * 60 * 1000;
566623
const now = Date.now();

0 commit comments

Comments
 (0)