|
| 1 | +import * as SentryCore from '../../src'; |
| 2 | +import { debug } from '../../src'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { spanStreamingIntegration } from '../../src/integrations/spanStreaming'; |
| 5 | +import { TestClient, getDefaultTestClientOptions } from '../mocks/client'; |
| 6 | + |
| 7 | +const mockSpanBufferInstance = vi.hoisted(() => ({ |
| 8 | + flush: vi.fn(), |
| 9 | + add: vi.fn(), |
| 10 | + drain: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +const MockSpanBuffer = vi.hoisted(() => { |
| 14 | + return vi.fn(() => mockSpanBufferInstance); |
| 15 | +}); |
| 16 | + |
| 17 | +vi.mock('../../src/tracing/spans/spanBuffer', async () => { |
| 18 | + const original = await vi.importActual('../../src/tracing/spans/spanBuffer'); |
| 19 | + return { |
| 20 | + ...original, |
| 21 | + SpanBuffer: MockSpanBuffer, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +describe('spanStreamingIntegration (core)', () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('has the correct name and setup hook', () => { |
| 31 | + const integration = spanStreamingIntegration(); |
| 32 | + expect(integration.name).toBe('SpanStreaming'); |
| 33 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 34 | + expect(integration.setup).toBeDefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it.each(['static', 'somethingElse'])( |
| 38 | + 'logs a warning if traceLifecycle is not set to "stream" but to %s', |
| 39 | + traceLifecycle => { |
| 40 | + const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {}); |
| 41 | + const client = new TestClient({ |
| 42 | + ...getDefaultTestClientOptions(), |
| 43 | + dsn: 'https://username@domain/123', |
| 44 | + integrations: [spanStreamingIntegration()], |
| 45 | + // @ts-expect-error - we want to test the warning for invalid traceLifecycle values |
| 46 | + traceLifecycle, |
| 47 | + }); |
| 48 | + |
| 49 | + SentryCore.setCurrentClient(client); |
| 50 | + client.init(); |
| 51 | + |
| 52 | + expect(debugSpy).toHaveBeenCalledWith( |
| 53 | + 'SpanStreaming integration requires `traceLifecycle` to be set to "stream"! Falling back to static trace lifecycle.', |
| 54 | + ); |
| 55 | + debugSpy.mockRestore(); |
| 56 | + |
| 57 | + expect(client.getOptions().traceLifecycle).toBe('static'); |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + it('falls back to static trace lifecycle if beforeSendSpan is not compatible with span streaming', () => { |
| 62 | + const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {}); |
| 63 | + const client = new TestClient({ |
| 64 | + ...getDefaultTestClientOptions(), |
| 65 | + dsn: 'https://username@domain/123', |
| 66 | + integrations: [spanStreamingIntegration()], |
| 67 | + traceLifecycle: 'stream', |
| 68 | + beforeSendSpan: (span: SentryCore.SpanJSON) => span, |
| 69 | + }); |
| 70 | + |
| 71 | + SentryCore.setCurrentClient(client); |
| 72 | + client.init(); |
| 73 | + |
| 74 | + expect(debugSpy).toHaveBeenCalledWith( |
| 75 | + 'SpanStreaming integration requires a beforeSendSpan callback using `withStreamedSpan`! Falling back to static trace lifecycle.', |
| 76 | + ); |
| 77 | + debugSpy.mockRestore(); |
| 78 | + |
| 79 | + expect(client.getOptions().traceLifecycle).toBe('static'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('sets up buffer when traceLifecycle is "stream"', () => { |
| 83 | + const client = new TestClient({ |
| 84 | + ...getDefaultTestClientOptions(), |
| 85 | + dsn: 'https://username@domain/123', |
| 86 | + integrations: [spanStreamingIntegration()], |
| 87 | + traceLifecycle: 'stream', |
| 88 | + }); |
| 89 | + |
| 90 | + SentryCore.setCurrentClient(client); |
| 91 | + client.init(); |
| 92 | + |
| 93 | + expect(MockSpanBuffer).toHaveBeenCalledWith(client); |
| 94 | + expect(client.getOptions().traceLifecycle).toBe('stream'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('enqueues a span into the buffer when the span ends', () => { |
| 98 | + const client = new TestClient({ |
| 99 | + ...getDefaultTestClientOptions(), |
| 100 | + dsn: 'https://username@domain/123', |
| 101 | + integrations: [spanStreamingIntegration()], |
| 102 | + traceLifecycle: 'stream', |
| 103 | + tracesSampleRate: 1, |
| 104 | + }); |
| 105 | + |
| 106 | + SentryCore.setCurrentClient(client); |
| 107 | + client.init(); |
| 108 | + |
| 109 | + const span = new SentryCore.SentrySpan({ name: 'test', sampled: true }); |
| 110 | + client.emit('afterSpanEnd', span); |
| 111 | + |
| 112 | + expect(mockSpanBufferInstance.add).toHaveBeenCalledWith( |
| 113 | + expect.objectContaining({ |
| 114 | + _segmentSpan: span, |
| 115 | + trace_id: span.spanContext().traceId, |
| 116 | + span_id: span.spanContext().spanId, |
| 117 | + name: 'test', |
| 118 | + }), |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('does not enqueue a span into the buffer when the span is not sampled', () => { |
| 123 | + const client = new TestClient({ |
| 124 | + ...getDefaultTestClientOptions(), |
| 125 | + dsn: 'https://username@domain/123', |
| 126 | + integrations: [spanStreamingIntegration()], |
| 127 | + traceLifecycle: 'stream', |
| 128 | + tracesSampleRate: 1, |
| 129 | + }); |
| 130 | + |
| 131 | + SentryCore.setCurrentClient(client); |
| 132 | + client.init(); |
| 133 | + |
| 134 | + const span = new SentryCore.SentrySpan({ name: 'test', sampled: false }); |
| 135 | + client.emit('afterSpanEnd', span); |
| 136 | + |
| 137 | + expect(mockSpanBufferInstance.add).not.toHaveBeenCalled(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments