From 548cb2eb442eef5c0478f0a9329a6c35482fc045 Mon Sep 17 00:00:00 2001 From: fugao Date: Tue, 26 Sep 2023 17:42:26 +0800 Subject: [PATCH] feat(otlp-exporter-base): supports gzip compression in the browser environment (#4162) --- experimental/CHANGELOG.md | 1 + .../src/platform/browser/OTLPLogExporter.ts | 4 +- .../test/browser/OTLPLogExporter.test.ts | 2 +- .../src/platform/browser/OTLPLogExporter.ts | 4 +- .../src/platform/browser/OTLPTraceExporter.ts | 4 +- .../browser/CollectorTraceExporter.test.ts | 9 +-- .../src/platform/browser/OTLPTraceExporter.ts | 4 +- .../platform/browser/OTLPMetricExporter.ts | 4 +- .../browser/CollectorMetricExporter.test.ts | 6 +- .../packages/otlp-exporter-base/package.json | 4 +- .../browser/OTLPExporterBrowserBase.ts | 21 ++---- .../src/platform/browser/util.ts | 42 +++++++---- .../src/platform/node/OTLPExporterNodeBase.ts | 3 +- .../src/platform/node/index.ts | 6 +- .../src/platform/node/types.ts | 6 -- .../src/platform/node/util.ts | 4 +- .../packages/otlp-exporter-base/src/types.ts | 6 ++ .../test/browser/util.test.ts | 73 ++++++++++++++++--- .../otlp-exporter-base/test/node/util.test.ts | 2 +- .../otlp-proto-exporter-base/package.json | 4 +- .../browser/OTLPProtoExporterBrowserBase.ts | 18 ++--- 21 files changed, 142 insertions(+), 85 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 11e98847da..209dee43fb 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to experimental packages in this project will be documented * feat(exporter-metrics-otlp-proto): add esm build [#4099](https://github.com/open-telemetry/opentelemetry-js/pull/4099) @pichlermarc * feat(opencensus-shim): implement OpenCensus metric producer [#4066](https://github.com/open-telemetry/opentelemetry-js/pull/4066) @aabmass +* feat(otlp-exporter-base): supports gzip compression in the browser environment [#4162](https://github.com/open-telemetry/opentelemetry-js/pull/4162) @fuaiyi ### :bug: (Bug Fix) diff --git a/experimental/packages/exporter-logs-otlp-http/src/platform/browser/OTLPLogExporter.ts b/experimental/packages/exporter-logs-otlp-http/src/platform/browser/OTLPLogExporter.ts index a7ecbbac95..59cf0873ff 100644 --- a/experimental/packages/exporter-logs-otlp-http/src/platform/browser/OTLPLogExporter.ts +++ b/experimental/packages/exporter-logs-otlp-http/src/platform/browser/OTLPLogExporter.ts @@ -39,8 +39,8 @@ export class OTLPLogExporter timeoutMillis: getEnv().OTEL_EXPORTER_OTLP_LOGS_TIMEOUT, ...config, }); - this._headers = { - ...this._headers, + this.headers = { + ...this.headers, ...baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS ), diff --git a/experimental/packages/exporter-logs-otlp-http/test/browser/OTLPLogExporter.test.ts b/experimental/packages/exporter-logs-otlp-http/test/browser/OTLPLogExporter.test.ts index 2443c97ef4..8de188155b 100644 --- a/experimental/packages/exporter-logs-otlp-http/test/browser/OTLPLogExporter.test.ts +++ b/experimental/packages/exporter-logs-otlp-http/test/browser/OTLPLogExporter.test.ts @@ -47,7 +47,7 @@ describe('OTLPLogExporter', () => { it('should use headers defined via env', () => { envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS = 'foo=bar'; const exporter = new OTLPLogExporter(); - assert.strictEqual(exporter['_headers'].foo, 'bar'); + assert.strictEqual(exporter['headers'].foo, 'bar'); delete envSource.OTEL_EXPORTER_OTLP_LOGS_HEADERS; }); diff --git a/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts b/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts index fff28d4e77..4a8b710322 100644 --- a/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts +++ b/experimental/packages/exporter-logs-otlp-proto/src/platform/browser/OTLPLogExporter.ts @@ -46,8 +46,8 @@ export class OTLPLogExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config); - this._headers = Object.assign( - this._headers, + this.headers = Object.assign( + this.headers, baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS ) diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts index 8a343306c4..cc93e126ff 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts @@ -39,8 +39,8 @@ export class OTLPTraceExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config); - this._headers = Object.assign( - this._headers, + this.headers = Object.assign( + this.headers, baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS ) diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts index 4e8bc1d6e1..6ed2ecc362 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts @@ -566,18 +566,15 @@ describe('when configuring via environment', () => { it('should use headers defined via env', () => { envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar'; const collectorExporter = new OTLPTraceExporter({ headers: {} }); - // @ts-expect-error access internal property for testing - assert.strictEqual(collectorExporter._headers.foo, 'bar'); + assert.strictEqual(collectorExporter.headers.foo, 'bar'); envSource.OTEL_EXPORTER_OTLP_HEADERS = ''; }); it('should override global headers config with signal headers defined via env', () => { envSource.OTEL_EXPORTER_OTLP_HEADERS = 'foo=bar,bar=foo'; envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = 'foo=boo'; const collectorExporter = new OTLPTraceExporter({ headers: {} }); - // @ts-expect-error access internal property for testing - assert.strictEqual(collectorExporter._headers.foo, 'boo'); - // @ts-expect-error access internal property for testing - assert.strictEqual(collectorExporter._headers.bar, 'foo'); + assert.strictEqual(collectorExporter.headers.foo, 'boo'); + assert.strictEqual(collectorExporter.headers.bar, 'foo'); envSource.OTEL_EXPORTER_OTLP_TRACES_HEADERS = ''; envSource.OTEL_EXPORTER_OTLP_HEADERS = ''; }); diff --git a/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts index 890268333f..f2687361d7 100644 --- a/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-proto/src/platform/browser/OTLPTraceExporter.ts @@ -42,8 +42,8 @@ export class OTLPTraceExporter { constructor(config: OTLPExporterConfigBase = {}) { super(config); - this._headers = Object.assign( - this._headers, + this.headers = Object.assign( + this.headers, baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS ) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts index 73c99a0693..89722e553d 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts @@ -38,8 +38,8 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase< > { constructor(config?: OTLPMetricExporterOptions & OTLPExporterConfigBase) { super(config); - this._headers = Object.assign( - this._headers, + this.headers = Object.assign( + this.headers, baggageUtils.parseKeyPairsIntoRecord( getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS ) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts index 053376b032..acff4a451e 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts @@ -521,7 +521,7 @@ describe('when configuring via environment', () => { temporalityPreference: AggregationTemporalityPreference.CUMULATIVE, }); assert.strictEqual( - collectorExporter['_otlpExporter']['_headers'].foo, + collectorExporter['_otlpExporter']['headers'].foo, 'bar' ); envSource.OTEL_EXPORTER_OTLP_HEADERS = ''; @@ -534,11 +534,11 @@ describe('when configuring via environment', () => { temporalityPreference: AggregationTemporalityPreference.CUMULATIVE, }); assert.strictEqual( - collectorExporter['_otlpExporter']['_headers'].foo, + collectorExporter['_otlpExporter']['headers'].foo, 'boo' ); assert.strictEqual( - collectorExporter['_otlpExporter']['_headers'].bar, + collectorExporter['_otlpExporter']['headers'].bar, 'foo' ); envSource.OTEL_EXPORTER_OTLP_METRICS_HEADERS = ''; diff --git a/experimental/packages/otlp-exporter-base/package.json b/experimental/packages/otlp-exporter-base/package.json index 7981365842..5f14133185 100644 --- a/experimental/packages/otlp-exporter-base/package.json +++ b/experimental/packages/otlp-exporter-base/package.json @@ -61,7 +61,8 @@ "access": "public" }, "dependencies": { - "@opentelemetry/core": "1.17.0" + "@opentelemetry/core": "1.17.0", + "pako": "^2.1.0" }, "devDependencies": { "@babel/core": "7.22.20", @@ -69,6 +70,7 @@ "@types/mocha": "10.0.1", "@types/node": "18.6.5", "@types/sinon": "10.0.16", + "@types/pako": "^2.0.0", "babel-plugin-istanbul": "6.1.1", "codecov": "3.8.3", "cross-var": "1.1.0", diff --git a/experimental/packages/otlp-exporter-base/src/platform/browser/OTLPExporterBrowserBase.ts b/experimental/packages/otlp-exporter-base/src/platform/browser/OTLPExporterBrowserBase.ts index 2888e317df..97d609920e 100644 --- a/experimental/packages/otlp-exporter-base/src/platform/browser/OTLPExporterBrowserBase.ts +++ b/experimental/packages/otlp-exporter-base/src/platform/browser/OTLPExporterBrowserBase.ts @@ -15,10 +15,10 @@ */ import { OTLPExporterBase } from '../../OTLPExporterBase'; -import { OTLPExporterConfigBase } from '../../types'; +import { CompressionAlgorithm, OTLPExporterConfigBase } from '../../types'; import * as otlpTypes from '../../types'; import { parseHeaders } from '../../util'; -import { sendWithBeacon, sendWithXhr } from './util'; +import { sendWithBeacon, sendWithXhr, configureCompression } from './util'; import { diag } from '@opentelemetry/api'; import { getEnv, baggageUtils } from '@opentelemetry/core'; @@ -29,7 +29,8 @@ export abstract class OTLPExporterBrowserBase< ExportItem, ServiceRequest, > extends OTLPExporterBase { - protected _headers: Record; + headers: Record; + compression: CompressionAlgorithm; private _useXHR: boolean = false; /** @@ -40,7 +41,7 @@ export abstract class OTLPExporterBrowserBase< this._useXHR = !!config.headers || typeof navigator.sendBeacon !== 'function'; if (this._useXHR) { - this._headers = Object.assign( + this.headers = Object.assign( {}, parseHeaders(config.headers), baggageUtils.parseKeyPairsIntoRecord( @@ -48,8 +49,9 @@ export abstract class OTLPExporterBrowserBase< ) ); } else { - this._headers = {}; + this.headers = {}; } + this.compression = configureCompression(config.compression); } onInit(): void { @@ -74,14 +76,7 @@ export abstract class OTLPExporterBrowserBase< const promise = new Promise((resolve, reject) => { if (this._useXHR) { - sendWithXhr( - body, - this.url, - this._headers, - this.timeoutMillis, - resolve, - reject - ); + sendWithXhr(this, body, 'application/json', resolve, reject); } else { sendWithBeacon( body, diff --git a/experimental/packages/otlp-exporter-base/src/platform/browser/util.ts b/experimental/packages/otlp-exporter-base/src/platform/browser/util.ts index fade4afa88..c2665e050b 100644 --- a/experimental/packages/otlp-exporter-base/src/platform/browser/util.ts +++ b/experimental/packages/otlp-exporter-base/src/platform/browser/util.ts @@ -14,7 +14,8 @@ * limitations under the License. */ import { diag } from '@opentelemetry/api'; -import { OTLPExporterError } from '../../types'; +import { gzip } from 'pako'; +import { CompressionAlgorithm, OTLPExporterError } from '../../types'; import { DEFAULT_EXPORT_MAX_ATTEMPTS, DEFAULT_EXPORT_INITIAL_BACKOFF, @@ -23,6 +24,7 @@ import { isExportRetryable, parseRetryAfterToMills, } from '../../util'; +import { OTLPExporterBrowserBase } from './OTLPExporterBrowserBase'; /** * Send metrics/spans using browser navigator.sendBeacon @@ -51,17 +53,16 @@ export function sendWithBeacon( /** * function to send metrics/spans using browser XMLHttpRequest * used when navigator.sendBeacon is not available + * @param collector * @param body - * @param url - * @param headers + * @param contentType * @param onSuccess * @param onError */ -export function sendWithXhr( - body: string | Blob, - url: string, - headers: Record, - exporterTimeout: number, +export function sendWithXhr( + collector: OTLPExporterBrowserBase, + body: string | Uint8Array, + contentType: string, onSuccess: () => void, onError: (error: OTLPExporterError) => void ): void { @@ -79,28 +80,37 @@ export function sendWithXhr( } else { xhr.abort(); } - }, exporterTimeout); + }, collector.timeoutMillis); const sendWithRetry = ( retries = DEFAULT_EXPORT_MAX_ATTEMPTS, minDelay = DEFAULT_EXPORT_INITIAL_BACKOFF ) => { xhr = new XMLHttpRequest(); - xhr.open('POST', url); + xhr.open('POST', collector.url); const defaultHeaders = { Accept: 'application/json', - 'Content-Type': 'application/json', + 'Content-Type': contentType || 'application/json', }; Object.entries({ ...defaultHeaders, - ...headers, + ...collector.headers, }).forEach(([k, v]) => { xhr.setRequestHeader(k, v); }); - xhr.send(body); + switch (collector.compression) { + case CompressionAlgorithm.GZIP: { + xhr.setRequestHeader('Content-Encoding', 'gzip'); + xhr.send(gzip(body)); + break; + } + default: + xhr.send(body); + break; + } xhr.onreadystatechange = () => { if (xhr.readyState === XMLHttpRequest.DONE && reqIsDestroyed === false) { @@ -161,3 +171,9 @@ export function sendWithXhr( sendWithRetry(); } + +export function configureCompression( + compression: CompressionAlgorithm | undefined +): CompressionAlgorithm { + return compression || CompressionAlgorithm.NONE; +} diff --git a/experimental/packages/otlp-exporter-base/src/platform/node/OTLPExporterNodeBase.ts b/experimental/packages/otlp-exporter-base/src/platform/node/OTLPExporterNodeBase.ts index 5a8b1dfdfa..505f1c851d 100644 --- a/experimental/packages/otlp-exporter-base/src/platform/node/OTLPExporterNodeBase.ts +++ b/experimental/packages/otlp-exporter-base/src/platform/node/OTLPExporterNodeBase.ts @@ -18,12 +18,13 @@ import type * as http from 'http'; import type * as https from 'https'; import { OTLPExporterBase } from '../../OTLPExporterBase'; -import { OTLPExporterNodeConfigBase, CompressionAlgorithm } from './types'; +import { OTLPExporterNodeConfigBase } from './types'; import * as otlpTypes from '../../types'; import { parseHeaders } from '../../util'; import { createHttpAgent, sendWithHttp, configureCompression } from './util'; import { diag } from '@opentelemetry/api'; import { getEnv, baggageUtils } from '@opentelemetry/core'; +import { CompressionAlgorithm } from '../../types'; /** * Collector Metric Exporter abstract base class diff --git a/experimental/packages/otlp-exporter-base/src/platform/node/index.ts b/experimental/packages/otlp-exporter-base/src/platform/node/index.ts index b8b13bda20..bdd2e95c3f 100644 --- a/experimental/packages/otlp-exporter-base/src/platform/node/index.ts +++ b/experimental/packages/otlp-exporter-base/src/platform/node/index.ts @@ -15,5 +15,7 @@ */ export { OTLPExporterNodeBase } from './OTLPExporterNodeBase'; -export { sendWithHttp, createHttpAgent, configureCompression } from './util'; -export { OTLPExporterNodeConfigBase, CompressionAlgorithm } from './types'; +export { sendWithHttp, createHttpAgent } from './util'; +export { configureCompression } from './util'; +export { OTLPExporterNodeConfigBase } from './types'; +export { CompressionAlgorithm } from '../../types'; diff --git a/experimental/packages/otlp-exporter-base/src/platform/node/types.ts b/experimental/packages/otlp-exporter-base/src/platform/node/types.ts index b1e355de2d..19226b8107 100644 --- a/experimental/packages/otlp-exporter-base/src/platform/node/types.ts +++ b/experimental/packages/otlp-exporter-base/src/platform/node/types.ts @@ -23,11 +23,5 @@ import { OTLPExporterConfigBase } from '../../types'; */ export interface OTLPExporterNodeConfigBase extends OTLPExporterConfigBase { keepAlive?: boolean; - compression?: CompressionAlgorithm; httpAgentOptions?: http.AgentOptions | https.AgentOptions; } - -export enum CompressionAlgorithm { - NONE = 'none', - GZIP = 'gzip', -} diff --git a/experimental/packages/otlp-exporter-base/src/platform/node/util.ts b/experimental/packages/otlp-exporter-base/src/platform/node/util.ts index fd40981e85..8adfbd8dfe 100644 --- a/experimental/packages/otlp-exporter-base/src/platform/node/util.ts +++ b/experimental/packages/otlp-exporter-base/src/platform/node/util.ts @@ -21,9 +21,9 @@ import { Readable } from 'stream'; import { OTLPExporterNodeBase } from './OTLPExporterNodeBase'; import { OTLPExporterNodeConfigBase } from '.'; import { diag } from '@opentelemetry/api'; -import { CompressionAlgorithm } from './types'; +import { CompressionAlgorithm, OTLPExporterError } from '../../types'; import { getEnv } from '@opentelemetry/core'; -import { OTLPExporterError } from '../../types'; + import { DEFAULT_EXPORT_MAX_ATTEMPTS, DEFAULT_EXPORT_INITIAL_BACKOFF, diff --git a/experimental/packages/otlp-exporter-base/src/types.ts b/experimental/packages/otlp-exporter-base/src/types.ts index a8ee1db743..b5df583092 100644 --- a/experimental/packages/otlp-exporter-base/src/types.ts +++ b/experimental/packages/otlp-exporter-base/src/types.ts @@ -49,7 +49,13 @@ export interface OTLPExporterConfigBase { hostname?: string; url?: string; concurrencyLimit?: number; + compression?: CompressionAlgorithm; /** Maximum time the OTLP exporter will wait for each batch export. * The default value is 10000ms. */ timeoutMillis?: number; } + +export enum CompressionAlgorithm { + NONE = 'none', + GZIP = 'gzip', +} diff --git a/experimental/packages/otlp-exporter-base/test/browser/util.test.ts b/experimental/packages/otlp-exporter-base/test/browser/util.test.ts index 1dd3b77d58..87710e125d 100644 --- a/experimental/packages/otlp-exporter-base/test/browser/util.test.ts +++ b/experimental/packages/otlp-exporter-base/test/browser/util.test.ts @@ -15,13 +15,17 @@ */ import * as sinon from 'sinon'; +import * as assert from 'assert'; +import { gzip } from 'pako'; import { sendWithXhr } from '../../src/platform/browser/util'; import { nextTick } from 'process'; import { ensureHeadersContain } from '../testHelper'; +import { OTLPExporterBrowserBase } from '../../src/platform/browser'; +import { CompressionAlgorithm } from '../../src/types'; describe('util - browser', () => { let server: any; - const body = ''; + const body = 'test body'; const url = ''; let onSuccessStub: sinon.SinonStub; @@ -54,15 +58,16 @@ describe('util - browser', () => { }); describe('and Content-Type header is set', () => { beforeEach(() => { - const explicitContentType = { - 'Content-Type': 'application/json', - }; + const explicitContentType = 'application/json'; const exporterTimeout = 10000; sendWithXhr( + { + headers: {}, + url, + timeoutMillis: exporterTimeout, + } as OTLPExporterBrowserBase, body, - url, explicitContentType, - exporterTimeout, onSuccessStub, onErrorStub ); @@ -91,10 +96,13 @@ describe('util - browser', () => { // use default exporter timeout const exporterTimeout = 10000; sendWithXhr( + { + headers: emptyHeaders, + url: url, + timeoutMillis: exporterTimeout, + } as OTLPExporterBrowserBase, body, - url, - emptyHeaders, - exporterTimeout, + '', onSuccessStub, onErrorStub ); @@ -116,16 +124,20 @@ describe('util - browser', () => { }); }); }); + describe('and custom headers are set', () => { let customHeaders: Record; beforeEach(() => { customHeaders = { aHeader: 'aValue', bHeader: 'bValue' }; const exporterTimeout = 10000; sendWithXhr( + { + headers: customHeaders, + url, + timeoutMillis: exporterTimeout, + } as OTLPExporterBrowserBase, body, - url, - customHeaders, - exporterTimeout, + '', onSuccessStub, onErrorStub ); @@ -155,5 +167,42 @@ describe('util - browser', () => { }); }); }); + + describe('and compression is set to "gzip"', () => { + beforeEach(() => { + const compression = CompressionAlgorithm.GZIP; + const exporterTimeout = 10000; + sendWithXhr( + { + headers: {}, + url, + timeoutMillis: exporterTimeout, + compression, + } as OTLPExporterBrowserBase, + body, + '', + onSuccessStub, + onErrorStub + ); + }); + + it('Request Headers should contain "Content-Encoding" header', done => { + nextTick(() => { + const { requestHeaders } = server.requests[0]; + ensureHeadersContain(requestHeaders, expectedHeaders); + clock.restore(); + done(); + }); + }); + + it('Request Body should be compressed by gzip', done => { + nextTick(() => { + const { requestBody } = server.requests[0]; + assert.deepStrictEqual(requestBody, gzip(body)); + clock.restore(); + done(); + }); + }); + }); }); }); diff --git a/experimental/packages/otlp-exporter-base/test/node/util.test.ts b/experimental/packages/otlp-exporter-base/test/node/util.test.ts index b279e57b9a..8d81bf7e7c 100644 --- a/experimental/packages/otlp-exporter-base/test/node/util.test.ts +++ b/experimental/packages/otlp-exporter-base/test/node/util.test.ts @@ -17,7 +17,7 @@ import * as assert from 'assert'; import { configureExporterTimeout, invalidTimeout } from '../../src/util'; import { sendWithHttp } from '../../src/platform/node/util'; -import { CompressionAlgorithm } from '../../src/platform/node/types'; +import { CompressionAlgorithm } from '../../src/types'; import { configureCompression } from '../../src/platform/node/util'; import { diag } from '@opentelemetry/api'; import * as sinon from 'sinon'; diff --git a/experimental/packages/otlp-proto-exporter-base/package.json b/experimental/packages/otlp-proto-exporter-base/package.json index d350c263f6..816aca3298 100644 --- a/experimental/packages/otlp-proto-exporter-base/package.json +++ b/experimental/packages/otlp-proto-exporter-base/package.json @@ -64,6 +64,7 @@ "@types/mocha": "10.0.1", "@types/node": "18.6.5", "@types/sinon": "10.0.16", + "@types/pako": "^2.0.0", "codecov": "3.8.3", "cross-var": "1.1.0", "lerna": "7.1.5", @@ -81,7 +82,8 @@ "dependencies": { "@opentelemetry/core": "1.17.0", "@opentelemetry/otlp-exporter-base": "0.43.0", - "protobufjs": "^7.2.3" + "protobufjs": "^7.2.3", + "pako": "^2.1.0" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/otlp-proto-exporter-base", "sideEffects": false diff --git a/experimental/packages/otlp-proto-exporter-base/src/platform/browser/OTLPProtoExporterBrowserBase.ts b/experimental/packages/otlp-proto-exporter-base/src/platform/browser/OTLPProtoExporterBrowserBase.ts index 4bc9e5c70a..41e2bd677f 100644 --- a/experimental/packages/otlp-proto-exporter-base/src/platform/browser/OTLPProtoExporterBrowserBase.ts +++ b/experimental/packages/otlp-proto-exporter-base/src/platform/browser/OTLPProtoExporterBrowserBase.ts @@ -33,7 +33,10 @@ export abstract class OTLPProtoExporterBrowserBase< ServiceRequest, > extends OTLPExporterBaseMain { constructor(config: OTLPExporterConfigBase = {}) { - super(config); + super({ + headers: { Accept: 'application/x-protobuf', ...config.headers }, + ...config, + }); } override send( @@ -55,18 +58,7 @@ export abstract class OTLPProtoExporterBrowserBase< if (message) { const body = exportRequestType.encode(message).finish(); if (body) { - sendWithXhr( - new Blob([body], { type: 'application/x-protobuf' }), - this.url, - { - ...this._headers, - 'Content-Type': 'application/x-protobuf', - Accept: 'application/x-protobuf', - }, - this.timeoutMillis, - onSuccess, - onError - ); + sendWithXhr(this, body, 'application/x-protobuf', onSuccess, onError); } } else { onError(new OTLPExporterError('No proto'));