事件处理器

了解更多关于如何全局或在当前作用域中添加您自己的事件处理器。

您可以通过添加自己的事件处理器来丰富事件数据,这些处理器可以添加到作用域级别或全局。虽然事件处理器与 beforeSend 和 beforeSendTransaction 类似,但有两点关键区别:

  • beforeSend 和 beforeSendTransaction 保证在所有其他事件处理器之后运行(这意味着它们在事件发送前获取最终版本的事件,因此得名)。而通过以下方法添加的事件处理器以不确定的顺序运行,这意味着在事件处理器运行后,事件仍可能被修改。
  • beforeSendbeforeSendTransaction 以及使用 Sentry.addEventProcessor 添加的处理器在全球范围内运行,不受作用域限制;而使用 scope.addEventProcessor 添加的处理器仅在该作用域活跃时对捕获的事件生效。

beforeSend 和 beforeSendTransaction 类似,事件处理器接收两个参数:事件本身和包含额外元数据的 hint 对象

添加到当前作用域的事件处理器将在添加后对每个发送的事件运行。

Copied
Sentry.addEventProcessor(function (event, hint) {
  // Add anything to the event here
  // returning `null` will drop the event
  return event;
});

使用 withScope 添加到本地作用域的事件处理器仅适用于在该作用域内捕获的事件。

Copied
Sentry.withScope(function (scope) {
  scope.addEventProcessor(function (event, hint) {
    // Add anything to the event here
    // returning `null` will drop the event
    return event;
  });
  // The event processor will apply to this event
  Sentry.captureMessage("Test");
});

// The event processor will NOT apply to this event
Sentry.captureMessage("Test2");