From 870e47acdee085fe2205fe5a234fa5f55a697a29 Mon Sep 17 00:00:00 2001 From: jennmueng Date: Sun, 6 Dec 2020 18:04:34 +0700 Subject: [PATCH 01/40] wip: Fetch/XHR + React Navigation Instrumentation --- sample/src/App.tsx | 33 +++- sample/src/screens/HomeScreen.tsx | 9 +- sample/src/screens/ManualTrackerScreen.tsx | 158 +++++++++++++++++ sample/src/screens/TrackerScreen.tsx | 33 +--- src/js/index.ts | 4 + src/js/tracing/index.ts | 3 + src/js/tracing/reactnativetracing.ts | 188 +++++++++++++++++++++ src/js/tracing/reactnavigation.ts | 74 ++++++++ src/js/tracing/router.ts | 30 ++++ 9 files changed, 497 insertions(+), 35 deletions(-) create mode 100644 sample/src/screens/ManualTrackerScreen.tsx create mode 100644 src/js/tracing/index.ts create mode 100644 src/js/tracing/reactnativetracing.ts create mode 100644 src/js/tracing/reactnavigation.ts create mode 100644 src/js/tracing/router.ts diff --git a/sample/src/App.tsx b/sample/src/App.tsx index 4b84f911da..6006be36ad 100644 --- a/sample/src/App.tsx +++ b/sample/src/App.tsx @@ -1,19 +1,25 @@ import * as React from 'react'; -import { Provider } from 'react-redux'; -import { NavigationContainer } from '@react-navigation/native'; -import { createStackNavigator } from '@react-navigation/stack'; +import {Provider} from 'react-redux'; +import { + NavigationContainer, + NavigationContainerRef, +} from '@react-navigation/native'; +import {createStackNavigator} from '@react-navigation/stack'; // Import the Sentry React Native SDK import * as Sentry from '@sentry/react-native'; import HomeScreen from './screens/HomeScreen'; import TrackerScreen from './screens/TrackerScreen'; +import ManualTrackerScreen from './screens/ManualTrackerScreen'; import PerformanceTimingScreen from './screens/PerformanceTimingScreen'; import EndToEndTestsScreen from './screens/EndToEndTestsScreen'; -import { store } from './reduxApp'; -import { version as packageVersion } from '../../package.json'; -import { SENTRY_INTERNAL_DSN } from './dsn'; +import {store} from './reduxApp'; +import {version as packageVersion} from '../../package.json'; +import {SENTRY_INTERNAL_DSN} from './dsn'; + +const reactNavigationInstrumentation = new Sentry.Tracing.ReactNavigationInstrumentation(); Sentry.init({ // Replace the example DSN below with your own DSN: @@ -24,6 +30,12 @@ Sentry.init({ return e; }, maxBreadcrumbs: 150, // Extend from the default 100 breadcrumbs. + integrations: [ + new Sentry.Tracing.ReactNativeTracing({ + routingInstrumentation: reactNavigationInstrumentation, + tracingOrigins: ['localhost', /^\//, /^https:\/\//], + }), + ], enableAutoSessionTracking: true, // For testing, session close when 5 seconds (instead of the default 30) in the background. sessionTrackingIntervalMillis: 5000, @@ -38,12 +50,19 @@ Sentry.init({ const Stack = createStackNavigator(); const App = () => { + const navigation = React.createRef(); + + React.useEffect(() => { + reactNavigationInstrumentation.registerNavigationContainer(navigation); + }, []); + return ( - + + { onPress={() => { props.navigation.navigate('Tracker'); }}> - Tracing Example + Auto Tracing Example + + + { + props.navigation.navigate('ManualTracker'); + }}> + Manual Tracing Example { + const [cases, setCases] = React.useState<{ + TotalConfirmed: number; + TotalDeaths: number; + TotalRecovered: number; + } | null>(null); + + const transaction = React.useRef(null); + + React.useEffect(() => { + // Initialize the transaction for the screen. + transaction.current = Sentry.startTransaction({ + name: 'Tracker Screen', + }); + + return () => { + // Finishing the transaction triggers sending the data to Sentry. + transaction.current?.finish(); + transaction.current = null; + Sentry.configureScope((scope) => { + scope.setSpan(undefined); + }); + }; + }, []); + + const loadData = () => { + setCases(null); + + // Create a child span for the API call. + const span = transaction.current?.startChild({ + op: 'http', + description: 'Fetch Covid19 data from API', + }); + + fetch('https://api.covid19api.com/world/total', { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + }) + .then((response) => response.json()) + .then((json) => { + setCases(json); + + span?.setData('json', json); + span?.finish(); + }); + }; + + React.useEffect(() => { + loadData(); + }, []); + + return ( + + + Global COVID19 Cases + + + {cases ? ( + <> + + + + + ) : ( + + )} + +