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