|
1 | 1 | import { context, trace } from '@opentelemetry/api';
|
2 |
| -import { getCurrentScope, setAsyncContextStrategy } from '@sentry/core'; |
| 2 | +import { getCurrentScope, Scope, setAsyncContextStrategy } from '@sentry/core'; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
4 | 4 | import { getTraceData } from '../../src/utils/getTraceData';
|
5 | 5 | import { makeTraceState } from '../../src/utils/makeTraceState';
|
6 | 6 | import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit';
|
| 7 | +import { getDefaultTestClientOptions, TestClient } from '../helpers/TestClient'; |
7 | 8 |
|
8 | 9 | describe('getTraceData', () => {
|
9 | 10 | beforeEach(() => {
|
@@ -52,6 +53,32 @@ describe('getTraceData', () => {
|
52 | 53 | });
|
53 | 54 | });
|
54 | 55 |
|
| 56 | +it('allows to pass a scope & client directly', () => { |
| 57 | +getCurrentScope().setPropagationContext({ |
| 58 | +traceId: '12345678901234567890123456789099', |
| 59 | +sampleRand: 0.44, |
| 60 | +}); |
| 61 | + |
| 62 | +const customClient = new TestClient( |
| 63 | +getDefaultTestClientOptions({ tracesSampleRate: 1, dsn: 'https://[email protected]/42' }), |
| 64 | +); |
| 65 | + |
| 66 | +// note: Right now, this only works properly if the scope is linked to a context |
| 67 | +const scope = new Scope(); |
| 68 | +scope.setPropagationContext({ |
| 69 | +traceId: '12345678901234567890123456789012', |
| 70 | +sampleRand: 0.42, |
| 71 | +}); |
| 72 | +scope.setClient(customClient); |
| 73 | + |
| 74 | +const traceData = getTraceData({ client: customClient, scope }); |
| 75 | + |
| 76 | +expect(traceData['sentry-trace']).toMatch(/^12345678901234567890123456789012-[a-f0-9]{16}$/); |
| 77 | +expect(traceData.baggage).toEqual( |
| 78 | +'sentry-environment=production,sentry-public_key=123,sentry-trace_id=12345678901234567890123456789012', |
| 79 | +); |
| 80 | +}); |
| 81 | + |
55 | 82 | it('returns propagationContext DSC data if no span is available', () => {
|
56 | 83 | getCurrentScope().setPropagationContext({
|
57 | 84 | traceId: '12345678901234567890123456789012',
|
|
0 commit comments