From 878df9b50d2fcdad9582c846a53668738f239c44 Mon Sep 17 00:00:00 2001 From: Prajjaval Verma Date: Mon, 9 Dec 2024 20:44:23 +0530 Subject: [PATCH] NTP-11825 | CyberDroid Integration (#13584) Co-authored-by: Shivam Goyal --- App.tsx | 9 ++- .../constants/AnalyticsEventsConstant.ts | 1 + App/common/constants/SentryConstants.ts | 13 ++- App/common/utilities/SentryUtils.ts | 81 +++++++++++++++++++ metro.config.js | 6 +- package.json | 1 + 6 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 App/common/utilities/SentryUtils.ts diff --git a/App.tsx b/App.tsx index d8082b201d..edd2914b96 100644 --- a/App.tsx +++ b/App.tsx @@ -3,18 +3,20 @@ import codePush from "react-native-code-push"; import { GenericErrorScreen } from "./App/Container/Navi-Insurance"; import { AnalyticsEventNameConstants, - BASE_SCREEN, BundleState, CODEPUSH_METHOD, EVENT_NAMES, EVENT_PROPERTY_KEYS, EVENT_PROPERTY_VALUES, - SentryConstants, } from "./App/common/constants"; import { sendAsAnalyticsEvent } from "./App/common/hooks/useAnalyticsEvent"; import { CtaData } from "./App/common/interface"; import RnApp from "./App/common/navigator/RnAppCreator"; -import { setBuildConfigDetailsFromCta } from "./App/common/utilities/CacheUtils"; +import { + getBuildConfigDetailsFromCta, + setBuildConfigDetailsFromCta, +} from "./App/common/utilities/CacheUtils"; +import { initSentry } from "./App/common/utilities/SentryUtils"; import { getStringPreference, setStringPreference, @@ -29,6 +31,7 @@ const preloadBundle: string = "PreloadBundle"; export default class App extends Component<{}, AppState> { constructor(props: {}) { super(props); + initSentry(getBuildConfigDetailsFromCta(this.getInitialCta()!!).flavor); let bundleState = BundleState.LOADING; getBundleStatus().then(value => (bundleState = value)); this.state = { diff --git a/App/common/constants/AnalyticsEventsConstant.ts b/App/common/constants/AnalyticsEventsConstant.ts index 02e7d66b2b..0449c72765 100644 --- a/App/common/constants/AnalyticsEventsConstant.ts +++ b/App/common/constants/AnalyticsEventsConstant.ts @@ -27,6 +27,7 @@ export const AnalyticsEventNameConstants = { HI_RN_INVALID_JSON_ERROR: "hi_rn_invalid_json_error", HI_RN_INVALID_ARITHMETIC_ERROR: "hi_rn_invalid_arithmetic_error", HI_RN_FETCH_NATIVE_HEADER_ERROR: "hi_rn_fetch_native_header_error", + HI_RN_SENTRY_INIT_FAIL: "hi_rn_sentry_init_fail", }; export const AnalyticsEventPropertyConstants = { diff --git a/App/common/constants/SentryConstants.ts b/App/common/constants/SentryConstants.ts index e0316cbf9a..af984b209a 100644 --- a/App/common/constants/SentryConstants.ts +++ b/App/common/constants/SentryConstants.ts @@ -1,5 +1,10 @@ export const SentryConstants = { - SENTRY_DSN: "https://3c1639ff154fcdcfe417c485e80db17f@o4506931811254272.ingest.us.sentry.io/4506931817676800", - SENTRY_ENVIRONMENT_PRODUCTION: "production", - SENTRY_ENVIRONMENT_DEVELOPMENT: "development", -}; \ No newline at end of file + SENTRY_DSN_QA: + "https://35a300131251d53a92e5e705c316fc55@cybetron.np.navi-ppl.in/220582376234120579338115031530316318641", + SENTRY_DSN_PROD: + "https://c7ea344069d4ddea1d969cbe89a300f5@cybetron.np.navi-ppl.in/232196262456315044471201793383582317162", + TUNNEL_URL_QA: + "https://qa-sa.navi.com/cybertron/api/220582376234120579338115031530316318641/envelope?sentry_key=35a300131251d53a92e5e705c316fc55", + TUNNEL_URL_PROD: + "https://sa.navi.com/cybertron/api/232196262456315044471201793383582317162/envelope?sentry_key=c7ea344069d4ddea1d969cbe89a300f5", +}; diff --git a/App/common/utilities/SentryUtils.ts b/App/common/utilities/SentryUtils.ts new file mode 100644 index 0000000000..3c2fffdf2a --- /dev/null +++ b/App/common/utilities/SentryUtils.ts @@ -0,0 +1,81 @@ +import { createTransport } from "@sentry/core"; +import * as Sentry from "@sentry/react-native"; +import { getBundleVersion } from "../../../network/NetworkUtils"; +import { AnalyticsEventNameConstants, SentryConstants } from "../constants"; +import { sendAsAnalyticsEvent } from "../hooks/useAnalyticsEvent"; + +function makeFetchTransport(options: any): any { + function makeRequest(request: any) { + const requestOptions: RequestInit = { + body: request.body, + method: "POST", + referrerPolicy: "origin", + headers: options.headers, + ...options.fetchOptions, + }; + + return fetch(options.url, requestOptions).then(response => { + return { + statusCode: response.status, + headers: { + "x-sentry-rate-limits": response.headers.get("X-Sentry-Rate-Limits"), + "retry-after": response.headers.get("Retry-After"), + }, + }; + }); + } + + return createTransport(options, makeRequest); +} + +export async function initSentry(env?: string) { + try { + Sentry.init({ + dsn: + env === "prod" + ? SentryConstants.SENTRY_DSN_PROD + : SentryConstants.SENTRY_DSN_QA, + transport: makeFetchTransport, + tunnel: + env === "prod" + ? SentryConstants.TUNNEL_URL_PROD + : SentryConstants.TUNNEL_URL_QA, + beforeSend(event) { + event.extra = { + ...event.extra, + release_id: getBundleVersion().toString(), + alfredSessionId: "", + metadata: {}, + }; + return event; + }, + }); + } catch (e) { + sendAsAnalyticsEvent({ + name: AnalyticsEventNameConstants.HI_RN_SENTRY_INIT_FAIL, + properties: { + error: `${e}`, + }, + }); + } +} + +export const logError = (error: Error, extraInfo = "") => { + if (__DEV__) { + return; + } + Sentry.captureException(error, scope => { + scope.setExtra("ExtraInfo", extraInfo); + return scope; + }); +}; + +export const sentryCaptureMessage = (errorStr: string, extraInfo = "") => { + if (__DEV__) { + return; + } + Sentry.captureMessage(errorStr, scope => { + scope.setExtra("ExtraInfo", extraInfo); + return scope; + }); +}; diff --git a/metro.config.js b/metro.config.js index 892433a83f..b87477f3d7 100644 --- a/metro.config.js +++ b/metro.config.js @@ -1,5 +1,5 @@ const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config"); - +const { withSentryConfig } = require("@sentry/react-native/metro"); /** * Metro configuration * https://facebook.github.io/metro/docs/configuration @@ -8,4 +8,6 @@ const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config"); */ const config = {}; -module.exports = mergeConfig(getDefaultConfig(__dirname), config); +module.exports = withSentryConfig( + mergeConfig(getDefaultConfig(__dirname), config), +); diff --git a/package.json b/package.json index 2c24dc54d1..0df679be25 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@react-navigation/stack": "^6.3.17", "@react-spring/native": "9.5.5", "@reduxjs/toolkit": "^1.9.5", + "@sentry/react-native": "^5.35.0", "axios": "^1.4.0", "lodash": "^4.17.21", "lottie-react-native": "^5.1.6",