|
1 | 1 | import { context, trace, TraceFlags } from '@opentelemetry/api'; |
2 | | -import type { TransactionEvent } from '@sentry/core'; |
| 2 | +import type { ErrorEvent, TransactionEvent } from '@sentry/core'; |
3 | 3 | import { debug, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; |
4 | 4 | import { afterEach, describe, expect, it, vi } from 'vitest'; |
5 | 5 | import * as Sentry from '../../src'; |
@@ -674,4 +674,65 @@ describe('Integration | Transactions', () => { |
674 | 674 | expect(spans).toContainEqual(expect.objectContaining({ description: 'inner span 1' })); |
675 | 675 | expect(spans).toContainEqual(expect.objectContaining({ description: 'inner span 2' })); |
676 | 676 | }); |
| 677 | + |
| 678 | + it('withMonitor should use the same traces for each monitor', async () => { |
| 679 | + const sendEvents: ErrorEvent[] = []; |
| 680 | + const transactionEvents: TransactionEvent[] = []; |
| 681 | + const beforeSendTransaction = vi.fn((event: TransactionEvent) => { |
| 682 | + transactionEvents.push(event); |
| 683 | + return null; |
| 684 | + }); |
| 685 | + const beforeSend = vi.fn((event: ErrorEvent) => { |
| 686 | + sendEvents.push(event); |
| 687 | + return null; |
| 688 | + }); |
| 689 | + |
| 690 | + mockSdkInit({ |
| 691 | + tracesSampleRate: 1, |
| 692 | + beforeSendTransaction, |
| 693 | + beforeSend, |
| 694 | + debug: true, |
| 695 | + }); |
| 696 | + |
| 697 | + const client = Sentry.getClient(); |
| 698 | + const errorMessage = 'Error outside withMonitor'; |
| 699 | + |
| 700 | + Sentry.startSpan({ name: 'span outside error' }, () => { |
| 701 | + Sentry.withMonitor('cron-job-1', () => Sentry.startSpan({ name: 'inner span 1' }, () => undefined)); |
| 702 | + |
| 703 | + try { |
| 704 | + throw new Error(errorMessage); |
| 705 | + } catch (e) { |
| 706 | + Sentry.startSpan({ name: 'span inside error' }, () => undefined); |
| 707 | + Sentry.captureException(e); |
| 708 | + } |
| 709 | + |
| 710 | + Sentry.withMonitor('cron-job-2', () => { |
| 711 | + Sentry.startSpan({ name: 'inner span 2' }, () => undefined); |
| 712 | + }); |
| 713 | + }); |
| 714 | + |
| 715 | + await client?.flush(); |
| 716 | + |
| 717 | + const transactionTraceId = transactionEvents[0]?.contexts?.trace?.trace_id; |
| 718 | + const errorTraceId = sendEvents[0]?.contexts?.trace?.trace_id; |
| 719 | + |
| 720 | + expect(beforeSendTransaction).toHaveBeenCalledTimes(1); |
| 721 | + expect(beforeSend).toHaveBeenCalledTimes(1); |
| 722 | + expect(transactionEvents).toHaveLength(1); |
| 723 | + expect(transactionTraceId).toBe(errorTraceId); |
| 724 | + expect(transactionEvents[0]?.spans).toHaveLength(3); |
| 725 | + expect(transactionEvents).toMatchObject([ |
| 726 | + { |
| 727 | + spans: [{ description: 'inner span 1' }, { description: 'span inside error' }, { description: 'inner span 2' }], |
| 728 | + }, |
| 729 | + ]); |
| 730 | + expect(sendEvents).toMatchObject([ |
| 731 | + { |
| 732 | + exception: { |
| 733 | + values: [{ value: errorMessage }], |
| 734 | + }, |
| 735 | + }, |
| 736 | + ]); |
| 737 | + }); |
677 | 738 | }); |
0 commit comments