-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathapp.ts
More file actions
163 lines (126 loc) · 4.57 KB
/
app.ts
File metadata and controls
163 lines (126 loc) · 4.57 KB
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
import type * as S from '@sentry/node';
const Sentry = require('@sentry/node') as typeof S;
// We wrap console.warn to find out if a warning is incorrectly logged
console.warn = new Proxy(console.warn, {
apply: function (target, thisArg, argumentsList) {
const msg = argumentsList[0];
if (typeof msg === 'string' && msg.startsWith('[Sentry]')) {
console.error(`Sentry warning was triggered: ${msg}`);
process.exit(1);
}
return target.apply(thisArg, argumentsList);
},
});
Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
});
import type * as H from 'http';
import type * as F from 'fastify';
// Make sure fastify is imported after Sentry is initialized
const { fastify } = require('fastify') as typeof F;
const http = require('http') as typeof H;
const app = fastify();
const port = 3030;
const port2 = 3040;
Sentry.setupFastifyErrorHandler(app);
app.get('/test-success', function (_req, res) {
res.send({ version: 'v1' });
});
app.get<{ Params: { param: string } }>('/test-param/:param', function (req, res) {
res.send({ paramWas: req.params.param });
});
app.get<{ Params: { id: string } }>('/test-inbound-headers/:id', function (req, res) {
const headers = req.headers;
res.send({ headers, id: req.params.id });
});
app.get<{ Params: { id: string } }>('/test-outgoing-http/:id', async function (req, res) {
const id = req.params.id;
const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`);
res.send(data);
});
app.get<{ Params: { id: string } }>('/test-outgoing-fetch/:id', async function (req, res) {
const id = req.params.id;
const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`);
const data = await response.json();
res.send(data);
});
app.get('/test-transaction', async function (req, res) {
Sentry.startSpan({ name: 'test-span' }, () => {
Sentry.startSpan({ name: 'child-span' }, () => {});
});
res.send({});
});
app.get('/test-error', async function (req, res) {
const exceptionId = Sentry.captureException(new Error('This is an error'));
await Sentry.flush(2000);
res.send({ exceptionId });
});
app.get('/test-4xx-error', async function (req, res) {
res.code(400);
throw new Error('This is a 4xx error');
});
app.get('/test-5xx-error', async function (req, res) {
res.code(500);
throw new Error('This is a 5xx error');
});
app.get<{ Params: { id: string } }>('/test-exception/:id', async function (req, res) {
throw new Error(`This is an exception with id ${req.params.id}`);
});
app.get('/test-outgoing-fetch-external-allowed', async function (req, res) {
const fetchResponse = await fetch(`http://localhost:${port2}/external-allowed`);
const data = await fetchResponse.json();
res.send(data);
});
app.get('/test-outgoing-fetch-external-disallowed', async function (req, res) {
const fetchResponse = await fetch(`http://localhost:${port2}/external-disallowed`);
const data = await fetchResponse.json();
res.send(data);
});
app.get('/test-outgoing-http-external-allowed', async function (req, res) {
const data = await makeHttpRequest(`http://localhost:${port2}/external-allowed`);
res.send(data);
});
app.get('/test-outgoing-http-external-disallowed', async function (req, res) {
const data = await makeHttpRequest(`http://localhost:${port2}/external-disallowed`);
res.send(data);
});
app.listen({ port: port });
// A second app so we can test header propagation between external URLs
const app2 = fastify();
app2.get('/external-allowed', function (req, res) {
const headers = req.headers;
res.send({ headers, route: '/external-allowed' });
});
app2.get('/external-disallowed', function (req, res) {
const headers = req.headers;
res.send({ headers, route: '/external-disallowed' });
});
app2.listen({ port: port2 });
function makeHttpRequest(url: string) {
return new Promise(resolve => {
const data: any[] = [];
http
.request(url, httpRes => {
httpRes.on('data', chunk => {
data.push(chunk);
});
httpRes.on('error', error => {
resolve({ error: error.message, url });
});
httpRes.on('end', () => {
try {
const json = JSON.parse(Buffer.concat(data).toString());
resolve(json);
} catch {
resolve({ data: Buffer.concat(data).toString(), url });
}
});
})
.end();
});
}