-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathpreviousTrace.test.ts
225 lines (191 loc) · 6.59 KB
/
previousTrace.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { describe, it, expect, beforeEach, vi } from 'vitest';
import type { PreviousTraceInfo } from '../../src/tracing/previousTrace';
import {
addPreviousTraceSpanLink,
getPreviousTraceFromSessionStorage,
PREVIOUS_TRACE_KEY,
PREVIOUS_TRACE_MAX_DURATION,
} from '../../src/tracing/previousTrace';
import { SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core';
import { storePreviousTraceInSessionStorage } from '../../src/tracing/previousTrace';
describe('addPreviousTraceSpanLink', () => {
it(`adds a previous_trace span link to startSpanOptions if the previous trace was created within ${PREVIOUS_TRACE_MAX_DURATION}s`, () => {
const currentSpanStart = timestampInSeconds();
const previousTraceInfo: PreviousTraceInfo = {
spanContext: {
traceId: '123',
spanId: '456',
traceFlags: 1,
},
// max time reached almost exactly
startTimestamp: currentSpanStart - PREVIOUS_TRACE_MAX_DURATION + 1,
};
const currentSpan = new SentrySpan({
name: 'test',
startTimestamp: currentSpanStart,
parentSpanId: '789',
spanId: 'abc',
traceId: 'def',
sampled: true,
});
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(previousTraceInfo, currentSpan);
expect(spanToJSON(currentSpan).links).toEqual([
{
attributes: {
'sentry.link.type': 'previous_trace',
},
trace_id: '123',
span_id: '456',
sampled: true,
},
]);
expect(updatedPreviousTraceInfo).toEqual({
spanContext: currentSpan.spanContext(),
startTimestamp: currentSpanStart,
});
});
it(`doesn't add a previous_trace span link if the previous trace was created more than ${PREVIOUS_TRACE_MAX_DURATION}s ago`, () => {
const currentSpanStart = timestampInSeconds();
const previousTraceInfo: PreviousTraceInfo = {
spanContext: {
traceId: '123',
spanId: '456',
traceFlags: 0,
},
startTimestamp: Date.now() / 1000 - PREVIOUS_TRACE_MAX_DURATION - 1,
};
const currentSpan = new SentrySpan({
name: '/dashboard',
startTimestamp: currentSpanStart,
});
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(previousTraceInfo, currentSpan);
expect(spanToJSON(currentSpan).links).toBeUndefined();
// but still updates the previousTraceInfo to the current span
expect(updatedPreviousTraceInfo).toEqual({
spanContext: currentSpan.spanContext(),
startTimestamp: currentSpanStart,
});
});
it("doesn't overwrite previously existing span links", () => {
const previousTraceInfo: PreviousTraceInfo = {
spanContext: {
traceId: '123',
spanId: '456',
traceFlags: 1,
},
startTimestamp: Date.now() / 1000,
};
const currentSpanStart = timestampInSeconds();
const currentSpan = new SentrySpan({
name: '/dashboard',
links: [
{
context: {
traceId: '789',
spanId: '101',
traceFlags: 1,
},
attributes: {
someKey: 'someValue',
},
},
],
startTimestamp: currentSpanStart,
});
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(previousTraceInfo, currentSpan);
expect(spanToJSON(currentSpan).links).toEqual([
{
trace_id: '789',
span_id: '101',
sampled: true,
attributes: {
someKey: 'someValue',
},
},
{
attributes: {
'sentry.link.type': 'previous_trace',
},
trace_id: '123',
span_id: '456',
sampled: true,
},
]);
expect(updatedPreviousTraceInfo).toEqual({
spanContext: currentSpan.spanContext(),
startTimestamp: currentSpanStart,
});
});
it("doesn't add a link and returns the current span's data as previous trace info, if previous trace info was undefined", () => {
const currentSpanStart = timestampInSeconds();
const currentSpan = new SentrySpan({ name: 'test', startTimestamp: currentSpanStart });
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(undefined, currentSpan);
expect(spanToJSON(currentSpan).links).toBeUndefined();
expect(updatedPreviousTraceInfo).toEqual({
spanContext: currentSpan.spanContext(),
startTimestamp: currentSpanStart,
});
});
it("doesn't add a link and returns the unchanged previous trace info if the current span is part of the same trace", () => {
const currentSpanStart = timestampInSeconds();
const currentSpan = new SentrySpan({
name: 'test',
startTimestamp: currentSpanStart,
traceId: '123',
spanId: '456',
});
const previousTraceInfo: PreviousTraceInfo = {
spanContext: {
traceId: '123',
spanId: '456',
traceFlags: 1,
},
startTimestamp: currentSpanStart - 1,
};
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(previousTraceInfo, currentSpan);
expect(spanToJSON(currentSpan).links).toBeUndefined();
expect(updatedPreviousTraceInfo).toBe(previousTraceInfo);
});
});
describe('store and retrieve previous trace data via sessionStorage ', () => {
const storage: Record<string, unknown> = {};
const sessionStorageMock = {
setItem: vi.fn((key, value) => {
storage[key] = value;
}),
getItem: vi.fn(key => storage[key]),
};
beforeEach(() => {
vi.clearAllMocks();
// @ts-expect-error - mock contains only necessary API
globalThis.sessionStorage = sessionStorageMock;
});
it('stores the previous trace info in sessionStorage', () => {
const previousTraceInfo: PreviousTraceInfo = {
spanContext: {
traceId: '123',
spanId: '456',
traceFlags: 1,
},
startTimestamp: Date.now() / 1000,
};
storePreviousTraceInSessionStorage(previousTraceInfo);
expect(sessionStorageMock.setItem).toHaveBeenCalledWith(PREVIOUS_TRACE_KEY, JSON.stringify(previousTraceInfo));
expect(getPreviousTraceFromSessionStorage()).toEqual(previousTraceInfo);
});
it("doesn't throw if accessing sessionStorage fails and returns undefined", () => {
// @ts-expect-error - this is fine
globalThis.sessionStorage = undefined;
const previousTraceInfo: PreviousTraceInfo = {
spanContext: {
traceId: '123',
spanId: '456',
traceFlags: 1,
},
startTimestamp: Date.now() / 1000,
};
expect(() => storePreviousTraceInSessionStorage(previousTraceInfo)).not.toThrow();
expect(getPreviousTraceFromSessionStorage).not.toThrow();
expect(getPreviousTraceFromSessionStorage()).toBeUndefined();
});
});