From 3bf12847d166db34967355a6de8cee5b4b70fab3 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Wed, 27 Nov 2024 10:14:59 +0100 Subject: [PATCH] feat(otlp-exporter-base): add http response body to exporter error (#5204) --- experimental/CHANGELOG.md | 1 + .../src/transport/http-transport-utils.ts | 6 +++++- .../test/node/http-exporter-transport.test.ts | 11 ++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 9127b7bd71..eebd013262 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -25,6 +25,7 @@ All notable changes to experimental packages in this project will be documented * feat(otlp-exporter-base): internally accept a http header provider function only [#5179](https://github.com/open-telemetry/opentelemetry-js/pull/5179) @pichlermarc * refactor(otlp-exporter-base): don't create blob before sending xhr [#5193](https://github.com/open-telemetry/opentelemetry-js/pull/5193) @pichlermarc * improves compatibility with some unsupported runtimes +* feat(otlp-exporter-base): add http response body to exporter error [#5204](https://github.com/open-telemetry/opentelemetry-js/pull/5204) @pichlermarc ### :bug: (Bug Fix) diff --git a/experimental/packages/otlp-exporter-base/src/transport/http-transport-utils.ts b/experimental/packages/otlp-exporter-base/src/transport/http-transport-utils.ts index 99c55b0872..5dab9670b5 100644 --- a/experimental/packages/otlp-exporter-base/src/transport/http-transport-utils.ts +++ b/experimental/packages/otlp-exporter-base/src/transport/http-transport-utils.ts @@ -72,7 +72,11 @@ export function sendWithHttp( retryInMillis: parseRetryAfterToMills(res.headers['retry-after']), }); } else { - const error = new OTLPExporterError(res.statusMessage, res.statusCode); + const error = new OTLPExporterError( + res.statusMessage, + res.statusCode, + Buffer.concat(responseData).toString() + ); onDone({ status: 'failure', error, diff --git a/experimental/packages/otlp-exporter-base/test/node/http-exporter-transport.test.ts b/experimental/packages/otlp-exporter-base/test/node/http-exporter-transport.test.ts index d4c9230ae1..2e511893ed 100644 --- a/experimental/packages/otlp-exporter-base/test/node/http-exporter-transport.test.ts +++ b/experimental/packages/otlp-exporter-base/test/node/http-exporter-transport.test.ts @@ -22,6 +22,7 @@ import { ExportResponseRetryable, ExportResponseFailure, ExportResponseSuccess, + OTLPExporterError, } from '../../src'; import * as zlib from 'zlib'; @@ -123,7 +124,7 @@ describe('HttpExporterTransport', function () { // arrange server = http.createServer((_, res) => { res.statusCode = 404; - res.end(); + res.end('response-body'); }); server.listen(8080); @@ -143,6 +144,14 @@ describe('HttpExporterTransport', function () { (result as ExportResponseFailure).error.message, 'Not Found' ); + assert.strictEqual( + ((result as ExportResponseFailure).error as OTLPExporterError).data, + 'response-body' + ); + assert.strictEqual( + ((result as ExportResponseFailure).error as OTLPExporterError).code, + 404 + ); }); it('returns failure when request times out', function (done) {