diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index f36b4333ba1..8d4561d26c8 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -352,8 +352,21 @@ export class HttpInstrumentation extends InstrumentationBase { context.bind(context.active(), response); this._diag.debug('outgoingRequest on response()'); + + // See https://github.com/open-telemetry/opentelemetry-js/pull/3625#issuecomment-1475673533 + let endEmitted = false; + if (semver.lt(process.version, '16.0.0')) { + response.on('close', () => { + this._diag.debug('outgoingRequest on close()'); + if (!endEmitted) { + response.emit('end'); + } + }); + } + response.on('end', () => { this._diag.debug('outgoingRequest on end()'); + endEmitted = true; let status: SpanStatus; if (response.aborted && !response.complete) { 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 ef0030141a3..adc8ba3412a 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 @@ -866,7 +866,7 @@ describe('HttpInstrumentation', () => { `${protocol}://${hostname}:${serverPort}/hang`, res => { res.on('close', () => {}); - res.on('error', () => {}); + res.on('error', reject); } ); // close the socket.