Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/instrumentation-undici/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ describe('UndiciInstrumentation `fetch` tests', function () {
);
mockServer.start(done);
mockServer.mockListener((req, res) => {
if (req.url === '/error') {
// Simulate an error
res.destroy();
return;
}
// There are some situations where there is no way to access headers
// for trace propagation asserts like:
// const resp = await fetch('http://host:port')
Expand Down Expand Up @@ -364,7 +369,7 @@ describe('UndiciInstrumentation `fetch` tests', function () {

let fetchError;
try {
const fetchUrl = 'http://unexistent-host-name/path';
const fetchUrl = `${protocol}://${hostname}:${mockServer.port}/error`;
await fetch(fetchUrl);
} catch (err) {
// Expected error
Expand All @@ -376,14 +381,14 @@ describe('UndiciInstrumentation `fetch` tests', function () {
assert.ok(span, 'a span is present');
assert.strictEqual(spans.length, 1);
assertSpan(span, {
hostname: 'unexistent-host-name',
hostname,
httpMethod: 'GET',
path: '/path',
path: '/error',
error: fetchError,
noNetPeer: true, // do not check network attribs
forceStatus: {
code: SpanStatusCode.ERROR,
message: 'getaddrinfo ENOTFOUND unexistent-host-name',
message: 'other side closed',
},
});
});
Expand All @@ -405,7 +410,7 @@ describe('UndiciInstrumentation `fetch` tests', function () {
}

// Let the error be published to diagnostics channel
await new Promise(r => setTimeout(r, 5));
await new Promise(r => setTimeout(r, 50));

spans = memoryExporter.getFinishedSpans();
const span = spans[0];
Expand Down
14 changes: 11 additions & 3 deletions packages/instrumentation-undici/test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ describe('UndiciInstrumentation metrics tests', function () {
);
mockServer.start(done);
mockServer.mockListener((req, res) => {
if (req.url === '/error') {
// Simulate an error
res.destroy();
return;
}
// Return a valid response always
res.statusCode = 200;
res.setHeader('content-type', 'application/json');
Expand Down Expand Up @@ -143,7 +148,7 @@ describe('UndiciInstrumentation metrics tests', function () {
});

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

try {
await fetch(fetchUrl);
Expand Down Expand Up @@ -181,9 +186,12 @@ describe('UndiciInstrumentation metrics tests', function () {
);
assert.strictEqual(
metricAttributes[SemanticAttributes.SERVER_ADDRESS],
'unknownhost'
hostname
);
assert.strictEqual(
metricAttributes[SemanticAttributes.SERVER_PORT],
mockServer.port
);
assert.strictEqual(metricAttributes[SemanticAttributes.SERVER_PORT], 80);
assert.ok(
metricAttributes[SemanticAttributes.ERROR_TYPE],
`the metric contains "${SemanticAttributes.ERROR_TYPE}" attribute if request failed`
Expand Down
15 changes: 10 additions & 5 deletions packages/instrumentation-undici/test/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ describe('UndiciInstrumentation `undici` tests', function () {
);
mockServer.start(done);
mockServer.mockListener((req, res) => {
if (req.url === '/error') {
// Simulate an error
res.destroy();
return;
}
// There are some situations where there is no way to access headers
// for trace propagation asserts like:
// const resp = await fetch('http://host:port')
Expand Down Expand Up @@ -727,7 +732,7 @@ describe('UndiciInstrumentation `undici` tests', function () {

let fetchError;
try {
const requestUrl = 'http://unexistent-host-name/path';
const requestUrl = `${protocol}://${hostname}:${mockServer.port}/error`;
await undici.request(requestUrl);
} catch (err) {
// Expected error
Expand All @@ -739,14 +744,14 @@ describe('UndiciInstrumentation `undici` tests', function () {
assert.ok(span, 'a span is present');
assert.strictEqual(spans.length, 1);
assertSpan(span, {
hostname: 'unexistent-host-name',
hostname,
httpMethod: 'GET',
path: '/path',
path: '/error',
error: fetchError,
noNetPeer: true, // do not check network attribs
forceStatus: {
code: SpanStatusCode.ERROR,
message: 'getaddrinfo ENOTFOUND unexistent-host-name',
message: 'other side closed',
},
});
});
Expand Down Expand Up @@ -777,7 +782,7 @@ describe('UndiciInstrumentation `undici` tests', function () {
}

// Let the error be published to diagnostics channel
await new Promise(r => setTimeout(r, 5));
await new Promise(r => setTimeout(r, 50));

spans = memoryExporter.getFinishedSpans();
const span = spans[0];
Expand Down
Loading