|
| 1 | +import * as Sentry from '@sentry/browser'; |
| 2 | +import { BrowserTracing } from '@sentry/tracing'; |
| 3 | +import React, { createContext, PropsWithChildren, useCallback, useContext, useEffect, useMemo } from 'react'; |
| 4 | +import { Primitive } from '@sentry/types'; |
| 5 | + |
| 6 | +import { MonitoringContext, MonitoringProps, SentryTransactionStatus } from './types'; |
| 7 | + |
| 8 | +export * from './types'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Monitoring context which will create wrapper for monitoring functionality. |
| 12 | +*/ |
| 13 | +export const Context = createContext<MonitoringContext | null>(null); |
| 14 | + |
| 15 | +/** |
| 16 | + * Monitoring wrapper used to abstract Sentry functionality. |
| 17 | + * |
| 18 | + * @param {MonitoringProps} data - Configuration for sentry to override default configuration. |
| 19 | + * @return {React.ReactNode} |
| 20 | +*/ |
| 21 | +export function MonitoringProvider({ children, config }: PropsWithChildren<MonitoringProps>) { |
| 22 | + useEffect(() => { |
| 23 | + Sentry.init({ |
| 24 | + dsn: config.dsn, |
| 25 | + environment: config.environment, |
| 26 | + debug: config.debug, |
| 27 | + tracesSampleRate: config.tracesSampleRate, |
| 28 | + integrations: [ |
| 29 | + new BrowserTracing({ tracePropagationTargets: config.tracingOrigins }), |
| 30 | + ], |
| 31 | + }); |
| 32 | + }, []); |
| 33 | + |
| 34 | + /** |
| 35 | + * Updates user context information for future events. |
| 36 | + * |
| 37 | + * @param id {string} set user for in sentry |
| 38 | + * @return {void} |
| 39 | + */ |
| 40 | + const setMonitoringUser = useCallback((id: string): void => { |
| 41 | + Sentry.setUser({ id }); |
| 42 | + }, []); |
| 43 | + |
| 44 | + /** |
| 45 | + * Set key:value that will be sent as tags data with the event. |
| 46 | + * |
| 47 | + * Can also be used to unset a tag, by passing `undefined`. |
| 48 | + * |
| 49 | + * @param key String key of tag |
| 50 | + * @param value Value of tag |
| 51 | + * @return {void} |
| 52 | + */ |
| 53 | + const setMonitoringTag = useCallback((key: string, value: Primitive): void => { |
| 54 | + Sentry.setTag(key, value); |
| 55 | + }, []); |
| 56 | + |
| 57 | + /** |
| 58 | + * Error handler function which is used to capture errors in sentry. |
| 59 | + * |
| 60 | + * @param error {Error | string} - Caught error that to be send to Sentry.io |
| 61 | + * @returns {string | null} |
| 62 | + */ |
| 63 | + const errorHandler = useCallback((error: Error | string): string | null => { |
| 64 | + if (!Sentry) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + return Sentry.captureException(error); |
| 69 | + }, []); |
| 70 | + |
| 71 | + /** |
| 72 | + * Measure the performance of application based on functionality and operation based on it. |
| 73 | + * Return type of the function is the IIFE, which will helps to close the transaction and complete the measurement. |
| 74 | + * |
| 75 | + * @param name {string} - Name of transaction |
| 76 | + * @param operation {string} - Operation of transaction to be performed |
| 77 | + * @param [data] {{[key: string]: number | string}} - Data to be added on transaction |
| 78 | + * @returns {() => void} - Which will helps to close the transaction and complete the measurement. |
| 79 | + */ |
| 80 | + const measurePerformance = useCallback((name: string, op: string, data?: { [key: string]: number | string }): (() => void) => { |
| 81 | + // This will create a new Transaction |
| 82 | + const transaction = Sentry.startTransaction({ name, data, op }); |
| 83 | + |
| 84 | + // Set transaction on scope to associate with errors and get included span instrumentation |
| 85 | + // If there's currently an unfinished transaction, it may be dropped |
| 86 | + Sentry.getCurrentHub().configureScope((scope) => { |
| 87 | + scope.setSpan(transaction); |
| 88 | + }); |
| 89 | + |
| 90 | + return () => { |
| 91 | + transaction.setStatus(SentryTransactionStatus); |
| 92 | + transaction.finish(); |
| 93 | + }; |
| 94 | + }, []); |
| 95 | + |
| 96 | + /** |
| 97 | + * Set the custom measurement on particular transaction |
| 98 | + * |
| 99 | + * @param transactionName Name of the transaction |
| 100 | + * @param name Name of the measurement |
| 101 | + * @param value Value of the measurement |
| 102 | + * @param [unit] Unit of the measurement. (Defaults to an empty string) |
| 103 | + * @return {void} |
| 104 | + */ |
| 105 | + const setMeasurement = useCallback((transactionName: string, name: string, value: number, unit?: string): void => { |
| 106 | + const transaction = Sentry.startTransaction({ name: transactionName, op: name }); |
| 107 | + |
| 108 | + setTimeout(() => { |
| 109 | + transaction.setMeasurement(name, value, unit); |
| 110 | + transaction.setMeasurement('frames_total', value, unit); |
| 111 | + transaction.setStatus(SentryTransactionStatus); |
| 112 | + transaction.finish(); |
| 113 | + }, 100); |
| 114 | + }, []); |
| 115 | + |
| 116 | + const monitoringContextValue = useMemo( |
| 117 | + () => ({ setMonitoringUser, setMonitoringTag, errorHandler, measurePerformance, setMeasurement }), |
| 118 | + [setMonitoringUser, setMonitoringTag, errorHandler, measurePerformance, setMeasurement], |
| 119 | + ); |
| 120 | + |
| 121 | + return ( |
| 122 | + <Context.Provider value={monitoringContextValue}> |
| 123 | + {children} |
| 124 | + </Context.Provider> |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Custom hook which will provide monitoring context which will expose all the functionality. |
| 130 | +*/ |
| 131 | +export function useMonitoring() { |
| 132 | + return useContext(Context); |
| 133 | +} |
0 commit comments