Skip to content

Commit 3fc8bc9

Browse files
jufabdyladan
andauthored
feat: use Blob in sendBeacon to add application/json type (#2336)
* fix(opentelemetry-exporter-collector): issue#2321 use Blob for method sendBeacon to add type application/json * fix: test unit browser error * feat: add contentType configuration for beacon * fix: some lint * fix: use BlobPropertyBag for Blob * fix: assertion name * fix: blob on public sendWithBeacon only * Style nit Co-authored-by: Daniel Dyla <[email protected]>
1 parent eb3cd50 commit 3fc8bc9

File tree

7 files changed

+24
-21
lines changed

7 files changed

+24
-21
lines changed

packages/opentelemetry-exporter-collector/src/CollectorExporterBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export abstract class CollectorExporterBase<
6565
* @param items
6666
* @param resultCallback
6767
*/
68-
export(items: ExportItem[], resultCallback: (result: ExportResult) => void) {
68+
export(items: ExportItem[], resultCallback: (result: ExportResult) => void): void {
6969
if (this._isShutdown) {
7070
resultCallback({
7171
code: ExportResultCode.FAILED,

packages/opentelemetry-exporter-collector/src/platform/browser/CollectorExporterBrowserBase.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import { getEnv, baggageUtils } from '@opentelemetry/core';
2828
export abstract class CollectorExporterBrowserBase<
2929
ExportItem,
3030
ServiceRequest
31-
> extends CollectorExporterBase<
31+
> extends CollectorExporterBase<
3232
CollectorExporterConfigBase,
3333
ExportItem,
3434
ServiceRequest
35-
> {
35+
> {
3636
protected _headers: Record<string, string>;
3737
private _useXHR: boolean = false;
3838

@@ -68,7 +68,7 @@ export abstract class CollectorExporterBrowserBase<
6868
items: ExportItem[],
6969
onSuccess: () => void,
7070
onError: (error: collectorTypes.CollectorExporterError) => void
71-
) {
71+
): void {
7272
if (this._isShutdown) {
7373
diag.debug('Shutdown already started. Cannot send objects');
7474
return;
@@ -94,7 +94,7 @@ export abstract class CollectorExporterBrowserBase<
9494
if (this._useXHR) {
9595
sendWithXhr(body, this.url, this._headers, _onSuccess, _onError);
9696
} else {
97-
sendWithBeacon(body, this.url, _onSuccess, _onError);
97+
sendWithBeacon(body, this.url, { type: 'application/json' }, _onSuccess, _onError);
9898
}
9999
});
100100
this._sendingPromises.push(promise);

packages/opentelemetry-exporter-collector/src/platform/browser/CollectorTraceExporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class CollectorTraceExporter
4747
return toCollectorExportTraceServiceRequest(spans, this, true);
4848
}
4949

50-
getDefaultUrl(config: CollectorExporterConfigBase) {
50+
getDefaultUrl(config: CollectorExporterConfigBase): string {
5151
return typeof config.url === 'string'
5252
? config.url
5353
: getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0

packages/opentelemetry-exporter-collector/src/platform/browser/util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import * as collectorTypes from '../../types';
2525
export function sendWithBeacon(
2626
body: string,
2727
url: string,
28+
blobPropertyBag: BlobPropertyBag,
2829
onSuccess: () => void,
2930
onError: (error: collectorTypes.CollectorExporterError) => void
30-
) {
31-
if (navigator.sendBeacon(url, body)) {
31+
): void {
32+
if (navigator.sendBeacon(url, new Blob([body], blobPropertyBag))) {
3233
diag.debug('sendBeacon - can send', body);
3334
onSuccess();
3435
} else {
@@ -52,7 +53,7 @@ export function sendWithXhr(
5253
headers: Record<string, string>,
5354
onSuccess: () => void,
5455
onError: (error: collectorTypes.CollectorExporterError) => void
55-
) {
56+
): void {
5657
const xhr = new XMLHttpRequest();
5758
xhr.open('POST', url);
5859

packages/opentelemetry-exporter-collector/src/platform/node/CollectorTraceExporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class CollectorTraceExporter
4848
return toCollectorExportTraceServiceRequest(spans, this, true);
4949
}
5050

51-
getDefaultUrl(config: CollectorExporterNodeConfigBase) {
51+
getDefaultUrl(config: CollectorExporterNodeConfigBase) : string {
5252
return typeof config.url === 'string'
5353
? config.url
5454
: getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0

packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ describe('CollectorMetricExporter - web', () => {
9393
it('should successfully send metrics using sendBeacon', done => {
9494
collectorExporter.export(metrics, () => {});
9595

96-
setTimeout(() => {
96+
setTimeout(async () => {
9797
const args = stubBeacon.args[0];
9898
const url = args[0];
99-
const body = args[1];
99+
const blob: Blob = args[1];
100+
const body = await blob.text();
100101
const json = JSON.parse(
101102
body
102103
) as collectorTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest;

packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ describe('CollectorTraceExporter - web', () => {
6767
});
6868

6969
it('should successfully send the spans using sendBeacon', done => {
70-
collectorTraceExporter.export(spans, () => {});
70+
collectorTraceExporter.export(spans, () => { });
7171

72-
setTimeout(() => {
72+
setTimeout(async () => {
7373
const args = stubBeacon.args[0];
7474
const url = args[0];
75-
const body = args[1];
75+
const blob: Blob = args[1];
76+
const body = await blob.text();
7677
const json = JSON.parse(
7778
body
7879
) as collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest;
@@ -107,7 +108,7 @@ describe('CollectorTraceExporter - web', () => {
107108
const spyLoggerError = sinon.stub(diag, 'error');
108109
stubBeacon.returns(true);
109110

110-
collectorTraceExporter.export(spans, () => {});
111+
collectorTraceExporter.export(spans, () => { });
111112

112113
setTimeout(() => {
113114
const response: any = spyLoggerDebug.args[1][0];
@@ -143,7 +144,7 @@ describe('CollectorTraceExporter - web', () => {
143144
});
144145

145146
it('should successfully send the spans using XMLHttpRequest', done => {
146-
collectorTraceExporter.export(spans, () => {});
147+
collectorTraceExporter.export(spans, () => { });
147148

148149
setTimeout(() => {
149150
const request = server.requests[0];
@@ -181,7 +182,7 @@ describe('CollectorTraceExporter - web', () => {
181182
const spyLoggerDebug = sinon.stub(diag, 'debug');
182183
const spyLoggerError = sinon.stub(diag, 'error');
183184

184-
collectorTraceExporter.export(spans, () => {});
185+
collectorTraceExporter.export(spans, () => { });
185186

186187
setTimeout(() => {
187188
const request = server.requests[0];
@@ -210,7 +211,7 @@ describe('CollectorTraceExporter - web', () => {
210211
});
211212

212213
it('should send custom headers', done => {
213-
collectorTraceExporter.export(spans, () => {});
214+
collectorTraceExporter.export(spans, () => { });
214215

215216
setTimeout(() => {
216217
const request = server.requests[0];
@@ -248,7 +249,7 @@ describe('CollectorTraceExporter - web', () => {
248249
);
249250
});
250251
it('should successfully send custom headers using XMLHTTPRequest', done => {
251-
collectorTraceExporter.export(spans, () => {});
252+
collectorTraceExporter.export(spans, () => { });
252253

253254
setTimeout(() => {
254255
const [{ requestHeaders }] = server.requests;
@@ -271,7 +272,7 @@ describe('CollectorTraceExporter - web', () => {
271272
});
272273

273274
it('should successfully send spans using XMLHttpRequest', done => {
274-
collectorTraceExporter.export(spans, () => {});
275+
collectorTraceExporter.export(spans, () => { });
275276

276277
setTimeout(() => {
277278
const [{ requestHeaders }] = server.requests;

0 commit comments

Comments
 (0)