node.js - 如何使用Sentry新推出的统一SDK发送请求信息?

标签 node.js sentry

旧版 Sentry Node SDK ( raven ) 允许通过在 options 对象(第二个参数)内传递请求对象来发送 HTTP 请求信息和错误:

Raven.captureException(someError, { req: req });

从文档中提取的代码行:https://docs.sentry.io/clients/node/usage/#raven-recording-breadcrumbs

这样我们就可以获得有关错误的更多上下文,例如正在使用的设备是什么。

有什么办法可以用新的 SDK 传递请求对象吗? context new 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);

关于node.js - 如何使用Sentry新推出的统一SDK发送请求信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54893900/

相关文章:

python - TurboGears 和 backlash : How to add extra, 每个请求上下文到 Raven 以获得更多信息的 Sentry 报告?

node.js - Node JS获取请求体长度

node.js - nodejs内存分析

node.js - NodeJS 网页抓取 - 表单提交

python - 您如何在开发环境中配置 Sentry raven 客户端以不发送异常并且仍然​​可以工作?

javascript - 在 Next.js 项目中集成 Sentry

node.js - sublime build - nodejs - 如何启动和停止 Node 应用程序

javascript - Node Puppeteer,page.on( "request") 抛出 "Request is already handled!"

django - 使用没有 raven 的新 sentry_sdk 为 Django 配置 Sentry 处理程序

c# - 如何在.Net 5项目中将Sentry连接到NLog