默认集成

了解更多关于系统集成:Dedupe、FunctionToString、Breadcrumbs、LinkedErrors 和 HttpContext (UserAgent),这些集成默认集成到标准库或解释器本身。

以下系统集成是标准库或解释器的一部分,并默认启用。要了解它们的作用以及如何在出现问题时禁用它们,请继续阅读。

Import name: Sentry.inboundFiltersIntegration

此集成允许你根据类型、消息或给定异常中的 URL 忽略特定错误。

默认情况下,它会忽略以 Script errorJavascript error: Script error 开头的错误。

要配置此集成,可以直接使用 ignoreErrorsignoreTransactionsdenyUrlsallowUrls SDK 选项。请注意,denyURLsallowURLs 仅适用于捕获的异常,而不适用于原始消息事件。

Import name: Sentry.functionToStringIntegration

此集成允许 SDK 提供原始函数和方法名称,即使这些函数或方法被我们的错误或面包屑处理器包装。

Import name: Sentry.breadcrumbsIntegration

此集成包装原生 API 以捕获面包屑。默认情况下,Sentry SDK 包装所有 API。

可用选项:

Copied
{
  // Log calls to `console.log`, `console.debug`, etc
  console: boolean;

  // Log all click and keypress events
  // - When an object with `serializeAttribute` key is provided,
  //   Breadcrumbs integration will look for given attribute(s) in DOM elements,
  //   while generating the breadcrumb trails.
  //   Matched elements will be followed by their custom attributes,
  //   instead of their `id`s or `class` names.
  dom: boolean | { serializeAttribute: string | string[] };

  // Log HTTP requests done with the Fetch API
  fetch: boolean;

  // Log calls to `history.pushState` and friends
  history: boolean;

  // Log whenever we send an event to the server
  sentry: boolean;

  // Log HTTP requests done with the XHR API
  xhr: boolean;
}

Import name: Sentry.nativeLinkedErrorsIntegration

此集成允许你配置递归读取的链接错误,直到指定的限制。然后在捕获的 Error 对象上通过特定键进行查找。默认情况下,集成记录最多五个错误,用于递归的键是 "cause"

可用选项:

Copied
{
  key: string; // default: "cause"
  limit: number; // default: 5
}

这是一个如何实现的代码示例:

Copied
<Button
  title="Get Reviews"
  onPress={async () => {
    const movie = event.target.dataset.title;
    try {
      await fetchMovieReviews(movie);
    } catch (e) {
      const fetchError = new Error(
        `Failed to fetch reviews for: ${movie}`,
      );
      // @ts-expect-error cause is not exposed on default error class.
      fetchError.cause = e;
      Sentry.captureException(fetchError);
    }
  }}
/>;

Import name: Sentry.httpContextIntegration

此集成将 HTTP 请求信息(如 URL、用户代理、来源和其他标头)附加到事件中。它使我们能够正确地根据特定的操作系统、浏览器和版本信息对事件进行分类和标记。

Import name: Sentry.dedupeIntegration

此集成默认启用,但仅去重某些事件。如果你收到许多重复的错误,这可能会有所帮助。请注意,Sentry 仅比较堆栈跟踪和指纹。

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [Sentry.dedupeIntegration()],
});

要禁用系统集成,在调用 init() 时设置 defaultIntegrations: false

要覆盖它们的设置,向 integrations 选项提供一个带有你配置的新实例。例如,要关闭浏览器捕获控制台调用:

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",

  integrations: [
    Sentry.breadcrumbsIntegration({
      console: false,
    }),
  ],
});

此示例移除了默认启用的将面包屑添加到事件的集成:

Copied
Sentry.init({
  // ...
  integrations: function (integrations) {
    // integrations will be all default integrations
    return integrations.filter(function (integration) {
      return integration.name !== "Breadcrumbs";
    });
  },
});