-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgroupSpansWithParents.test.ts
174 lines (141 loc) · 6.47 KB
/
groupSpansWithParents.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { trace } from '@opentelemetry/api';
import type { BasicTracerProvider, ReadableSpan } from '@opentelemetry/sdk-trace-base';
import type { Span } from '@sentry/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { withActiveSpan } from '../../src/trace';
import { groupSpansWithParents } from '../../src/utils/groupSpansWithParents';
import { TestClient, getDefaultTestClientOptions } from '../helpers/TestClient';
import { setupOtel } from '../helpers/initOtel';
import { cleanupOtel } from '../helpers/mockSdkInit';
describe('groupSpansWithParents', () => {
let provider: BasicTracerProvider | undefined;
beforeEach(() => {
const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1 }));
provider = setupOtel(client);
});
afterEach(() => {
cleanupOtel(provider);
});
it('works with no spans', () => {
const actual = groupSpansWithParents([]);
expect(actual).toEqual([]);
});
it('works with a single root span & in-order spans', () => {
const tracer = trace.getTracer('test');
const rootSpan = tracer.startSpan('root') as unknown as ReadableSpan;
const parentSpan1 = withActiveSpan(
rootSpan as unknown as Span,
() => tracer.startSpan('parent1') as unknown as ReadableSpan,
);
const parentSpan2 = withActiveSpan(
rootSpan as unknown as Span,
() => tracer.startSpan('parent2') as unknown as ReadableSpan,
);
const child1 = withActiveSpan(
parentSpan1 as unknown as Span,
() => tracer.startSpan('child1') as unknown as ReadableSpan,
);
const actual = groupSpansWithParents([rootSpan, parentSpan1, parentSpan2, child1]);
expect(actual).toHaveLength(4);
// Ensure parent & span is correctly set
const rootRef = actual.find(ref => ref.span === rootSpan);
const parent1Ref = actual.find(ref => ref.span === parentSpan1);
const parent2Ref = actual.find(ref => ref.span === parentSpan2);
const child1Ref = actual.find(ref => ref.span === child1);
expect(rootRef).toBeDefined();
expect(parent1Ref).toBeDefined();
expect(parent2Ref).toBeDefined();
expect(child1Ref).toBeDefined();
expect(rootRef?.parentNode).toBeUndefined();
expect(rootRef?.children).toEqual([parent1Ref, parent2Ref]);
expect(parent1Ref?.span).toBe(parentSpan1);
expect(parent2Ref?.span).toBe(parentSpan2);
expect(parent1Ref?.parentNode).toBe(rootRef);
expect(parent2Ref?.parentNode).toBe(rootRef);
expect(parent1Ref?.children).toEqual([child1Ref]);
expect(parent2Ref?.children).toEqual([]);
expect(child1Ref?.parentNode).toBe(parent1Ref);
expect(child1Ref?.children).toEqual([]);
});
it('works with a spans with missing root span', () => {
const tracer = trace.getTracer('test');
// We create this root span here, but we do not pass it to `groupSpansWithParents` below
const rootSpan = tracer.startSpan('root') as unknown as ReadableSpan;
const parentSpan1 = withActiveSpan(
rootSpan as unknown as Span,
() => tracer.startSpan('parent1') as unknown as ReadableSpan,
);
const parentSpan2 = withActiveSpan(
rootSpan as unknown as Span,
() => tracer.startSpan('parent2') as unknown as ReadableSpan,
);
const child1 = withActiveSpan(
parentSpan1 as unknown as Span,
() => tracer.startSpan('child1') as unknown as ReadableSpan,
);
const actual = groupSpansWithParents([parentSpan1, parentSpan2, child1]);
expect(actual).toHaveLength(4);
// Ensure parent & span is correctly set
const rootRef = actual.find(ref => ref.id === rootSpan.spanContext().spanId);
const parent1Ref = actual.find(ref => ref.span === parentSpan1);
const parent2Ref = actual.find(ref => ref.span === parentSpan2);
const child1Ref = actual.find(ref => ref.span === child1);
expect(rootRef).toBeDefined();
expect(parent1Ref).toBeDefined();
expect(parent2Ref).toBeDefined();
expect(child1Ref).toBeDefined();
expect(rootRef?.parentNode).toBeUndefined();
expect(rootRef?.span).toBeUndefined();
expect(rootRef?.children).toEqual([parent1Ref, parent2Ref]);
expect(parent1Ref?.span).toBe(parentSpan1);
expect(parent2Ref?.span).toBe(parentSpan2);
expect(parent1Ref?.parentNode).toBe(rootRef);
expect(parent2Ref?.parentNode).toBe(rootRef);
expect(parent1Ref?.children).toEqual([child1Ref]);
expect(parent2Ref?.children).toEqual([]);
expect(child1Ref?.parentNode).toBe(parent1Ref);
expect(child1Ref?.children).toEqual([]);
});
it('works with multiple root spans & out-of-order spans', () => {
const tracer = trace.getTracer('test');
const rootSpan1 = tracer.startSpan('root1') as unknown as ReadableSpan;
const rootSpan2 = tracer.startSpan('root2') as unknown as ReadableSpan;
const parentSpan1 = withActiveSpan(
rootSpan1 as unknown as Span,
() => tracer.startSpan('parent1') as unknown as ReadableSpan,
);
const parentSpan2 = withActiveSpan(
rootSpan2 as unknown as Span,
() => tracer.startSpan('parent2') as unknown as ReadableSpan,
);
const childSpan1 = withActiveSpan(
parentSpan1 as unknown as Span,
() => tracer.startSpan('child1') as unknown as ReadableSpan,
);
const actual = groupSpansWithParents([childSpan1, parentSpan1, parentSpan2, rootSpan2, rootSpan1]);
expect(actual).toHaveLength(5);
// Ensure parent & span is correctly set
const root1Ref = actual.find(ref => ref.span === rootSpan1);
const root2Ref = actual.find(ref => ref.span === rootSpan2);
const parent1Ref = actual.find(ref => ref.span === parentSpan1);
const parent2Ref = actual.find(ref => ref.span === parentSpan2);
const child1Ref = actual.find(ref => ref.span === childSpan1);
expect(root1Ref).toBeDefined();
expect(root2Ref).toBeDefined();
expect(parent1Ref).toBeDefined();
expect(parent2Ref).toBeDefined();
expect(child1Ref).toBeDefined();
expect(root1Ref?.parentNode).toBeUndefined();
expect(root1Ref?.children).toEqual([parent1Ref]);
expect(root2Ref?.parentNode).toBeUndefined();
expect(root2Ref?.children).toEqual([parent2Ref]);
expect(parent1Ref?.span).toBe(parentSpan1);
expect(parent2Ref?.span).toBe(parentSpan2);
expect(parent1Ref?.parentNode).toBe(root1Ref);
expect(parent2Ref?.parentNode).toBe(root2Ref);
expect(parent1Ref?.children).toEqual([child1Ref]);
expect(parent2Ref?.children).toEqual([]);
expect(child1Ref?.parentNode).toBe(parent1Ref);
expect(child1Ref?.children).toEqual([]);
});
});