From 762a65a6e4760f3da758fb766cbfd6069c6b92bd Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 29 Nov 2023 16:12:03 -0500 Subject: [PATCH 1/2] fix(instrumentation-http): resume responses when there is no response listener Fixes a memory leak where unhandled response bodies pile up in node 20 --- experimental/CHANGELOG.md | 1 + .../packages/opentelemetry-instrumentation-http/src/http.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index b9935e26c5..90dedfcf6f 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to experimental packages in this project will be documented * fix(sdk-logs): avoid map attribute set when count limit exceeded * fix(instrumentation-fetch): only access navigator if it is defined [#4063](https://github.com/open-telemetry/opentelemetry-js/pull/4063) * allows for experimental usage of this instrumentation with non-browser runtimes +* fix(instrumentation-http): memory leak when responses are not resumed ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 9422bbc9ef..a76f1a918d 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -330,6 +330,9 @@ export class HttpInstrumentation extends InstrumentationBase { 'response', (response: http.IncomingMessage & { aborted?: boolean }) => { this._diag.debug('outgoingRequest on response()'); + if (request.listenerCount('request') <= 0) { + response.resume(); + } const responseAttributes = utils.getOutgoingRequestAttributesOnResponse(response); span.setAttributes(responseAttributes); From bf8ee62a1ecb3cf37af0f09d4dc67c47f2d1dd9b Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Thu, 30 Nov 2023 08:37:03 -0500 Subject: [PATCH 2/2] Fix event name --- .../packages/opentelemetry-instrumentation-http/src/http.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index a76f1a918d..81e08ac860 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -330,7 +330,7 @@ export class HttpInstrumentation extends InstrumentationBase { 'response', (response: http.IncomingMessage & { aborted?: boolean }) => { this._diag.debug('outgoingRequest on response()'); - if (request.listenerCount('request') <= 0) { + if (request.listenerCount('response') <= 1) { response.resume(); } const responseAttributes =