Skip to content

Commit

Permalink
Merge 73c873b into e089984
Browse files Browse the repository at this point in the history
  • Loading branch information
niko-achilles authored Jul 27, 2021
2 parents e089984 + 73c873b commit 9afe781
Show file tree
Hide file tree
Showing 6 changed files with 928 additions and 361 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export abstract class CollectorExporterBrowserBase<
ExportItem,
ServiceRequest
> {
private DEFAULT_HEADERS = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
private DEFAULT_BLOB_TYPE = { type: 'application/json' };

protected _headers: Record<string, string>;
private _useXHR: boolean = false;

Expand All @@ -43,16 +49,20 @@ export abstract class CollectorExporterBrowserBase<
super(config);
this._useXHR =
!!config.headers || typeof navigator.sendBeacon !== 'function';

if (this._useXHR) {
this._headers = Object.assign(
{},
parseHeaders(config.headers),
baggageUtils.parseKeyPairsIntoRecord(
this._headers = {
...this.DEFAULT_HEADERS,
...parseHeaders(config.headers),
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_HEADERS
)
);
),
};
} else {
this._headers = {};
this._headers = {
...this.DEFAULT_BLOB_TYPE,
...parseHeaders({ type: config.blobType }),
};
}
}

Expand Down Expand Up @@ -94,7 +104,7 @@ export abstract class CollectorExporterBrowserBase<
if (this._useXHR) {
sendWithXhr(body, this.url, this._headers, _onSuccess, _onError);
} else {
sendWithBeacon(body, this.url, _onSuccess, _onError);
sendWithBeacon(body, this.url, this._headers, _onSuccess, _onError);
}
});
this._sendingPromises.push(promise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import * as collectorTypes from '../../types';
export function sendWithBeacon(
body: string,
url: string,
headers: Record<string, string>,
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
) {
if (navigator.sendBeacon(url, body)) {
const blob = new Blob([body], headers);
if (navigator.sendBeacon(url, blob)) {
diag.debug('sendBeacon - can send', body);
onSuccess();
} else {
Expand Down Expand Up @@ -56,13 +58,7 @@ export function sendWithXhr(
const xhr = new XMLHttpRequest();
xhr.open('POST', url);

const defaultHeaders = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};

Object.entries({
...defaultHeaders,
...headers,
}).forEach(([k, v]) => {
xhr.setRequestHeader(k, v);
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-exporter-collector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ export interface ExportServiceError {
* Collector Exporter base config
*/
export interface CollectorExporterConfigBase {
/**
* type of the data contained in the Blob
*/
blobType?: string;
headers?: Partial<Record<string, unknown>>;
hostname?: string;
attributes?: SpanAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ describe('CollectorMetricExporter - web', () => {
},
'double-observer2'
);
const recorder: Metric<BoundValueRecorder> &
ValueRecorder = mockValueRecorder();
const recorder: Metric<BoundValueRecorder> & ValueRecorder =
mockValueRecorder();
counter.add(1);
recorder.record(7);
recorder.record(14);
Expand Down Expand Up @@ -93,10 +93,11 @@ describe('CollectorMetricExporter - web', () => {
it('should successfully send metrics using sendBeacon', done => {
collectorExporter.export(metrics, () => {});

setTimeout(() => {
setTimeout(async () => {
const args = stubBeacon.args[0];
const url = args[0];
const body = args[1];
const blob: Blob = args[1];
const body = await blob.text();
const json = JSON.parse(
body
) as collectorTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest;
Expand Down Expand Up @@ -158,6 +159,22 @@ describe('CollectorMetricExporter - web', () => {
});
});

it('should successfully send blob type using sendBeacon', done => {
collectorExporter.export(metrics, () => {});
const expectedType = 'application/json';

setTimeout(() => {
const args = stubBeacon.args[0];
const blob: Blob = args[1];
const { type } = blob;

assert.strictEqual(type, expectedType);
assert.strictEqual(stubBeacon.callCount, 1);
assert.strictEqual(stubOpen.callCount, 0);
done();
});
});

it('should log the successful message', done => {
// Need to stub/spy on the underlying logger as the "diag" instance is global
const spyLoggerDebug = sinon.stub(diag, 'debug');
Expand Down
Loading

0 comments on commit 9afe781

Please sign in to comment.