nodejs 中 AWS lambda 上的哨兵不发送异常

Sentry on AWS lambda in nodejs doesn't send exception

我正在尝试在 AWS Lambda (nodejs 8.10) 上配置哨兵,但异常没有发送到哨兵。我觉得这是一个时间问题:lambda 在数据发送到哨兵之前终止。

在 AWS Lambda 上集成 sentry 的正确方法是什么?

谢谢!

更新:Sentry 在 Node/Lambda 上自动报告异常。见 docs

您必须使用函数 flush 确保发送当前排队的所有事件:

Sentry.captureException(new Error('test'));
await Sentry.flush();

http://getsentry.github.io/sentry-javascript/modules/node.html#flush

安装sentry/node

npm i @sentry/node

将此代码添加到您的 lambda 的顶部

const Sentry = require("@sentry/node");
Sentry.init({
  dsn: "https://0dc9558ffb934657b6c3b97182b4338d@sentry.io/182963" // your_dsn_key_here
});

使用哨兵对象

 try {
   //Logic here
  } catch (error) {
    Sentry.captureException("Error on getLogsById");
  }

安装哨兵模块

npm install @sentry/node

请参阅以下示例以使用 Sentry 配置 AWS Lambda 函数。

      // Import the Sentry module.
      const Sentry = require("@sentry/node");
      // Configure the Sentry SDK.
      Sentry.init({
        dsn: "your dsn",
      });

      exports.handler = function (event, context) {
        try {
          textFunctionCall(); // Call undefined function.
        } catch (e) {
          Sentry.captureException(e); // using this function exception captured in Sentry dashboard.
          Sentry.flush(2000);
        }

        const response = {
          statusCode: 200,
          body: "Hello, exception captured",
        };

       return response;
      };

https://docs.sentry.io/platforms/node/serverless/#aws-lambda

仍然没有在 Sentry 中捕获错误,然后安装下面的节点模块这些是依赖项。

npm install lru-cache
npm  install lru_map
npm install tslib