Skip to content

Commit

Permalink
fix(@opentelemetry/exporter-collector): remove fulfilled promises cor…
Browse files Browse the repository at this point in the history
…rectly
  • Loading branch information
aabmass committed Feb 27, 2021
1 parent 8af4444 commit 37ecc80
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 71 deletions.
Expand Up @@ -55,25 +55,17 @@ export abstract class CollectorExporterNodeBase<
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
): void {
const promise = new Promise<void>(resolve => {
const _onSuccess = (): void => {
onSuccess();
_onFinish();
};
const _onError = (error: collectorTypes.CollectorExporterError): void => {
onError(error);
_onFinish();
};
const _onFinish = () => {
resolve();
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
};

this._send(this, objects, _onSuccess, _onError);
});
const promise = new Promise<void>((resolve, reject) => {
this._send(this, objects, resolve, reject);
})
.then(onSuccess)
.catch(onError);

this._sendingPromises.push(promise);
promise.finally(() => {
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
});
}

onInit(config: CollectorExporterConfigNode): void {
Expand Down
Expand Up @@ -36,25 +36,17 @@ export abstract class CollectorExporterNodeBase<
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
): void {
const promise = new Promise<void>(resolve => {
const _onSuccess = (): void => {
onSuccess();
_onFinish();
};
const _onError = (error: collectorTypes.CollectorExporterError): void => {
onError(error);
_onFinish();
};
const _onFinish = () => {
resolve();
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
};

this._send(this, objects, _onSuccess, _onError);
});
const promise = new Promise<void>((resolve, reject) => {
this._send(this, objects, resolve, reject);
})
.then(onSuccess)
.catch(onError);

this._sendingPromises.push(promise);
promise.finally(() => {
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
});
}

onInit(config: CollectorExporterNodeConfigBase): void {
Expand Down
Expand Up @@ -69,27 +69,20 @@ export abstract class CollectorExporterBrowserBase<
const serviceRequest = this.convert(items);
const body = JSON.stringify(serviceRequest);

const promise = new Promise<void>(resolve => {
const _onSuccess = (): void => {
onSuccess();
_onFinish();
};
const _onError = (error: collectorTypes.CollectorExporterError): void => {
onError(error);
_onFinish();
};
const _onFinish = () => {
resolve();
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
};

const promise = new Promise<void>((resolve, reject) => {
if (this._useXHR) {
sendWithXhr(body, this.url, this._headers, _onSuccess, _onError);
sendWithXhr(body, this.url, this._headers, resolve, reject);
} else {
sendWithBeacon(body, this.url, _onSuccess, _onError);
sendWithBeacon(body, this.url, resolve, reject);
}
});
})
.then(onSuccess)
.catch(onError);

this._sendingPromises.push(promise);
promise.finally(() => {
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
});
}
}
Expand Up @@ -62,30 +62,23 @@ export abstract class CollectorExporterNodeBase<
}
const serviceRequest = this.convert(objects);

const promise = new Promise<void>(resolve => {
const _onSuccess = (): void => {
onSuccess();
_onFinish();
};
const _onError = (error: collectorTypes.CollectorExporterError): void => {
onError(error);
_onFinish();
};
const _onFinish = () => {
resolve();
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
};
const promise = new Promise<void>((resolve, reject) => {
sendWithHttp(
this,
JSON.stringify(serviceRequest),
'application/json',
_onSuccess,
_onError
resolve,
reject
);
});
})
.then(onSuccess)
.catch(onError);

this._sendingPromises.push(promise);
promise.finally(() => {
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
});
}

onShutdown(): void {}
Expand Down
7 changes: 5 additions & 2 deletions packages/opentelemetry-exporter-zipkin/src/zipkin.ts
Expand Up @@ -76,11 +76,14 @@ export class ZipkinExporter implements SpanExporter {
this._sendSpans(spans, this._serviceName!, result => {
resolve();
resultCallback(result);
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
});
});

this._sendingPromises.push(promise);
promise.finally(() => {
const index = this._sendingPromises.indexOf(promise);
this._sendingPromises.splice(index, 1);
});
}

/**
Expand Down

0 comments on commit 37ecc80

Please sign in to comment.