From 762a65a6e4760f3da758fb766cbfd6069c6b92bd Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 29 Nov 2023 16:12:03 -0500 Subject: [PATCH] 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);