Skip to content

Commit ad6f789

Browse files
authored
feat(node): Ensure tracePropagationTargets are respected (#11285)
This PR ensures that `tracePropagationTargets` are respected for node API requests. I also updated the node-fetch instrumentation, because that fixed an issue where `http.url` was not correctly set. I also updated the other node-fastify E2E tests to be less flaky (we could have race conditions of transactions we were waiting for). I bumped `@types/node` to the correct version to ensure I can use `crypto` properly in tests (and that generally makes sense, I'd say). Closes #11272
1 parent a0b3540 commit ad6f789

File tree

25 files changed

+286
-194
lines changed

25 files changed

+286
-194
lines changed

dev-packages/browser-integration-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
},
5050
"devDependencies": {
5151
"@types/glob": "8.0.0",
52-
"@types/node": "^14.6.4",
52+
"@types/node": "^14.18.0",
5353
"@types/pako": "^2.0.0",
5454
"glob": "8.0.3"
5555
},

dev-packages/e2e-tests/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
},
1919
"devDependencies": {
2020
"@types/glob": "8.0.0",
21-
"@types/node": "^14.6.4",
21+
"@types/node": "^14.18.0",
2222
"dotenv": "16.0.3",
2323
"esbuild": "0.20.0",
2424
"glob": "8.0.3",
2525
"ts-node": "10.9.1",
26-
"yaml": "2.2.2"
26+
"yaml": "2.2.2",
27+
"rimraf": "^3.0.2"
2728
},
2829
"volta": {
2930
"node": "18.18.0",
30-
"yarn": "1.22.19"
31+
"yarn": "1.22.19",
32+
"pnpm": "8.15.5"
3133
}
3234
}

dev-packages/e2e-tests/test-applications/node-fastify-app/src/app.js

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const http = require('http');
66

77
const app = fastify();
88
const port = 3030;
9+
const port2 = 3040;
910

1011
Sentry.setupFastifyErrorHandler(app);
1112

@@ -17,20 +18,22 @@ app.get('/test-param/:param', function (req, res) {
1718
res.send({ paramWas: req.params.param });
1819
});
1920

20-
app.get('/test-inbound-headers', function (req, res) {
21+
app.get('/test-inbound-headers/:id', function (req, res) {
2122
const headers = req.headers;
2223

23-
res.send({ headers });
24+
res.send({ headers, id: req.params.id });
2425
});
2526

26-
app.get('/test-outgoing-http', async function (req, res) {
27-
const data = await makeHttpRequest('http://localhost:3030/test-inbound-headers');
27+
app.get('/test-outgoing-http/:id', async function (req, res) {
28+
const id = req.params.id;
29+
const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`);
2830

2931
res.send(data);
3032
});
3133

32-
app.get('/test-outgoing-fetch', async function (req, res) {
33-
const response = await fetch('http://localhost:3030/test-inbound-headers');
34+
app.get('/test-outgoing-fetch/:id', async function (req, res) {
35+
const id = req.params.id;
36+
const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`);
3437
const data = await response.json();
3538

3639
res.send(data);
@@ -56,8 +59,48 @@ app.get('/test-exception', async function (req, res) {
5659
throw new Error('This is an exception');
5760
});
5861

62+
app.get('/test-outgoing-fetch-external-allowed', async function (req, res) {
63+
const fetchResponse = await fetch(`http://localhost:${port2}/external-allowed`);
64+
const data = await fetchResponse.json();
65+
66+
res.send(data);
67+
});
68+
69+
app.get('/test-outgoing-fetch-external-disallowed', async function (req, res) {
70+
const fetchResponse = await fetch(`http://localhost:${port2}/external-disallowed`);
71+
const data = await fetchResponse.json();
72+
73+
res.send(data);
74+
});
75+
76+
app.get('/test-outgoing-http-external-allowed', async function (req, res) {
77+
const data = await makeHttpRequest(`http://localhost:${port2}/external-allowed`);
78+
res.send(data);
79+
});
80+
81+
app.get('/test-outgoing-http-external-disallowed', async function (req, res) {
82+
const data = await makeHttpRequest(`http://localhost:${port2}/external-disallowed`);
83+
res.send(data);
84+
});
85+
5986
app.listen({ port: port });
6087

88+
// A second app so we can test header propagation between external URLs
89+
const app2 = fastify();
90+
app2.get('/external-allowed', function (req, res) {
91+
const headers = req.headers;
92+
93+
res.send({ headers, route: '/external-allowed' });
94+
});
95+
96+
app2.get('/external-disallowed', function (req, res) {
97+
const headers = req.headers;
98+
99+
res.send({ headers, route: '/external-disallowed' });
100+
});
101+
102+
app2.listen({ port: port2 });
103+
61104
function makeHttpRequest(url) {
62105
return new Promise(resolve => {
63106
const data = [];
@@ -67,9 +110,16 @@ function makeHttpRequest(url) {
67110
httpRes.on('data', chunk => {
68111
data.push(chunk);
69112
});
113+
httpRes.on('error', error => {
114+
resolve({ error: error.message, url });
115+
});
70116
httpRes.on('end', () => {
71-
const json = JSON.parse(Buffer.concat(data).toString());
72-
resolve(json);
117+
try {
118+
const json = JSON.parse(Buffer.concat(data).toString());
119+
resolve(json);
120+
} catch {
121+
resolve({ data: Buffer.concat(data).toString(), url });
122+
}
73123
});
74124
})
75125
.end();

dev-packages/e2e-tests/test-applications/node-fastify-app/src/tracing.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Sentry.init({
66
integrations: [],
77
tracesSampleRate: 1,
88
tunnel: 'http://localhost:3031/', // proxy server
9+
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
910
});

0 commit comments

Comments
 (0)