集成

了解更多关于集成如何扩展我们 SDK 的功能,以自动覆盖常见的库和环境。

Sentry SDK 使用集成来挂钩流行库的功能,以自动仪器化您的应用程序,并为您提供开箱即用的最佳数据。

集成会自动为您的应用程序添加错误仪器化、性能仪器化和/或额外的上下文信息。某些集成默认启用,但您可以禁用它们或修改其设置。

Auto EnabledErrorsTracingReplayAdditional Context
breadcrumbsIntegration
browserApiErrorsIntegration
browserSessionIntegration
dedupeIntegration
functionToStringIntegration
globalHandlersIntegration
httpContextIntegration
inboundFiltersIntegration
linkedErrorsIntegration
browserProfilingIntegration
browserTracingIntegration
captureConsoleIntegration
contextLinesIntegration
debugIntegration
extraErrorDataIntegration
featureFlagsIntegration
httpClientIntegration
launchDarklyIntegration
moduleMetadataIntegration
openFeatureIntegration
rewriteFramesIntegration
replayIntegration
replayCanvasIntegration
reportingObserverIntegration
sessionTimingIntegration

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

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

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

  integrations: [
    Sentry.linkedErrorsIntegration({
      limit: 7,
    }),
  ],
});

您可以在 init 调用中添加额外的集成:

Copied
import * as Sentry from "@sentry/browser";

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

或者,您可以通过 Sentry.addIntegration() 添加集成。 这在您只想在特定环境中启用集成或稍后加载集成时非常有用。 对于所有其他情况,我们建议您使用 integrations 选项。

Copied
import * as Sentry from "@sentry/browser";

Sentry.init({
  integrations: [],
});

Sentry.addIntegration(Sentry.reportingObserverIntegration());

懒加载允许您在不增加初始包大小的情况下添加可插拔集成。您可以使用两种方法实现:

您可以使用 import() 进行动态导入来添加集成。此方法从 npm 包加载集成。为了避免遇到 import() 的问题,您应该使用支持动态导入的打包工具。如果您使用像 Vite 这样的工具进行项目开发,打包过程可能已经设置好了。

Copied
Sentry.init({
  // Note, Replay is NOT instantiated below:
  integrations: [],
});

// Sometime later
import("@sentry/browser").then((lazyLoadedSentry) => {
  Sentry.addIntegration(lazyLoadedSentry.replayIntegration());
});

您也可以通过 Sentry.lazyLoadIntegration() 懒加载可插拔集成。这将尝试从 Sentry CDN 加载集成。 请注意,如果用户启用了广告拦截器或出现网络问题,导致无法从 Sentry CDN 加载集成时,此函数将被拒绝。您应确保在应用程序中处理此函数的拒绝情况。

Copied
async function loadHttpClient() {
  const httpClientIntegration = await Sentry.lazyLoadIntegration(
    "httpClientIntegration",
  );
  Sentry.addIntegration(httpClientIntegration());
}

懒加载适用于以下集成:

  • replayIntegration
  • replayCanvasIntegration
  • feedbackIntegration
  • feedbackModalIntegration
  • feedbackScreenshotIntegration
  • captureConsoleIntegration
  • contextLinesIntegration
  • linkedErrorsIntegration
  • debugIntegration
  • dedupeIntegration
  • extraErrorDataIntegration
  • httpClientIntegration
  • reportingObserverIntegration
  • rewriteFramesIntegration
  • sessionTimingIntegration
  • browserProfilingIntegration

如果您只想移除一个或部分默认集成,而不是通过 defaultIntegrations: false 禁用所有集成,您可以使用以下语法过滤掉不需要的集成。

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

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

您也可以创建自定义集成