如何在 Cloud 运行 上使用 Node.js 进行错误报告

How to make Error Reporting work with Node.js on Cloud Run

我已按照 Node.js 说明进行操作 here

我有一个部署到云端的快速服务器 运行。如果我使用 express 中间件并将错误发送到 next(),如文档中所述,它会显示在错误报告中。

但是,如果我以如下所示的各种形式使用 report 方法,则不会报告任何内容。

// Report an Error object
errors.report(new Error('My error message'), () => {
  console.log('Done reporting Error object!');
});

// Report an error by provided just a string
errors.report('My error message', () => {
  console.log('Done reporting error string!');
});

// Use the error message builder to customize all fields ...
const errorEvent = errors.event();

// Add error information
errorEvent.setMessage('My error message');
errorEvent.setUser('root@nexus');

// Report the error event
errors.report(errorEvent, () => {
  console.log('Done reporting error event!');
});

我已经尝试将 IAM 角色“Error Reporter Writer”添加到我用来部署到云的(AppEngine 默认)服务帐户 运行,但它仍然不起作用。

TS 编译器也不接受示例中使用的回调函数,因此示例已过时或 TS 类型定义错误。

有什么想法吗?

我试图重现您的错误并发现了一些事情,例如这个警告:

WARN:@google-cloud/error-reporting: The stackdriver error reporting client is configured to report errors if and only if the NODE_ENV environment variable is set to "production". Errors will not be reported. To have errors always reported, regardless of the value of NODE_ENV, set the reportMode configuration option to "always".

要使错误报告客户端库与云一起工作 运行,请确保:

  1. Stackdriver 错误报告API 已启用。
  2. 云 运行 服务必须有一个 environment variable called NODE_ENV which contains value production. Or add reportMode 实例化客户端时:
const errors = new ErrorReporting({
    projectId: 'my-project-id',
    reportMode: 'always',
    }

这是我能够使用 report() 方法提交给错误报告的示例: