|
| 1 | +import type { Span as OtelSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'; |
| 2 | +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; |
| 3 | +import type { Hub } from '@sentry/types'; |
| 4 | + |
| 5 | +import { maybeCaptureExceptionForTimedEvent } from '../../src/utils/captureExceptionForTimedEvent'; |
| 6 | + |
| 7 | +describe('maybeCaptureExceptionForTimedEvent', () => { |
| 8 | + it('ignores non-exception events', async () => { |
| 9 | + const event: TimedEvent = { |
| 10 | + time: [12345, 0], |
| 11 | + name: 'test event', |
| 12 | + }; |
| 13 | + |
| 14 | + const captureException = jest.fn(); |
| 15 | + const hub = { |
| 16 | + captureException, |
| 17 | + } as unknown as Hub; |
| 18 | + |
| 19 | + maybeCaptureExceptionForTimedEvent(hub, event); |
| 20 | + |
| 21 | + expect(captureException).not.toHaveBeenCalled(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('ignores exception events without EXCEPTION_MESSAGE', async () => { |
| 25 | + const event: TimedEvent = { |
| 26 | + time: [12345, 0], |
| 27 | + name: 'exception', |
| 28 | + }; |
| 29 | + |
| 30 | + const captureException = jest.fn(); |
| 31 | + const hub = { |
| 32 | + captureException, |
| 33 | + } as unknown as Hub; |
| 34 | + |
| 35 | + maybeCaptureExceptionForTimedEvent(hub, event); |
| 36 | + |
| 37 | + expect(captureException).not.toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('captures exception from event with EXCEPTION_MESSAGE', async () => { |
| 41 | + const event: TimedEvent = { |
| 42 | + time: [12345, 0], |
| 43 | + name: 'exception', |
| 44 | + attributes: { |
| 45 | + [SemanticAttributes.EXCEPTION_MESSAGE]: 'test-message', |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + const captureException = jest.fn(); |
| 50 | + const hub = { |
| 51 | + captureException, |
| 52 | + } as unknown as Hub; |
| 53 | + |
| 54 | + maybeCaptureExceptionForTimedEvent(hub, event); |
| 55 | + |
| 56 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 57 | + expect(captureException).toHaveBeenCalledWith(expect.objectContaining({ message: 'test-message' }), { |
| 58 | + captureContext: undefined, |
| 59 | + }); |
| 60 | + expect(captureException).toHaveBeenCalledWith(expect.any(Error), { |
| 61 | + captureContext: undefined, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('captures stack and type, if available', async () => { |
| 66 | + const event: TimedEvent = { |
| 67 | + time: [12345, 0], |
| 68 | + name: 'exception', |
| 69 | + attributes: { |
| 70 | + [SemanticAttributes.EXCEPTION_MESSAGE]: 'test-message', |
| 71 | + [SemanticAttributes.EXCEPTION_STACKTRACE]: 'test-stack', |
| 72 | + [SemanticAttributes.EXCEPTION_TYPE]: 'test-type', |
| 73 | + }, |
| 74 | + }; |
| 75 | + |
| 76 | + const captureException = jest.fn(); |
| 77 | + const hub = { |
| 78 | + captureException, |
| 79 | + } as unknown as Hub; |
| 80 | + |
| 81 | + maybeCaptureExceptionForTimedEvent(hub, event); |
| 82 | + |
| 83 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 84 | + expect(captureException).toHaveBeenCalledWith( |
| 85 | + expect.objectContaining({ message: 'test-message', name: 'test-type', stack: 'test-stack' }), |
| 86 | + { |
| 87 | + captureContext: undefined, |
| 88 | + }, |
| 89 | + ); |
| 90 | + expect(captureException).toHaveBeenCalledWith(expect.any(Error), { |
| 91 | + captureContext: undefined, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('captures span context, if available', async () => { |
| 96 | + const event: TimedEvent = { |
| 97 | + time: [12345, 0], |
| 98 | + name: 'exception', |
| 99 | + attributes: { |
| 100 | + [SemanticAttributes.EXCEPTION_MESSAGE]: 'test-message', |
| 101 | + }, |
| 102 | + }; |
| 103 | + |
| 104 | + const span = { |
| 105 | + parentSpanId: 'test-parent-span-id', |
| 106 | + attributes: { |
| 107 | + 'test-attr1': 'test-value1', |
| 108 | + }, |
| 109 | + resource: { |
| 110 | + attributes: { |
| 111 | + 'test-attr2': 'test-value2', |
| 112 | + }, |
| 113 | + }, |
| 114 | + spanContext: () => { |
| 115 | + return { spanId: 'test-span-id', traceId: 'test-trace-id' }; |
| 116 | + }, |
| 117 | + } as unknown as OtelSpan; |
| 118 | + |
| 119 | + const captureException = jest.fn(); |
| 120 | + const hub = { |
| 121 | + captureException, |
| 122 | + } as unknown as Hub; |
| 123 | + |
| 124 | + maybeCaptureExceptionForTimedEvent(hub, event, span); |
| 125 | + |
| 126 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 127 | + expect(captureException).toHaveBeenCalledWith(expect.objectContaining({ message: 'test-message' }), { |
| 128 | + captureContext: { |
| 129 | + contexts: { |
| 130 | + otel: { |
| 131 | + attributes: { |
| 132 | + 'test-attr1': 'test-value1', |
| 133 | + }, |
| 134 | + resource: { |
| 135 | + 'test-attr2': 'test-value2', |
| 136 | + }, |
| 137 | + }, |
| 138 | + trace: { |
| 139 | + trace_id: 'test-trace-id', |
| 140 | + span_id: 'test-span-id', |
| 141 | + parent_span_id: 'test-parent-span-id', |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments