|
| 1 | +import { eventContextExtractor, getAwsTraceData } from '../src/utils'; |
| 2 | + |
| 3 | +const mockExtractContext = jest.fn(); |
| 4 | +jest.mock('@opentelemetry/api', () => { |
| 5 | + const actualApi = jest.requireActual('@opentelemetry/api'); |
| 6 | + return { |
| 7 | + ...actualApi, |
| 8 | + propagation: { |
| 9 | + extract: (...args: unknown[]) => mockExtractContext(args), |
| 10 | + }, |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +const mockContext = { |
| 15 | + clientContext: { |
| 16 | + Custom: { |
| 17 | + 'sentry-trace': '12345678901234567890123456789012-1234567890123456-1', |
| 18 | + baggage: 'sentry-environment=production', |
| 19 | + }, |
| 20 | + }, |
| 21 | +}; |
| 22 | +const mockEvent = { |
| 23 | + headers: { |
| 24 | + 'sentry-trace': '12345678901234567890123456789012-1234567890123456-2', |
| 25 | + baggage: 'sentry-environment=staging', |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +describe('getTraceData', () => { |
| 30 | + test('gets sentry trace data from the context', () => { |
| 31 | + // @ts-expect-error, a partial context object is fine here |
| 32 | + const traceData = getAwsTraceData({}, mockContext); |
| 33 | + |
| 34 | + expect(traceData['sentry-trace']).toEqual('12345678901234567890123456789012-1234567890123456-1'); |
| 35 | + expect(traceData.baggage).toEqual('sentry-environment=production'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('gets sentry trace data from the context even if event has data', () => { |
| 39 | + // @ts-expect-error, a partial context object is fine here |
| 40 | + const traceData = getAwsTraceData(mockEvent, mockContext); |
| 41 | + |
| 42 | + expect(traceData['sentry-trace']).toEqual('12345678901234567890123456789012-1234567890123456-1'); |
| 43 | + expect(traceData.baggage).toEqual('sentry-environment=production'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('gets sentry trace data from the event if no context is passed', () => { |
| 47 | + const traceData = getAwsTraceData(mockEvent); |
| 48 | + |
| 49 | + expect(traceData['sentry-trace']).toEqual('12345678901234567890123456789012-1234567890123456-2'); |
| 50 | + expect(traceData.baggage).toEqual('sentry-environment=staging'); |
| 51 | + }); |
| 52 | + |
| 53 | + test('gets sentry trace data from the event if the context sentry trace is undefined', () => { |
| 54 | + const traceData = getAwsTraceData(mockEvent, { |
| 55 | + // @ts-expect-error, a partial context object is fine here |
| 56 | + clientContext: { Custom: { 'sentry-trace': undefined, baggage: '' } }, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(traceData['sentry-trace']).toEqual('12345678901234567890123456789012-1234567890123456-2'); |
| 60 | + expect(traceData.baggage).toEqual('sentry-environment=staging'); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('eventContextExtractor', () => { |
| 65 | + afterEach(() => { |
| 66 | + jest.clearAllMocks(); |
| 67 | + }); |
| 68 | + |
| 69 | + test('passes sentry trace data to the propagation extractor', () => { |
| 70 | + // @ts-expect-error, a partial context object is fine here |
| 71 | + eventContextExtractor(mockEvent, mockContext); |
| 72 | + |
| 73 | + // @ts-expect-error, a partial context object is fine here |
| 74 | + const expectedTraceData = getAwsTraceData(mockEvent, mockContext); |
| 75 | + |
| 76 | + expect(mockExtractContext).toHaveBeenCalledTimes(1); |
| 77 | + expect(mockExtractContext).toHaveBeenCalledWith(expect.arrayContaining([expectedTraceData])); |
| 78 | + }); |
| 79 | + |
| 80 | + test('passes along non-sentry trace headers along', () => { |
| 81 | + eventContextExtractor( |
| 82 | + { |
| 83 | + ...mockEvent, |
| 84 | + headers: { |
| 85 | + ...mockEvent.headers, |
| 86 | + 'X-Custom-Header': 'Foo', |
| 87 | + }, |
| 88 | + }, |
| 89 | + // @ts-expect-error, a partial context object is fine here |
| 90 | + mockContext, |
| 91 | + ); |
| 92 | + |
| 93 | + const expectedHeaders = { |
| 94 | + 'X-Custom-Header': 'Foo', |
| 95 | + // @ts-expect-error, a partial context object is fine here |
| 96 | + ...getAwsTraceData(mockEvent, mockContext), |
| 97 | + }; |
| 98 | + |
| 99 | + expect(mockExtractContext).toHaveBeenCalledTimes(1); |
| 100 | + expect(mockExtractContext).toHaveBeenCalledWith(expect.arrayContaining([expectedHeaders])); |
| 101 | + }); |
| 102 | +}); |
0 commit comments