|
| 1 | +import { createRunner } from '../../../utils/runner'; |
| 2 | + |
| 3 | +describe('span links', () => { |
| 4 | + test('should link spans with addLink() in trace context', done => { |
| 5 | + let span1_traceId: string, span1_spanId: string; |
| 6 | + |
| 7 | + createRunner(__dirname, 'scenario-addLink.ts') |
| 8 | + .expect({ |
| 9 | + transaction: event => { |
| 10 | + expect(event.transaction).toBe('span1'); |
| 11 | + |
| 12 | + span1_traceId = event.contexts?.trace?.trace_id as string; |
| 13 | + span1_spanId = event.contexts?.trace?.span_id as string; |
| 14 | + |
| 15 | + expect(event.spans).toEqual([]); |
| 16 | + }, |
| 17 | + }) |
| 18 | + .expect({ |
| 19 | + transaction: event => { |
| 20 | + expect(event.transaction).toBe('rootSpan'); |
| 21 | + |
| 22 | + expect(event.contexts?.trace?.links).toEqual([ |
| 23 | + expect.objectContaining({ |
| 24 | + trace_id: expect.stringMatching(span1_traceId), |
| 25 | + span_id: expect.stringMatching(span1_spanId), |
| 26 | + attributes: expect.objectContaining({ |
| 27 | + 'sentry.link.type': 'previous_trace', |
| 28 | + }), |
| 29 | + }), |
| 30 | + ]); |
| 31 | + }, |
| 32 | + }) |
| 33 | + .start(done); |
| 34 | + }); |
| 35 | + |
| 36 | + test('should link spans with addLinks() in trace context', done => { |
| 37 | + let span1_traceId: string, span1_spanId: string, span2_traceId: string, span2_spanId: string; |
| 38 | + |
| 39 | + createRunner(__dirname, 'scenario-addLinks.ts') |
| 40 | + .expect({ |
| 41 | + transaction: event => { |
| 42 | + expect(event.transaction).toBe('span1'); |
| 43 | + |
| 44 | + span1_traceId = event.contexts?.trace?.trace_id as string; |
| 45 | + span1_spanId = event.contexts?.trace?.span_id as string; |
| 46 | + |
| 47 | + expect(event.spans).toEqual([]); |
| 48 | + }, |
| 49 | + }) |
| 50 | + .expect({ |
| 51 | + transaction: event => { |
| 52 | + expect(event.transaction).toBe('span2'); |
| 53 | + |
| 54 | + span2_traceId = event.contexts?.trace?.trace_id as string; |
| 55 | + span2_spanId = event.contexts?.trace?.span_id as string; |
| 56 | + |
| 57 | + expect(event.spans).toEqual([]); |
| 58 | + }, |
| 59 | + }) |
| 60 | + .expect({ |
| 61 | + transaction: event => { |
| 62 | + expect(event.transaction).toBe('rootSpan'); |
| 63 | + |
| 64 | + expect(event.contexts?.trace?.links).toEqual([ |
| 65 | + expect.not.objectContaining({ attributes: expect.anything() }) && |
| 66 | + expect.objectContaining({ |
| 67 | + trace_id: expect.stringMatching(span1_traceId), |
| 68 | + span_id: expect.stringMatching(span1_spanId), |
| 69 | + }), |
| 70 | + expect.objectContaining({ |
| 71 | + trace_id: expect.stringMatching(span2_traceId), |
| 72 | + span_id: expect.stringMatching(span2_spanId), |
| 73 | + attributes: expect.objectContaining({ |
| 74 | + 'sentry.link.type': 'previous_trace', |
| 75 | + }), |
| 76 | + }), |
| 77 | + ]); |
| 78 | + }, |
| 79 | + }) |
| 80 | + .start(done); |
| 81 | + }); |
| 82 | + |
| 83 | + test('should link spans with addLink() in nested startSpan() calls', done => { |
| 84 | + createRunner(__dirname, 'scenario-addLink-nested.ts') |
| 85 | + .expect({ |
| 86 | + transaction: event => { |
| 87 | + expect(event.transaction).toBe('parent1'); |
| 88 | + |
| 89 | + const parent1_traceId = event.contexts?.trace?.trace_id as string; |
| 90 | + const parent1_spanId = event.contexts?.trace?.span_id as string; |
| 91 | + |
| 92 | + const spans = event.spans || []; |
| 93 | + const child1_1 = spans.find(span => span.description === 'child1.1'); |
| 94 | + const child1_2 = spans.find(span => span.description === 'child1.2'); |
| 95 | + |
| 96 | + expect(child1_1).toBeDefined(); |
| 97 | + expect(child1_1?.links).toEqual([ |
| 98 | + expect.objectContaining({ |
| 99 | + trace_id: expect.stringMatching(parent1_traceId), |
| 100 | + span_id: expect.stringMatching(parent1_spanId), |
| 101 | + attributes: expect.objectContaining({ |
| 102 | + 'sentry.link.type': 'previous_trace', |
| 103 | + }), |
| 104 | + }), |
| 105 | + ]); |
| 106 | + |
| 107 | + expect(child1_2).toBeDefined(); |
| 108 | + expect(child1_2?.links).toEqual([ |
| 109 | + expect.objectContaining({ |
| 110 | + trace_id: expect.stringMatching(parent1_traceId), |
| 111 | + span_id: expect.stringMatching(parent1_spanId), |
| 112 | + attributes: expect.objectContaining({ |
| 113 | + 'sentry.link.type': 'previous_trace', |
| 114 | + }), |
| 115 | + }), |
| 116 | + ]); |
| 117 | + }, |
| 118 | + }) |
| 119 | + .start(done); |
| 120 | + }); |
| 121 | + |
| 122 | + test('should link spans with addLinks() in nested startSpan() calls', done => { |
| 123 | + createRunner(__dirname, 'scenario-addLinks-nested.ts') |
| 124 | + .expect({ |
| 125 | + transaction: event => { |
| 126 | + expect(event.transaction).toBe('parent1'); |
| 127 | + |
| 128 | + const parent1_traceId = event.contexts?.trace?.trace_id as string; |
| 129 | + const parent1_spanId = event.contexts?.trace?.span_id as string; |
| 130 | + |
| 131 | + const spans = event.spans || []; |
| 132 | + const child1_1 = spans.find(span => span.description === 'child1.1'); |
| 133 | + const child2_1 = spans.find(span => span.description === 'child2.1'); |
| 134 | + |
| 135 | + expect(child1_1).toBeDefined(); |
| 136 | + |
| 137 | + expect(child2_1).toBeDefined(); |
| 138 | + |
| 139 | + expect(child2_1?.links).toEqual([ |
| 140 | + expect.not.objectContaining({ attributes: expect.anything() }) && |
| 141 | + expect.objectContaining({ |
| 142 | + trace_id: expect.stringMatching(parent1_traceId), |
| 143 | + span_id: expect.stringMatching(parent1_spanId), |
| 144 | + }), |
| 145 | + expect.objectContaining({ |
| 146 | + trace_id: expect.stringMatching(child1_1?.trace_id || 'non-existent-id-fallback'), |
| 147 | + span_id: expect.stringMatching(child1_1?.span_id || 'non-existent-id-fallback'), |
| 148 | + attributes: expect.objectContaining({ |
| 149 | + 'sentry.link.type': 'previous_trace', |
| 150 | + }), |
| 151 | + }), |
| 152 | + ]); |
| 153 | + }, |
| 154 | + }) |
| 155 | + .start(done); |
| 156 | + }); |
| 157 | +}); |
0 commit comments