Skip to content

Commit

Permalink
chore: use Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
sugi committed Oct 19, 2023
1 parent 975f9f9 commit da0c607
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import { sendWithBeacon, sendWithFetch, sendWithXhr } from './util';
import { diag } from '@opentelemetry/api';
import { getEnv, baggageUtils, _globalThis } from '@opentelemetry/core';

enum SendMethod {
beacon = 1,
xhr = 2,
fetch = 3,
}

/**
* Collector Metric Exporter abstract base class
*/
Expand All @@ -30,21 +36,21 @@ export abstract class OTLPExporterBrowserBase<
ServiceRequest,
> extends OTLPExporterBase<OTLPExporterConfigBase, ExportItem, ServiceRequest> {
protected _headers: Record<string, string>;
private sendMethod: 'beacon' | 'xhr' | 'fetch' = 'beacon';
private sendMethod: SendMethod;

/**
* @param config
*/
constructor(config: OTLPExporterConfigBase = {}) {
super(config);
if (!config.headers && typeof navigator.sendBeacon === 'function') {
this.sendMethod = 'beacon';
this.sendMethod = SendMethod.beacon
} else if (typeof XMLHttpRequest === 'function') {
this.sendMethod = 'xhr';
this.sendMethod = SendMethod.xhr
} else {
this.sendMethod = 'fetch';
this.sendMethod = SendMethod.fetch;
}
if (this.sendMethod !== 'beacon') {
if (this.sendMethod !== SendMethod.beacon) {
this._headers = Object.assign(
{},
parseHeaders(config.headers),
Expand Down Expand Up @@ -78,7 +84,7 @@ export abstract class OTLPExporterBrowserBase<
const body = JSON.stringify(serviceRequest);

const promise = new Promise<void>((resolve, reject) => {
if (this.sendMethod === 'xhr') {
if (this.sendMethod === SendMethod.xhr) {
sendWithXhr(
body,
this.url,
Expand All @@ -87,7 +93,7 @@ export abstract class OTLPExporterBrowserBase<
resolve,
reject
);
} else if (this.sendMethod === 'fetch') {
} else if (this.sendMethod === SendMethod.fetch) {
sendWithFetch(
body,
this.url,
Expand All @@ -96,7 +102,7 @@ export abstract class OTLPExporterBrowserBase<
resolve,
reject
);
} else if (this.sendMethod === 'beacon') {
} else if (this.sendMethod === SendMethod.beacon) {
sendWithBeacon(
body,
this.url,
Expand Down

0 comments on commit da0c607

Please sign in to comment.