From 195bb0e791657efd7bac0387d822695053196784 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 15 Nov 2024 12:32:45 +0100 Subject: [PATCH 1/3] fix(otlp-exporter-base): fix unhandled error when writing to destroyed http request --- .../src/platform/node/http-transport-utils.ts | 4 +-- .../test/node/http-transport-utils.test.ts | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts diff --git a/experimental/packages/otlp-exporter-base/src/platform/node/http-transport-utils.ts b/experimental/packages/otlp-exporter-base/src/platform/node/http-transport-utils.ts index 8fa7529fdc4..6d7822af370 100644 --- a/experimental/packages/otlp-exporter-base/src/platform/node/http-transport-utils.ts +++ b/experimental/packages/otlp-exporter-base/src/platform/node/http-transport-utils.ts @@ -111,7 +111,7 @@ export function sendWithHttp( }); } -function compressAndSend( +export function compressAndSend( req: http.ClientRequest, compression: 'gzip' | 'none', data: Uint8Array, @@ -127,7 +127,7 @@ function compressAndSend( .on('error', onError); } - dataStream.pipe(req); + dataStream.pipe(req).on('error', onError); } function readableFromUint8Array(buff: string | Uint8Array): Readable { diff --git a/experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts b/experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts new file mode 100644 index 00000000000..11ada3d8b7d --- /dev/null +++ b/experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts @@ -0,0 +1,33 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import * as http from 'http'; +import { compressAndSend } from '../../src/platform/node/http-transport-utils'; +import * as assert from 'assert'; + +describe('compressAndSend', function () { + it('compressAndSend on destroyed request should handle error', function (done) { + const request = http.request({}); + request.destroy(); + compressAndSend(request, 'gzip', Buffer.from([1, 2, 3]), error => { + try { + assert.match(error.message, /socket hang up/); + done(); + } catch (e) { + done(e); + } + }); + }); +}); From daf4169f60bc2d20740b88c93ba081ea2386bb04 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 15 Nov 2024 12:42:30 +0100 Subject: [PATCH 2/3] chore: add changelog entry --- experimental/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 0e3f5c968d6..86224e1a941 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -52,9 +52,9 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) * fix(instrumentation-http): Fix the `OTEL_SEMCONV_STABILITY_OPT_IN` variable check. Using `of` instead of `in` [#5137](/~https://github.com/open-telemetry/opentelemetry-js/pull/5137) - * fix(instrumentation-http): drop url.parse in favor of URL constructor [#5091](/~https://github.com/open-telemetry/opentelemetry-js/pull/5091) @pichlermarc * fixes a bug where using cyrillic characters in a client request string URL would throw an exception, whereas an un-instrumented client would accept the same input without throwing an exception +* fix(otlp-exporter-base): fix unhandled error when writing to destroyed http request [#5163](/~https://github.com/open-telemetry/opentelemetry-js/pull/5163) @pichlermarc ### :books: (Refine Doc) From ce2f9970e70416484b2da2e83d90da797c0b67b8 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 15 Nov 2024 12:44:02 +0100 Subject: [PATCH 3/3] refactor: import order --- .../otlp-exporter-base/test/node/http-transport-utils.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts b/experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts index 11ada3d8b7d..8d588300070 100644 --- a/experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts +++ b/experimental/packages/otlp-exporter-base/test/node/http-transport-utils.test.ts @@ -14,8 +14,8 @@ * limitations under the License. */ import * as http from 'http'; -import { compressAndSend } from '../../src/platform/node/http-transport-utils'; import * as assert from 'assert'; +import { compressAndSend } from '../../src/platform/node/http-transport-utils'; describe('compressAndSend', function () { it('compressAndSend on destroyed request should handle error', function (done) {