Skip to content

Commit 7c88f2c

Browse files
authored
chore: update undici tests (#3020)
1 parent f8e623e commit 7c88f2c

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

packages/instrumentation-undici/test/fetch.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ describe('UndiciInstrumentation `fetch` tests', function () {
6464
);
6565
mockServer.start(done);
6666
mockServer.mockListener((req, res) => {
67+
if (req.url === '/error') {
68+
// Simulate an error
69+
res.destroy();
70+
return;
71+
}
6772
// There are some situations where there is no way to access headers
6873
// for trace propagation asserts like:
6974
// const resp = await fetch('http://host:port')
@@ -364,7 +369,7 @@ describe('UndiciInstrumentation `fetch` tests', function () {
364369

365370
let fetchError;
366371
try {
367-
const fetchUrl = 'http://unexistent-host-name/path';
372+
const fetchUrl = `${protocol}://${hostname}:${mockServer.port}/error`;
368373
await fetch(fetchUrl);
369374
} catch (err) {
370375
// Expected error
@@ -376,14 +381,14 @@ describe('UndiciInstrumentation `fetch` tests', function () {
376381
assert.ok(span, 'a span is present');
377382
assert.strictEqual(spans.length, 1);
378383
assertSpan(span, {
379-
hostname: 'unexistent-host-name',
384+
hostname,
380385
httpMethod: 'GET',
381-
path: '/path',
386+
path: '/error',
382387
error: fetchError,
383388
noNetPeer: true, // do not check network attribs
384389
forceStatus: {
385390
code: SpanStatusCode.ERROR,
386-
message: 'getaddrinfo ENOTFOUND unexistent-host-name',
391+
message: 'other side closed',
387392
},
388393
});
389394
});
@@ -405,7 +410,7 @@ describe('UndiciInstrumentation `fetch` tests', function () {
405410
}
406411

407412
// Let the error be published to diagnostics channel
408-
await new Promise(r => setTimeout(r, 5));
413+
await new Promise(r => setTimeout(r, 50));
409414

410415
spans = memoryExporter.getFinishedSpans();
411416
const span = spans[0];

packages/instrumentation-undici/test/metrics.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ describe('UndiciInstrumentation metrics tests', function () {
6363
);
6464
mockServer.start(done);
6565
mockServer.mockListener((req, res) => {
66+
if (req.url === '/error') {
67+
// Simulate an error
68+
res.destroy();
69+
return;
70+
}
6671
// Return a valid response always
6772
res.statusCode = 200;
6873
res.setHeader('content-type', 'application/json');
@@ -143,7 +148,7 @@ describe('UndiciInstrumentation metrics tests', function () {
143148
});
144149

145150
it('should have error.type in "http.client.request.duration" metric', async () => {
146-
const fetchUrl = 'http://unknownhost/';
151+
const fetchUrl = `${protocol}://${hostname}:${mockServer.port}/error`;
147152

148153
try {
149154
await fetch(fetchUrl);
@@ -181,9 +186,12 @@ describe('UndiciInstrumentation metrics tests', function () {
181186
);
182187
assert.strictEqual(
183188
metricAttributes[SemanticAttributes.SERVER_ADDRESS],
184-
'unknownhost'
189+
hostname
190+
);
191+
assert.strictEqual(
192+
metricAttributes[SemanticAttributes.SERVER_PORT],
193+
mockServer.port
185194
);
186-
assert.strictEqual(metricAttributes[SemanticAttributes.SERVER_PORT], 80);
187195
assert.ok(
188196
metricAttributes[SemanticAttributes.ERROR_TYPE],
189197
`the metric contains "${SemanticAttributes.ERROR_TYPE}" attribute if request failed`

packages/instrumentation-undici/test/undici.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ describe('UndiciInstrumentation `undici` tests', function () {
9090
);
9191
mockServer.start(done);
9292
mockServer.mockListener((req, res) => {
93+
if (req.url === '/error') {
94+
// Simulate an error
95+
res.destroy();
96+
return;
97+
}
9398
// There are some situations where there is no way to access headers
9499
// for trace propagation asserts like:
95100
// const resp = await fetch('http://host:port')
@@ -727,7 +732,7 @@ describe('UndiciInstrumentation `undici` tests', function () {
727732

728733
let fetchError;
729734
try {
730-
const requestUrl = 'http://unexistent-host-name/path';
735+
const requestUrl = `${protocol}://${hostname}:${mockServer.port}/error`;
731736
await undici.request(requestUrl);
732737
} catch (err) {
733738
// Expected error
@@ -739,14 +744,14 @@ describe('UndiciInstrumentation `undici` tests', function () {
739744
assert.ok(span, 'a span is present');
740745
assert.strictEqual(spans.length, 1);
741746
assertSpan(span, {
742-
hostname: 'unexistent-host-name',
747+
hostname,
743748
httpMethod: 'GET',
744-
path: '/path',
749+
path: '/error',
745750
error: fetchError,
746751
noNetPeer: true, // do not check network attribs
747752
forceStatus: {
748753
code: SpanStatusCode.ERROR,
749-
message: 'getaddrinfo ENOTFOUND unexistent-host-name',
754+
message: 'other side closed',
750755
},
751756
});
752757
});
@@ -777,7 +782,7 @@ describe('UndiciInstrumentation `undici` tests', function () {
777782
}
778783

779784
// Let the error be published to diagnostics channel
780-
await new Promise(r => setTimeout(r, 5));
785+
await new Promise(r => setTimeout(r, 50));
781786

782787
spans = memoryExporter.getFinishedSpans();
783788
const span = spans[0];

0 commit comments

Comments
 (0)