Skip to content

Commit 97e79d9

Browse files
committed
test(node-core): Proof that withMonitor doesn't create a new trace
1 parent 98de756 commit 97e79d9

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

packages/node-core/test/integration/transactions.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { context, trace, TraceFlags } from '@opentelemetry/api';
2-
import type { TransactionEvent } from '@sentry/core';
2+
import type { ErrorEvent, TransactionEvent } from '@sentry/core';
33
import { debug, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
44
import { afterEach, describe, expect, it, vi } from 'vitest';
55
import * as Sentry from '../../src';
@@ -674,4 +674,65 @@ describe('Integration | Transactions', () => {
674674
expect(spans).toContainEqual(expect.objectContaining({ description: 'inner span 1' }));
675675
expect(spans).toContainEqual(expect.objectContaining({ description: 'inner span 2' }));
676676
});
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+
});
677738
});

0 commit comments

Comments
 (0)