What version of OpenCensus are you using?
0.1.0
What version of Node are you using?
18.7.0
What did you do?
If possible, provide a recipe for reproducing the error.
Have code that looks like this inside a Google Cloud Function:
functions.cloudEvent('lg-stock-check', async () => {
// Omitted code...
const exporter = new StackdriverStatsExporter({
projectId: projectId,
period: 60 * 1000,
onMetricUploadError: (err) => {
log.error(log.entry({err: err}, "Error uploading metrics: " + err));
},
});
globalStats.registerExporter(exporter);
// Omitted code...
await logMetricsFunction(); // Log out some metrics here.
await exporter.export();
// This is horrible, but the exporter calls:
// https://github.com/census-instrumentation/opencensus-node/blob/04ae44cf8875c59b99ea65ed2135b6bc816b49b4/packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts#L182
// Which is an async function we cannot get the promise for.
// So sleeping a bit to try and let it finish.
await sleep(30000); // This still doesn't help.
exporter.stop();
globalStats.unregisterExporter(exporter);
} catch (err) {
console.error("Error: " + err);
}
});
What did you expect to see?
I expected the metrics to get uploaded.
What did you see instead?
The metrics do not get uploaded. They only upload if I run the function locally on my laptop.
Additional context
I suspect this is because of the function getting torn down before the Promise chains here can return:
|
return this.authorize().then(authClient => { |
I attempted to work around this by calling export() manually and then sleeping a bunch, but this doesn't work either, and it is a total hack.
Export calls createTimeSeries, but it does not return the Promise that createTimeSeries returns, so there's no way for anyone to wait on them:
|
this.createTimeSeries(metricsList); |
So I'm wondering what is the recommendation to fix this? Can we have a synchronous way to ensure metrics are written out BEFORE the GCP Function returns?
Happy to make changes with guidance.
What version of OpenCensus are you using?
0.1.0
What version of Node are you using?
18.7.0
What did you do?
If possible, provide a recipe for reproducing the error.
Have code that looks like this inside a Google Cloud Function:
What did you expect to see?
I expected the metrics to get uploaded.
What did you see instead?
The metrics do not get uploaded. They only upload if I run the function locally on my laptop.
Additional context
I suspect this is because of the function getting torn down before the Promise chains here can return:
opencensus-node/packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts
Line 195 in 04ae44c
I attempted to work around this by calling
export()manually and thensleepinga bunch, but this doesn't work either, and it is a total hack.Export calls
createTimeSeries, but it does not return the Promise thatcreateTimeSeriesreturns, so there's no way for anyone to wait on them:opencensus-node/packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts
Line 137 in 04ae44c
So I'm wondering what is the recommendation to fix this? Can we have a synchronous way to ensure metrics are written out BEFORE the GCP Function returns?
Happy to make changes with guidance.