Skip to content

Commit 5f51310

Browse files
committed
WIP add test
1 parent b3242e0 commit 5f51310

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracePropagationTargets: [/\/v0/, 'v1'],
8+
integrations: [Sentry.httpIntegration({ spans: false })],
9+
transport: loggingTransport,
10+
// Ensure this gets a correct hint
11+
beforeBreadcrumb(breadcrumb, hint) {
12+
breadcrumb.data = breadcrumb.data || {};
13+
const req = hint?.request;
14+
breadcrumb.data.ADDED_PATH = req?.path;
15+
return breadcrumb;
16+
},
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as Sentry from '@sentry/node';
2+
import * as http from 'http';
3+
4+
async function run() {
5+
Sentry.addBreadcrumb({ message: 'manual breadcrumb' });
6+
7+
await makeHttpRequest(`${process.env.SERVER_URL}/api/v0`);
8+
await makeHttpGet(`${process.env.SERVER_URL}/api/v1`);
9+
await makeHttpRequest(`${process.env.SERVER_URL}/api/v2`);
10+
await makeHttpRequest(`${process.env.SERVER_URL}/api/v3`);
11+
12+
Sentry.captureException(new Error('foo'));
13+
}
14+
15+
run();
16+
17+
function makeHttpRequest(url) {
18+
return new Promise(resolve => {
19+
http
20+
.request(url, httpRes => {
21+
httpRes.on('data', () => {
22+
// we don't care about data
23+
});
24+
httpRes.on('end', () => {
25+
resolve();
26+
});
27+
})
28+
.end();
29+
});
30+
}
31+
32+
function makeHttpGet(url) {
33+
return new Promise(resolve => {
34+
http.get(url, httpRes => {
35+
httpRes.on('data', () => {
36+
// we don't care about data
37+
});
38+
httpRes.on('end', () => {
39+
resolve();
40+
});
41+
});
42+
});
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { describe,expect } from 'vitest';
2+
import { createEsmAndCjsTests } from '../../../../utils/runner';
3+
import { createTestServer } from '../../../../utils/server';
4+
5+
describe('outgoing http requests with tracing & spans disabled xxx', () => {
6+
createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
7+
test('outgoing http requests are correctly instrumented with tracing & spans disabled', async () => {
8+
expect.assertions(11);
9+
10+
const [SERVER_URL, closeTestServer] = await createTestServer()
11+
.get('/api/v0', headers => {
12+
expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/));
13+
expect(headers['sentry-trace']).not.toEqual('00000000000000000000000000000000-0000000000000000');
14+
expect(headers['baggage']).toEqual(expect.any(String));
15+
})
16+
.get('/api/v1', headers => {
17+
expect(headers['sentry-trace']).toEqual(expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/));
18+
expect(headers['sentry-trace']).not.toEqual('00000000000000000000000000000000-0000000000000000');
19+
expect(headers['baggage']).toEqual(expect.any(String));
20+
})
21+
.get('/api/v2', headers => {
22+
expect(headers['baggage']).toBeUndefined();
23+
expect(headers['sentry-trace']).toBeUndefined();
24+
})
25+
.get('/api/v3', headers => {
26+
expect(headers['baggage']).toBeUndefined();
27+
expect(headers['sentry-trace']).toBeUndefined();
28+
})
29+
.start();
30+
31+
await createRunner()
32+
.withEnv({ SERVER_URL })
33+
.ensureNoErrorOutput()
34+
.expect({
35+
event: {
36+
exception: {
37+
values: [
38+
{
39+
type: 'Error',
40+
value: 'foo',
41+
},
42+
],
43+
},
44+
breadcrumbs: [
45+
{
46+
message: 'manual breadcrumb',
47+
timestamp: expect.any(Number),
48+
},
49+
{
50+
category: 'http',
51+
data: {
52+
'http.method': 'GET',
53+
url: `${SERVER_URL}/api/v0`,
54+
status_code: 200,
55+
ADDED_PATH: '/api/v0',
56+
},
57+
timestamp: expect.any(Number),
58+
type: 'http',
59+
},
60+
{
61+
category: 'http',
62+
data: {
63+
'http.method': 'GET',
64+
url: `${SERVER_URL}/api/v1`,
65+
status_code: 200,
66+
ADDED_PATH: '/api/v1',
67+
},
68+
timestamp: expect.any(Number),
69+
type: 'http',
70+
},
71+
{
72+
category: 'http',
73+
data: {
74+
'http.method': 'GET',
75+
url: `${SERVER_URL}/api/v2`,
76+
status_code: 200,
77+
ADDED_PATH: '/api/v2',
78+
},
79+
timestamp: expect.any(Number),
80+
type: 'http',
81+
},
82+
{
83+
category: 'http',
84+
data: {
85+
'http.method': 'GET',
86+
url: `${SERVER_URL}/api/v3`,
87+
status_code: 200,
88+
ADDED_PATH: '/api/v3',
89+
},
90+
timestamp: expect.any(Number),
91+
type: 'http',
92+
},
93+
],
94+
},
95+
})
96+
.start()
97+
.completed();
98+
99+
closeTestServer();
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)