From 225e24a880ab2b5495401f1abd6246531cbf89bc Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 20 Mar 2023 10:24:44 +0100 Subject: [PATCH] attempt to add a test --- .../test/functionals/http-enable.test.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index adc8ba3412..7f70c7118c 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -16,11 +16,13 @@ import { SpanStatusCode, context, + diag, propagation, Span as ISpan, SpanKind, trace, SpanAttributes, + DiagConsoleLogger, } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { @@ -269,6 +271,14 @@ describe('HttpInstrumentation', () => { // hang the request. return; } + if (request.url?.includes('/destroy-request')) { + // force flush http response header to trigger client response callback + response.write(''); + setTimeout(() => { + request.socket.destroy(); + }, 100); + return; + } if (request.url?.includes('/ignored')) { provider.getTracer('test').startSpan('some-span').end(); } @@ -920,6 +930,37 @@ describe('HttpInstrumentation', () => { assert.strictEqual(clientSpan.status.code, SpanStatusCode.ERROR); assert.ok(Object.keys(clientSpan.attributes).length >= 6); }); + + it('should not end span multiple times if request socket destroyed before response completes', async () => { + const warnMessages: string[] = []; + diag.setLogger({ + ...new DiagConsoleLogger(), + warn: message => { + warnMessages.push(message); + }, + }); + const promise = new Promise(resolve => { + const req = http.get( + `${protocol}://${hostname}:${serverPort}/destroy-request`, + res => { + res.on('end', () => {}); + res.on('close', () => {}); + res.on('error', () => { + resolve(); + }); + } + ); + // force flush http request header to trigger client response callback + req.write(''); + req.on('error', () => {}); + }); + + await promise; + + diag.disable(); + + assert.strictEqual(warnMessages, []); + }); }); describe('with require parent span', () => {