-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtest.ts
159 lines (124 loc) · 4.52 KB
/
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
import { expect } from '@playwright/test';
import { sentryTest } from '../../../utils/fixtures';
import {
envelopeRequestParser,
properEnvelopeRequestParser,
shouldSkipTracingTest,
waitForTransactionRequest,
} from '../../../utils/helpers';
sentryTest('allows to wrap sync methods with a timing metric', async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestUrl({ testDir: __dirname });
const beforeTime = Math.floor(Date.now() / 1000);
const metricsPromiseReq = page.waitForRequest(req => {
const postData = req.postData();
if (!postData) {
return false;
}
try {
// this implies this is a metrics envelope
return typeof envelopeRequestParser(req) === 'string';
} catch {
return false;
}
});
const transactionPromise = waitForTransactionRequest(page);
await page.goto(url);
await page.waitForFunction('typeof window.timingSync === "function"');
const response = await page.evaluate('window.timingSync()');
expect(response).toBe('sync done');
const statsdString = envelopeRequestParser<string>(await metricsPromiseReq);
const transactionEvent = properEnvelopeRequestParser(await transactionPromise);
expect(typeof statsdString).toEqual('string');
const parsedStatsd = /timingSync@second:(0\.\d+)\|d\|#(.+)\|T(\d+)/.exec(statsdString);
expect(parsedStatsd).toBeTruthy();
const duration = parseFloat(parsedStatsd![1]);
const tags = parsedStatsd![2];
const timestamp = parseInt(parsedStatsd![3], 10);
expect(timestamp).toBeGreaterThanOrEqual(beforeTime);
expect(tags).toEqual('release:1.0.0,transaction:manual span');
expect(duration).toBeGreaterThan(0.2);
expect(duration).toBeLessThan(1);
expect(transactionEvent).toBeDefined();
expect(transactionEvent.transaction).toEqual('manual span');
const spans = transactionEvent.spans || [];
expect(spans.length).toBe(1);
const span = spans[0];
expect(span.op).toEqual('metrics.timing');
expect(span.description).toEqual('timingSync');
expect(span.timestamp! - span.start_timestamp).toEqual(duration);
expect(span._metrics_summary).toEqual({
'd:timingSync@second': [
{
count: 1,
max: duration,
min: duration,
sum: duration,
tags: {
release: '1.0.0',
transaction: 'manual span',
},
},
],
});
});
sentryTest('allows to wrap async methods with a timing metric', async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestUrl({ testDir: __dirname });
const beforeTime = Math.floor(Date.now() / 1000);
const metricsPromiseReq = page.waitForRequest(req => {
const postData = req.postData();
if (!postData) {
return false;
}
try {
// this implies this is a metrics envelope
return typeof envelopeRequestParser(req) === 'string';
} catch {
return false;
}
});
const transactionPromise = waitForTransactionRequest(page);
await page.goto(url);
await page.waitForFunction('typeof window.timingAsync === "function"');
const response = await page.evaluate('window.timingAsync()');
expect(response).toBe('async done');
const statsdString = envelopeRequestParser<string>(await metricsPromiseReq);
const transactionEvent = properEnvelopeRequestParser(await transactionPromise);
expect(typeof statsdString).toEqual('string');
const parsedStatsd = /timingAsync@second:(0\.\d+)\|d\|#(.+)\|T(\d+)/.exec(statsdString);
expect(parsedStatsd).toBeTruthy();
const duration = parseFloat(parsedStatsd![1]);
const tags = parsedStatsd![2];
const timestamp = parseInt(parsedStatsd![3], 10);
expect(timestamp).toBeGreaterThanOrEqual(beforeTime);
expect(tags).toEqual('release:1.0.0,transaction:manual span');
expect(duration).toBeGreaterThan(0.2);
expect(duration).toBeLessThan(1);
expect(transactionEvent).toBeDefined();
expect(transactionEvent.transaction).toEqual('manual span');
const spans = transactionEvent.spans || [];
expect(spans.length).toBe(1);
const span = spans[0];
expect(span.op).toEqual('metrics.timing');
expect(span.description).toEqual('timingAsync');
expect(span.timestamp! - span.start_timestamp).toEqual(duration);
expect(span._metrics_summary).toEqual({
'd:timingAsync@second': [
{
count: 1,
max: duration,
min: duration,
sum: duration,
tags: {
release: '1.0.0',
transaction: 'manual span',
},
},
],
});
});