可插拔集成

了解更多关于可插拔集成:HttpClient 和 RewriteFrames,这些代码片段为特定应用程序和/或框架增强功能。

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

集成会自动为你的应用程序添加错误 instrumentation、性能 instrumentation 和/或额外的上下文信息。有些集成默认启用,但你可以禁用它们或修改其设置。其他集成可以添加以扩展 SDK 的默认功能。

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

Copied
import * as Sentry from "@sentry/react-native";

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

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

Copied
import * as Sentry from "@sentry/react-native";

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

Sentry.addIntegration(Sentry.dedupeIntegration());

(新版本 5.3.0)

Import name: Sentry.httpContextIntegration

此集成捕获来自 Fetch 和 XHR 请求失败的错误,并附加请求和响应信息。

默认情况下,错误事件不包含头部或 cookie 数据。你可以通过将 sendDefaultPii 选项设置为 true 来更改此行为。

Copied
import * as Sentry from "@sentry/react-native";

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

  // This option is required for capturing headers and cookies.
  sendDefaultPii: true,
});

Import name: Sentry.rewriteFramesIntegration

此集成允许你对堆栈跟踪的每一帧应用转换。在简化场景中,它可以用于更改帧来源文件的名称,或者可以通过迭代函数应用任意转换。

在 Windows 机器上,你需要在 root 选项中使用 Unix 路径并跳过卷字母来启用它。例如,C:\\Program Files\\Apache\\www 不会生效,但 /Program Files/Apache/www 会生效。

Copied
import * as Sentry from "@sentry/react-native";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [Sentry.rewriteFramesIntegration(
    {
      // root path that will be stripped from the current frame's filename by the default iteratee if the filename is an absolute path
      root: string;

      // a custom prefix that will be used by the default iteratee (default: `app://`)
      prefix: string;

      // function that takes the frame, applies a transformation, and returns it
      iteratee: (frame) => frame;
    }
  )],
});

我们将使用 bundles/bundle1.js 作为以下表格中文件的完整路径示例:

使用方法堆栈跟踪中的路径描述
RewriteFrames()app:///bundle1.js默认行为是替换绝对路径,去掉文件名,并添加默认前缀 (app:///)。
RewriteFrames({root: '/bundles'})app:///bundle1.jsroot 定义为 /bundles。只有这部分会从路径的开头被修剪掉。
RewriteFrames({iteratee: () => {} })app:///bundle.js束结尾的数字可能是文件哈希。这在 Expo 应用中很常见。你可以在 iteratee 回调中移除它。