Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Stackdriver exporter does not upload metrics when used in GCP Function and there is no way to wait on it #996

Open
ckenna opened this issue Aug 5, 2022 · 0 comments
Labels

Comments

@ckenna
Copy link

ckenna commented Aug 5, 2022

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:

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:

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.

@ckenna ckenna added the bug label Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant