node.js - Sentry新的统一SDK如何发送请求信息?
问题描述
旧的 Sentry Node SDK ( ravenoptions
) 允许通过在对象中传递请求对象(第二个参数)来发送 HTTP 请求信息以及错误:
Raven.captureException(someError, { req: req });
从文档中提取的代码行:https ://docs.sentry.io/clients/node/usage/#raven-recording-breadcrumbs
有了它,我们可以获得有关错误的其他上下文,例如正在使用的设备。
有什么方法可以通过新的 SDK 传递请求对象?新 SDK的上下文部分解释了如何使用范围发送数据,如用户标识、自定义标签等,但其中没有可用的请求选项。这是否意味着现在应该在内部和/或对象中手动发送请求信息?tags
extra
解决方案
正如@MarkusUnterwaditzer 所分享的,Express 集成对我有用:https ://docs.sentry.io/platforms/node/express/关键部分是添加Sentry.Handlers.requestHandler()
中间件,然后添加 errorHandler 中间件或Sentry.captureException(error)
自己做(无需通过中{req: req}
)。
示例代码:
const express = require('express');
const app = express();
const Sentry = require('@sentry/node');
Sentry.init({ dsn: 'https://8f0620a3bfea4f2ca26aefb074851e23@sentry.io/280382' });
// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
app.get('/', function mainHandler(req, res) {
throw new Error('Broke!');
});
// The error handler must be before any other error middleware
app.use(Sentry.Handlers.errorHandler());
// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + '\n');
});
app.listen(3000);
推荐阅读
- html - 编码字符在 HTML 上显示正常,但在 SVG 上显示
- automation - 是否可以使用第三方自动化 Visio?
- python - 在python中的语句引发异常
- java - 将未知大小的文件上传到 S3 的最佳策略
- angularjs - Angular JS:ng-option 添加“string:”前缀 int 选项值
- python - 如何从 DataFrame 中随机选择记录?
- ssl - 访问 Tomcat 站点时出现 Firefox SSL_ERROR_RX_UNKNOWN_ALERT 错误
- react-native - 如何在 this.state 数组中获取孩子
- java - 将 Firebase 添加到我的依赖项“程序已存在”后出现错误
- java - 根据 Map 的 String 元素创建新的分组
>