【问题标题】:How to make Error Reporting work with Node.js on Cloud Run如何在 Cloud Run 上使用 Node.js 进行错误报告
【发布时间】:2022-01-02 20:06:12
【问题描述】:

我已经按照 Node.js 的指令here

我有一个快速服务器部署到 Cloud Run。如果我使用 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”添加到我用于部署到 Cloud Run 的(AppEngine 默认)服务帐户,但它仍然不起作用。

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

有什么想法吗?

【问题讨论】:

    标签: node.js google-cloud-run google-cloud-error-reporting


    【解决方案1】:

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

    WARN:@google-cloud/error-reporting:stackdriver 错误报告 当且仅当 NODE_ENV 环境变量设置为“生产”时,客户端被配置为报告错误。错误不会 报道。要始终报告错误,无论 NODE_ENV 的值如何,请将 reportMode 配置选项设置为“always”。

    要让错误报告客户端库与 Cloud Run 一起使用,请确保:

    1. Stackdriver 错误报告 API 已启用。
    2. Cloud Run 服务必须有一个名为 NODE_ENVenvironment variable,其中包含值 production添加reportMode 实例化客户端时:
    const errors = new ErrorReporting({
        projectId: 'my-project-id',
        reportMode: 'always',
        }
    

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

    【讨论】:

    • 我尝试在选项中设置projectIdreportMode: "always",在我的Dockerfile 中设置ENV NODE_ENV production,但似乎没有什么区别。我什至使用console.errorerrors.report() 周围进行了尝试/捕获,以查看是否有任何结果,但我什么也没得到,甚至在日志资源管理器中也没有。
    • 原来我没有启用 Stackdriver 错误报告 API ?‍♂️ 我很确定,因为我一直在使用错误报告 UI 并且 Express 中间件正在工作。我刚刚从云功能收到一个关于它的错误。显然,如果 API 未启用,则会报告为错误。
    猜你喜欢
    • 2015-10-16
    • 2020-09-24
    • 2021-03-13
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2020-12-04
    • 2019-09-11
    • 2014-09-17
    相关资源
    最近更新 更多