diff --git a/packages/instrumentation-undici/test/fetch.test.ts b/packages/instrumentation-undici/test/fetch.test.ts index 7e389838bb..7496f860de 100644 --- a/packages/instrumentation-undici/test/fetch.test.ts +++ b/packages/instrumentation-undici/test/fetch.test.ts @@ -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') @@ -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 @@ -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', }, }); }); @@ -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]; diff --git a/packages/instrumentation-undici/test/metrics.test.ts b/packages/instrumentation-undici/test/metrics.test.ts index bca94ac6e9..5a4dd16048 100644 --- a/packages/instrumentation-undici/test/metrics.test.ts +++ b/packages/instrumentation-undici/test/metrics.test.ts @@ -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'); @@ -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); @@ -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` diff --git a/packages/instrumentation-undici/test/undici.test.ts b/packages/instrumentation-undici/test/undici.test.ts index 2a124fb199..26e391f32a 100644 --- a/packages/instrumentation-undici/test/undici.test.ts +++ b/packages/instrumentation-undici/test/undici.test.ts @@ -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') @@ -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 @@ -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', }, }); }); @@ -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];