Redux 集成

了解 Sentry 的 Redux 集成。

(适用于版本 1.8.0 及以上)

Redux 支持包含在 @sentry/react-native 包中,版本 1.8.0 及以上。要将 Sentry 应用于 Redux,请在初始化 Redux 存储的地方使用 Sentry.createReduxEnhancer

Copied
import { configureStore, createStore, compose } from "redux";
import * as Sentry from "@sentry/react-native";

// ...

const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  // Optionally pass options listed below
});

// If you are using the `configureStore` API, pass the enhancer as follows:
const store = configureStore({
  reducer,
  enhancers: (getDefaultEnhancers) => {
    return getDefaultEnhancers().concat(sentryReduxEnhancer);
  },
});

// If you are using the deprecated `createStore` API, pass the enhancer as follows:
const store = createStore(reducer, sentryReduxEnhancer);

如果你正在使用已弃用的 createStore API,并且有其他增强器或中间件(例如 thunk):

Copied
const store = createStore(
  rootReducer,
  compose(applyMiddleware(thunk), sentryReduxEnhancer),
);

默认情况下,Sentry SDK 会将任何上下文正规化到深度为 3。如果你想发送 Redux 状态,可能需要通过在 Sentry.init 调用中传递 normalizeDepth 来增加这个深度:

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  normalizeDepth: 10, // Or however deep you want your state context to be.
});

要配置增强器选项,请将一个选项对象作为第一个参数传递给 Sentry.createReduxEnhancer

actionTransformer (函数)

此函数可用于从操作中移除敏感信息。传递给该函数的第一个参数是 Redux 操作。默认情况下,我们会发送所有操作,如果你不希望某个操作发送到 Sentry,可以使用 return null

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  actionTransformer: (action) => {
    if (action.type === "GOVERNMENT_SECRETS") {
      // Return null to not log the action to Sentry
      return null;
    }
    if (action.type === "SET_PASSWORD") {
      // Return a transformed action to remove sensitive information
      return {
        ...action,
        password: null,
      };
    }

    return action;
  },
});

stateTransformer (函数)

此函数可用于从状态中移除敏感信息。传递给该函数的第一个参数是 Redux 状态。默认情况下,我们会附加所有状态变化。如果你不希望将状态变化附加到发送到 Sentry 的事件中,可以使用 return null。请注意,如果你选择不发送状态到 Sentry,你的错误可能不会附带最新版本的状态。

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  stateTransformer: (state) => {
    if (state.topSecret.doNotSend) {
      // Return null to not send this version of the state.
      return null;
    }

    // Transform the state to remove sensitive information
    const transformedState = {
      ...state,
      topSecret: {
        ...state.topSecret,
        // Replace sensitive information with something else
        nuclearLaunchCodes: "I love pizza",
        // or just remove it entirely
        hiddenTreasureLocation: null,
      },
      // You should also remove large data that is irrelevant to debugging to not clutter your Sentry issues
      giganticState: null,
    };

    return transformedState;
  },
});

configureScopeWithState (函数)

此函数在每次状态更新时被调用。要使用它,可以通过 Redux 状态配置 Sentry Scope。第一个参数是 scope,即你在调用 Sentry.configureScope 时获得的同一个 scope 实例。第二个参数是最新的 Redux 状态。

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  configureScopeWithState: (scope, state) => {
    // Set tag if the user is using imperial units.
    if (state.settings.useImperialUnits) {
      scope.setTag("user.usesImperialUnits", true);
    }
  },
});

attachReduxState (布尔值)

(适用于 7.69.0 及以上版本)

默认情况下,此选项为 true。它会将包含 Redux 状态的文件 redux_state.json 附加到所有发送到 Sentry 的错误事件中。这在 Redux 状态较大时特别有用。如果提供了 stateTransformer 函数,它将附加转换后的状态。如果你不希望将状态附加到错误事件中,可以将此选项设置为 false

Copied
const sentryReduxEnhancer = Sentry.createReduxEnhancer({
  attachReduxState: false,
});