1- import React , { createContext , useEffect , useMemo } from 'react' ;
1+ import React , { createContext , useContext , useEffect , useMemo } from 'react' ;
22import { Primitive } from '@sentry/types' ;
33import { CaptureConsole } from '@sentry/integrations' ;
44import { Integrations } from '@sentry/tracing' ;
@@ -12,22 +12,22 @@ import { MonitoringContextType } from './types';
1212
1313const tracingOrigins = [ 'localhost' , 'cna.dev.monk.ai' , 'cna.staging.monk.ai' , 'cna.preview.monk.ai' , 'cna.monk.ai' ] ;
1414const MonitoringConstants = {
15- type : {
16- UPLOAD : 'upload' , // logs linked to the upload
17- CAMERA : 'camera' , // logs linked to the camera
18- FUNC : 'func' , // logs linked to a function
19- APP : 'app' , // logs linked to the application
20- HTTP : 'http' , // logs linked to the api
21- } ,
22- operation : {
23- HTTP : 'http' ,
24- USER_TIME : 'user-time-per-action' ,
25- USER_CAMERA_TIME : 'user-camera-time' ,
26- USER_UPLOAD_CENTER_TIME : 'user-upload-center-time' ,
27- USER_ACTION : 'user-action' ,
28- RESPONSE_TIME : 'response-time' ,
29- FUNC : 'func' ,
30- } ,
15+ type : {
16+ UPLOAD : 'upload' , // logs linked to the upload
17+ CAMERA : 'camera' , // logs linked to the camera
18+ FUNC : 'func' , // logs linked to a function
19+ APP : 'app' , // logs linked to the application
20+ HTTP : 'http' , // logs linked to the api
21+ } ,
22+ operation : {
23+ HTTP : 'http' ,
24+ USER_TIME : 'user-time-per-action' ,
25+ USER_CAMERA_TIME : 'user-camera-time' ,
26+ USER_UPLOAD_CENTER_TIME : 'user-upload-center-time' ,
27+ USER_ACTION : 'user-action' ,
28+ RESPONSE_TIME : 'response-time' ,
29+ FUNC : 'func' ,
30+ } ,
3131} ;
3232
3333const Tracing = Integrations . BrowserTracing ;
@@ -36,91 +36,95 @@ const setUser = (id: string) => SentryR.setUser({ id });
3636const MonitoringContext = createContext < MonitoringContextType | null > ( null ) ;
3737
3838function MonitoringProvider ( { children, config } ) {
39- useEffect ( ( ) => {
40- Sentry . init ( {
41- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
42- dsn : ( config . dns || Constants . manifest . extra . SENTRY_DSN ) as string ,
43- environment : Constants . manifest . extra . ENV as string ,
44- debug : Constants . manifest . extra . ENV !== 'production' ,
45- enableAutoSessionTracking : true ,
46- enableInExpoDevelopment : true ,
47- sessionTrackingIntervalMillis : 10000 ,
48- tracesSampleRate : Constants . manifest . extra . ENV !== 'production' ? 1.0 : 0.2 ,
49- integrations : [
50- ...( Platform . select ( {
51- web : [ new CaptureConsole ( { levels : [ 'log' ] } ) ] ,
52- native : [ ] ,
53- } ) ) ,
54- new Tracing ( {
55- tracingOrigins,
56- } ) ,
57- ] ,
58- } as Sentry . SentryExpoNativeOptions ) ;
59- } , [ ] ) ;
39+ useEffect ( ( ) => {
40+ Sentry . init ( {
41+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
42+ dsn : ( config . dns || Constants . manifest . extra . SENTRY_DSN ) as string ,
43+ environment : Constants . manifest . extra . ENV as string ,
44+ debug : Constants . manifest . extra . ENV !== 'production' ,
45+ enableAutoSessionTracking : true ,
46+ enableInExpoDevelopment : true ,
47+ sessionTrackingIntervalMillis : 10000 ,
48+ tracesSampleRate : Constants . manifest . extra . ENV !== 'production' ? 1.0 : 0.2 ,
49+ integrations : [
50+ ...( Platform . select ( {
51+ web : [ new CaptureConsole ( { levels : [ 'log' ] } ) ] ,
52+ native : [ ] ,
53+ } ) ) ,
54+ new Tracing ( {
55+ tracingOrigins,
56+ } ) ,
57+ ] ,
58+ } as Sentry . SentryExpoNativeOptions ) ;
59+ } , [ ] ) ;
6060
61- /**
62- * @param error {Error} - Caught error to send to Sentry.io
63- * @param type {string} - tag of the error's type
64- * @param extras {Record} - Useful information that can be sent (request's body for example)
65- * @param additionalTags - (Optional) Additional tags to add to the error
66- */
67- const errorHandler = ( error : Error | string ) : string => {
68- if ( ! Sentry || ( ! Sentry ?. Browser && ! Sentry ?. Native ) ) { return null ; }
61+ /**
62+ * @param error {Error} - Caught error to send to Sentry.io
63+ * @param type {string} - tag of the error's type
64+ * @param extras {Record} - Useful information that can be sent (request's body for example)
65+ * @param additionalTags - (Optional) Additional tags to add to the error
66+ */
67+ const errorHandler = ( error : Error | string ) : string => {
68+ if ( ! Sentry || ( ! Sentry ?. Browser && ! Sentry ?. Native ) ) { return null ; }
6969
70- return Platform . select ( {
71- web : Sentry . Browser ,
72- native : Sentry . Browser ,
73- } ) . captureException ( error ) ;
74- } ;
70+ return Platform . select ( {
71+ web : Sentry . Browser ,
72+ native : Sentry . Browser ,
73+ } ) . captureException ( error ) ;
74+ } ;
7575
76- /**
77- * @param name {string} - Name of transaction
78- * @param operation {string} - Operation of transaction to be performed
79- * @param data {{[key: string]: number | string}} - Data to be added on transaction
80- */
81- const measurePerformance = ( name : string , operation : string , data : { [ key : string ] : number | string } | null ) => {
82- const transaction = Platform . select ( {
83- web : Sentry . Browser ,
84- native : Sentry . Browser ,
85- } ) . startTransaction ( { name, data } ) ;
86- const transactionOperation = transaction . startChild ( { op : operation } ) ;
76+ /**
77+ * @param name {string} - Name of transaction
78+ * @param operation {string} - Operation of transaction to be performed
79+ * @param data {{[key: string]: number | string}} - Data to be added on transaction
80+ */
81+ const measurePerformance = ( name : string , operation : string , data : { [ key : string ] : number | string } | null ) => {
82+ const transaction = Platform . select ( {
83+ web : Sentry . Browser ,
84+ native : Sentry . Browser ,
85+ } ) . startTransaction ( { name, data } ) ;
86+ const transactionOperation = transaction . startChild ( { op : operation } ) ;
8787
88- return ( ) => {
89- transactionOperation . finish ( ) ;
90- transaction . finish ( ) ;
91- } ;
92- } ;
88+ return ( ) => {
89+ transactionOperation . finish ( ) ;
90+ transaction . finish ( ) ;
91+ } ;
92+ } ;
9393
9494 const monitoringContextValue = useMemo (
9595 ( ) => ( { errorHandler, measurePerformance } ) ,
9696 [ errorHandler , measurePerformance ] ,
9797 ) ;
9898
99- return (
100- < MonitoringContext . Provider value = { monitoringContextValue } >
101- { children }
102- </ MonitoringContext . Provider >
103- ) ;
99+ return (
100+ < MonitoringContext . Provider value = { monitoringContextValue } >
101+ { children }
102+ </ MonitoringContext . Provider >
103+ ) ;
104104}
105105
106- export default MonitoringProvider ;
106+ const useMonitoring = ( ) => {
107+ return useContext ( MonitoringContext ) ;
108+ }
107109
108110export {
109- MonitoringContext ,
110- setTag ,
111- setUser ,
112- Tracing ,
113111 MonitoringConstants ,
112+ MonitoringContext ,
113+ setTag ,
114+ setUser ,
115+ Tracing ,
116+ MonitoringProvider ,
117+ useMonitoring ,
114118} ;
115119
116120MonitoringProvider . propTypes = {
117- config : PropTypes . shape ( {
118- dns : PropTypes . string ,
119- } ) ,
121+ config : PropTypes . shape ( {
122+ dns : PropTypes . string ,
123+ } ) ,
120124} ;
121125
122126MonitoringProvider . defaultProps = {
123- config : {
124- dns : '' ,
125- } ,
127+ config : {
128+ dns : '' ,
129+ } ,
126130} ;
0 commit comments