【问题标题】:How to send request information with Sentry's new unified SDK?Sentry新的统一SDK如何发送请求信息?
【发布时间】:2019-07-20 11:48:18
【问题描述】:

旧的 Sentry 节点 SDK (raven) 允许通过在 options 对象(第二个参数)中传递请求对象来发送 HTTP 请求信息以及错误:

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

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

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

有什么方法可以通过新的 SDK 传递请求对象? new SDKcontext 部分解释了如何使用范围发送数据,例如用户标识、自定义标签等,但其中没有可用的请求选项。这是否意味着现在应该在 tags 和/或 extra 对象中手动发送请求信息?

【问题讨论】:

  • 一般请求信息由集成设置。您要设置的请求数据来自哪里?
  • @MarkusUnterwaditzer 它将是来自 Express 框架的 req 对象,就像在之前的 Sentry node.js SDK 中一样。旧版 SDK 的 req 字段的描述是:“与此事件关联的请求对象,来自 Node http 服务器、Express、Koa 或类似服务器”
  • 我们有一个快速集成,我认为应该可以自动捕获这些数据:docs.sentry.io/platforms/node/express

标签: node.js sentry


【解决方案1】:

正如@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);

【讨论】:

    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2015-11-02
    • 2013-06-10
    • 2018-12-24
    • 2015-08-31
    相关资源
    最近更新 更多