Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use Blob in sendBeacon to add application/json type #2336

Merged
merged 14 commits into from
Aug 10, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { sendWithBeacon, sendWithXhr } from './util';
import { diag } from '@opentelemetry/api';
import { getEnv, baggageUtils } from '@opentelemetry/core';

const DEFAULT_BLOB_PROPERTY_BAG: BlobPropertyBag = { type: 'application/json' };

/**
* Collector Metric Exporter abstract base class
*/
Expand All @@ -35,7 +37,7 @@ export abstract class CollectorExporterBrowserBase<
> {
protected _headers: Record<string, string>;
private _useXHR: boolean = false;
private _contentType: string = '';
private _blobPropertBag: BlobPropertyBag = {};

/**
* @param config
Expand All @@ -54,7 +56,10 @@ export abstract class CollectorExporterBrowserBase<
);
} else {
this._headers = {};
this._contentType = config.contentTypeBeacon ?? 'application/json';
this._blobPropertBag = {
MSNev marked this conversation as resolved.
Show resolved Hide resolved
...DEFAULT_BLOB_PROPERTY_BAG,
...config.beaconBlobPropertyBag
}
}
}

Expand All @@ -70,7 +75,7 @@ export abstract class CollectorExporterBrowserBase<
items: ExportItem[],
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
) : void {
): void {
if (this._isShutdown) {
diag.debug('Shutdown already started. Cannot send objects');
return;
Expand All @@ -96,7 +101,7 @@ export abstract class CollectorExporterBrowserBase<
if (this._useXHR) {
sendWithXhr(body, this.url, this._headers, _onSuccess, _onError);
} else {
sendWithBeacon(body, this.url, this._contentType, _onSuccess, _onError);
sendWithBeacon(body, this.url, this._blobPropertBag, _onSuccess, _onError);
MSNev marked this conversation as resolved.
Show resolved Hide resolved
}
});
this._sendingPromises.push(promise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import * as collectorTypes from '../../types';
export function sendWithBeacon(
body: string,
url: string,
contentType: string,
blobPropertyBag: BlobPropertyBag,
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
): void {
contentType = contentType !== '' ? contentType : 'application/json';
if (navigator.sendBeacon(url, new Blob([body], { type: contentType }))) {
if (navigator.sendBeacon(url, new Blob([body], blobPropertyBag))) {
diag.debug('sendBeacon - can send', body);
onSuccess();
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-exporter-collector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export interface CollectorExporterConfigBase {
attributes?: SpanAttributes;
url?: string;
concurrencyLimit?: number;
contentTypeBeacon?:string;
beaconBlobPropertyBag?: BlobPropertyBag;
jufab marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('CollectorTraceExporter - web', () => {
hostname: 'foo',
attributes: {},
url: 'http://foo.bar.com',
contentTypeBeacon: 'text/plain',
beaconBlobPropertyBag: { type: 'text/plain' },
};
});

Expand Down Expand Up @@ -98,7 +98,7 @@ describe('CollectorTraceExporter - web', () => {

assert.strictEqual(stubOpen.callCount, 0);

assert.strictEqual(header, collectorExporterConfig.contentTypeBeacon);
assert.strictEqual(header, collectorExporterConfig.beaconBlobPropertyBag?.type);
jufab marked this conversation as resolved.
Show resolved Hide resolved

ensureExportTraceServiceRequestIsSet(json);

Expand Down