我们所有与JavaScript相关的SDK都提供了相同的API,但这些文档的这一部分解释了它们之间存在一些差异。
一、集成
我们所有的SDK都提供了可以在某种插件中看到的集成。所有JavaScript SDK都提供默认的集成,请检查特定SDK的详细信息,以查看它们提供的集成。
在我们所有的JavaScript SDK中,有一点是相同的,那就是添加或删除Integrations的方式,例如:for @ sentry / node。
二、添加集成
import * as Sentry from '@sentry/node';
// All integration that come with an SDK can be found on Sentry.Integrations object
// Custom integration must conform Integration interface: https://github.com/getsentry/sentry-javascript/blob/master/packages/types/src/index.ts
Sentry.init({
dsn: 'https://<key>@sentry.io/<project>',
integrations: [new MyAwesomeIntegration()]
});
三、删除集成
在此示例中,我们将删除默认启用的集成,以便为事件添加面包屑:
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: 'https://<key>@sentry.io/<project>',
integrations: integrations => {
// integrations will be all default integrations
return integrations.filter(integration => integration.id !== 'Console');
}
});
四、设置集成的替代方法
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: 'https://<key>@sentry.io/<project>',
integrations: integrations => {
// integrations will be all default integrations
return [...integrations, new MyCustomIntegration()];
}
});
五、提示
Event和Breadcrumb提示是包含用于组合事件或面包屑的各种信息的对象。对于事件,这些事件包括event_id,originalException,syntheticException(在内部用于生成更清晰的堆栈跟踪)以及用户附加的任何其他任意数据。对于面包屑,它依赖于所有实现。对于XHR请求,提示包含xhr对象本身,对于用户交互,它包含DOM元素和事件名称等。它们在两个地方可用。 beforeSend / beforeBreadcrumb和eventProcessors。这是我们允许用户修改我们放在一起的两种方式。当前存在事件的常见提示:
originalException
导致创建事件的原始异常。这对于更改事件的分组方式或提取其他信息非常有用。
syntheticException
当引发字符串或非错误对象时,Sentry会创建一个合成异常,以便您可以获得基本的堆栈跟踪。此异常存储在此处以进行进一步的数据提取。这些存在于面包屑中:
level / input
对于从控制台日志拦截创建的面包屑,它将原始控制台日志级别和原始输入数据保存到日志功能。
request
/ response
/ event
对于从HTTP请求创建的面包屑,它保存请求和响应对象(来自节点HTTP API)以及节点事件(响应或错误)。
六、EventProcessors
使用eventProcessors,您可以使用其他数据进入丰富事件的过程。您可以在当前范围上添加自己的eventProcessor。与beforeSend的区别在于eventProcessors在范围级别上运行,其中beforeSend全局运行,而不管您在哪个范围内运行。 eventProcessors还可以选择接收提示,参见:Hints。
// This will be set globally for every succeeding event send
Sentry.configureScope(scope => {
scope.addEventProcessor(async (event, hint) => {
// Add anything to the event here
// returning null will drop the event
return event;
});
});
// Using withScope, will only call the event processor for all "sends"
// that happen within withScope
Sentry.withScope(scope => {
scope.addEventProcessor(async (event, hint) => {
// Add anything to the event here
// returning null will drop the event
return event;
});
Sentry.captureMessage('Test');
});