使用 Express 进行 Sentry 错误日志记录不会发送任何错误

Sum*_*ai8 1 node.js express sentry

当我尝试向简单的 Node.js Express 服务器的 Sentry 发送错误时,Sentry 本身没有记录任何错误。没有设置入站过滤器,并且不会报告过去 30 天内过滤的任何内容。允许的域设置为*,我认为这是默认值。我使用的代码与文档中的示例代码或多或少相同。但是,当调用端点时,Sentry 中不会出现任何内容,并且调试行即使发送了错误也不会显示任何有关错误的信息。

如何让 Sentry 正确捕获错误?

这是测试.js

'use strict';

const express = require('express');
const Sentry = require('@sentry/node');

const app = express();

// TODO dotenv setting
const SENTRY_NODE_DSN = process.env.SENTRY_NODE_DSN || 'the ingest url from settings > client keys';

console.log(`Started logging errors at sentry on DSN ${SENTRY_NODE_DSN}`);
// Sentry
Sentry.init({
    dsn: SENTRY_NODE_DSN,
    debug: true,
});

// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());

const port = process.argv[2];
if (port === undefined) {
    console.log(`Please specify a port as first argument`);
    process.exit(0);
}

app.get('/test', () => {
    throw new Error('test');
});

// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler({
    shouldHandleError(error) {
        return true;
    }
}));

app.listen(port);
Run Code Online (Sandbox Code Playgroud)

我们开始它node ./test.js 3000

然后从另一个窗口执行wget -- localhost:3000/test,得到以下输出。

--2021-07-29 14:03:01--  http://localhost:3000/test
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:3000... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2021-07-29 14:03:01 ERROR 500: Internal Server Error.
Run Code Online (Sandbox Code Playgroud)

快递服务器的输出是这样的:

$ node ./test.js 3000
Started logging errors at sentry on DSN same url as in the code above
Sentry Logger [Log]: Integration installed: InboundFilters
Sentry Logger [Log]: Integration installed: FunctionToString
Sentry Logger [Log]: Integration installed: Console
Sentry Logger [Log]: Integration installed: Http
Sentry Logger [Log]: Integration installed: OnUncaughtException
Sentry Logger [Log]: Integration installed: OnUnhandledRejection
Sentry Logger [Log]: Integration installed: LinkedErrors
Error: test
    at /var/www/vhosts/myproject/test.js:28:8
    at Layer.handle [as handle_request] (/var/www/vhosts/myproject/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/vhosts/myproject/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/www/vhosts/myproject/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/www/vhosts/myproject/node_modules/express/lib/router/layer.js:95:5)
    at /var/www/vhosts/myproject/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/var/www/vhosts/myproject/node_modules/express/lib/router/index.js:335:12)
    at next (/var/www/vhosts/myproject/node_modules/express/lib/router/index.js:275:10)
    at Domain.<anonymous> (/var/www/vhosts/myproject/node_modules/@sentry/node/dist/handlers.js:321:13)
    at Domain.run (domain.js:370:15)
Run Code Online (Sandbox Code Playgroud)

Sum*_*ai8 5

就我而言,这段代码是正确的,但问题是我在 VPN 上的 wsl 中运行此脚本,并且正在发送请求,但挂起。通过实验,Sentry 在调试模式下尝试发送错误时似乎不会记录任何内容,因此您无法从中推断出任何内容。

要测试 Sentry 是否确实发送任何内容,请尝试更简单的版本

const Sentry = require('@sentry/node');
const SENTRY_NODE_DSN = 'the ingest url';
Sentry.init({
    dsn: SENTRY_NODE_DSN,
    debug: true,
});
Sentry.captureException(new Error('test exception'));
Run Code Online (Sandbox Code Playgroud)

如果脚本挂起(未终止),则意味着对哨兵服务器的 http 请求尚未完成。这可能是由于您这边的网络问题造成的。