From e3ae7fdc39021e5284bbf387848baab37edb2881 Mon Sep 17 00:00:00 2001 From: ywangjl <51412839+ywangjl@users.noreply.github.com> Date: Tue, 10 May 2022 11:50:44 +0100 Subject: [PATCH] Add await to createTimeSeries so that onMetricUploadError can take effect Currently the error thrown by createTimeSeries cannot be captured by onMetricUploadError, because the code is not run under the same context as the try catch block without "await". Please see the following code illustration, toggle between "delayWithError(ms);" and "await delayWithError(ms);" to see the difference. Without await, the error is not caught and handled. //////////////////////////////////////////////////// async function delay(ms: number): Promise { return new Promise(resolve => { setTimeout(resolve, ms); }); } async function delayWithError(ms: number) { await delay(ms); throw("some error"); } async function test(ms: number) { console.log(`start first delay, waiting for ${ms}ms...`); await delay(ms); console.log("Done first delay"); console.log(`start second delay with error, waiting for ${ms}ms...`); // await delayWithError(ms); delayWithError(ms); } async function main() { const ms = 1000; try{ await test(ms); }catch(err){ console.log("caugh error:" + err); } } main(); ///////////////////////////////////////////////////// --- .../src/stackdriver-monitoring.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts b/packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts index b72ced042..1feabbf4b 100644 --- a/packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts +++ b/packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts @@ -129,7 +129,7 @@ export class StackdriverStatsExporter implements StatsEventListener { } } - this.createTimeSeries(metricsList); + await this.createTimeSeries(metricsList); } /**