事件处理器
了解如何添加自己的事件处理器以全局或当前作用域级别丰富事件。
你可以通过添加自己的事件处理器来丰富事件,这些处理器可以添加到作用域级别或全局。虽然事件处理器与 beforeSend
和 beforeSendTransaction
类似,但有两点关键区别:
beforeSend
和beforeSendTransaction
保证在所有其他事件处理器之后运行(这意味着它们在事件发送前获取最终版本的事件,因此得名)。而使用以下方法添加的事件处理器运行顺序不确定,这意味着在事件处理器运行后,事件仍可能被修改。beforeSend
、beforeSendTransaction
和使用Sentry.addGlobalEventProcessor
添加的处理器在全球范围内运行,不依赖于作用域;而使用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");