From 508f7e87396e72840e474dd750c73d71fe2536b6 Mon Sep 17 00:00:00 2001 From: Ali Sabzevari Date: Thu, 8 Jul 2021 18:43:49 +0200 Subject: [PATCH] fix(exporter-collector): node8 compatibility fix for exporter gzip compression --- .../src/platform/node/util.ts | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/util.ts b/packages/opentelemetry-exporter-collector/src/platform/node/util.ts index 2773824a498..4070f6abd65 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/util.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/util.ts @@ -17,7 +17,7 @@ import * as url from 'url'; import * as http from 'http'; import * as https from 'https'; import * as zlib from 'zlib'; -import { pipeline, Readable } from 'stream'; +import { Readable } from 'stream'; import * as collectorTypes from '../../types'; import { CollectorExporterNodeBase } from './CollectorExporterNodeBase'; import { CollectorExporterNodeConfigBase } from '.'; @@ -83,26 +83,24 @@ export function sendWithHttp( }); if (compress) { - const dataStream = Readable.from(data); - pipeline(dataStream, gzip, req, onGzipError(onError)); + const dataStream = readableFromBuffer(data); + dataStream.on('error', onError) + .pipe(gzip).on('error', onError) + .pipe(req); } else { req.write(data); req.end(); } } -function onGzipError(onError: (error: collectorTypes.CollectorExporterError) => void) { - return (err: NodeJS.ErrnoException | null) => { - const error = new collectorTypes.CollectorExporterError( - err?.message, - 500, - 'Compressing the request body for collector exporter failed.' - ); - onError(error) - } +function readableFromBuffer(buff: string | Buffer): Readable { + const readable = new Readable(); + readable.push(buff); + readable.push(null); + + return readable; } - export function createHttpAgent( config: CollectorExporterNodeConfigBase ): http.Agent | https.Agent | undefined {