-
Notifications
You must be signed in to change notification settings - Fork 13
Feature/mn 175/factorize monitor code #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
605e0d4
feat: factorize monitoring code (MN-175)
293680e
Merge branch 'main' of https://github.com/monkvision/monkjs into feat…
71736ea
fix: merge with main branch
17cce46
fix: resolve linting issue
fe5762d
fix: increase trace sample rates to send data on sentry dashboard
917d572
feat: set user in Sentry and added custom measurement
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
| import { BrowserTracing } from '@sentry/tracing'; | ||
| import React, { createContext, PropsWithChildren, useCallback, useContext, useEffect, useMemo } from 'react'; | ||
| import { Primitive } from '@sentry/types'; | ||
|
|
||
| import { MonitoringContext, MonitoringProps, SentryTransactionStatus } from './types'; | ||
|
|
||
| export * from './types'; | ||
|
|
||
| /** | ||
| * Monitoring context which will create wrapper for monitoring functionality. | ||
| */ | ||
| export const Context = createContext<MonitoringContext | null>(null); | ||
|
|
||
| /** | ||
| * Monitoring wrapper used to abstract Sentry functionality. | ||
| * | ||
| * @param {MonitoringProps} data - Configuration for sentry to override default configuration. | ||
| * @return {React.ReactNode} | ||
| */ | ||
| export function MonitoringProvider({ children, config }: PropsWithChildren<MonitoringProps>) { | ||
| useEffect(() => { | ||
| Sentry.init({ | ||
| dsn: config.dsn, | ||
| environment: config.environment, | ||
| debug: config.debug, | ||
| tracesSampleRate: config.tracesSampleRate, | ||
| integrations: [ | ||
| new BrowserTracing({ tracePropagationTargets: config.tracingOrigins }), | ||
| ], | ||
| }); | ||
| }, []); | ||
|
|
||
| /** | ||
| * Updates user context information for future events. | ||
| * | ||
| * @param id {string} set user for in sentry | ||
| * @return {void} | ||
| */ | ||
| const setMonitoringUser = useCallback((id: string): void => { | ||
| Sentry.setUser({ id }); | ||
| }, []); | ||
|
|
||
| /** | ||
| * Set key:value that will be sent as tags data with the event. | ||
| * | ||
| * Can also be used to unset a tag, by passing `undefined`. | ||
| * | ||
| * @param key String key of tag | ||
| * @param value Value of tag | ||
| * @return {void} | ||
| */ | ||
| const setMonitoringTag = useCallback((key: string, value: Primitive): void => { | ||
| Sentry.setTag(key, value); | ||
| }, []); | ||
|
|
||
| /** | ||
| * Error handler function which is used to capture errors in sentry. | ||
| * | ||
| * @param error {Error | string} - Caught error that to be send to Sentry.io | ||
souyahia-monk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @returns {string | null} | ||
| */ | ||
| const errorHandler = useCallback((error: Error | string): string | null => { | ||
| if (!Sentry) { | ||
| return null; | ||
| } | ||
|
|
||
| return Sentry.captureException(error); | ||
| }, []); | ||
|
|
||
| /** | ||
souyahia-monk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Measure the performance of application based on functionality and operation based on it. | ||
| * Return type of the function is the IIFE, which will helps to close the transaction and complete the measurement. | ||
| * | ||
| * @param name {string} - Name of transaction | ||
| * @param operation {string} - Operation of transaction to be performed | ||
souyahia-monk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param [data] {{[key: string]: number | string}} - Data to be added on transaction | ||
| * @returns {() => void} - Which will helps to close the transaction and complete the measurement. | ||
| */ | ||
| const measurePerformance = useCallback((name: string, op: string, data?: { [key: string]: number | string }): (() => void) => { | ||
| // This will create a new Transaction | ||
| const transaction = Sentry.startTransaction({ name, data, op }); | ||
|
|
||
| // Set transaction on scope to associate with errors and get included span instrumentation | ||
| // If there's currently an unfinished transaction, it may be dropped | ||
| Sentry.getCurrentHub().configureScope((scope) => { | ||
| scope.setSpan(transaction); | ||
| }); | ||
|
|
||
| return () => { | ||
| transaction.setStatus(SentryTransactionStatus); | ||
| transaction.finish(); | ||
| }; | ||
| }, []); | ||
|
|
||
| /** | ||
| * Set the custom measurement on particular transaction | ||
| * | ||
| * @param transactionName Name of the transaction | ||
| * @param name Name of the measurement | ||
| * @param value Value of the measurement | ||
| * @param [unit] Unit of the measurement. (Defaults to an empty string) | ||
| * @return {void} | ||
| */ | ||
| const setMeasurement = useCallback((transactionName: string, name: string, value: number, unit?: string): void => { | ||
| const transaction = Sentry.startTransaction({ name: transactionName, op: name }); | ||
|
|
||
| setTimeout(() => { | ||
| transaction.setMeasurement(name, value, unit); | ||
| transaction.setMeasurement('frames_total', value, unit); | ||
souyahia-monk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| transaction.setStatus(SentryTransactionStatus); | ||
| transaction.finish(); | ||
| }, 100); | ||
| }, []); | ||
|
|
||
| const monitoringContextValue = useMemo( | ||
| () => ({ setMonitoringUser, setMonitoringTag, errorHandler, measurePerformance, setMeasurement }), | ||
| [setMonitoringUser, setMonitoringTag, errorHandler, measurePerformance, setMeasurement], | ||
| ); | ||
|
|
||
| return ( | ||
| <Context.Provider value={monitoringContextValue}> | ||
| {children} | ||
| </Context.Provider> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Custom hook which will provide monitoring context which will expose all the functionality. | ||
| */ | ||
| export function useMonitoring() { | ||
| return useContext(Context); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * Monitoring config interface | ||
| */ | ||
| export interface MonitoringConfig { | ||
| /** | ||
| * DSN key for sentry.io application | ||
| */ | ||
| dsn: string; | ||
| /** | ||
| * The current environment of your application (e.g. "production") | ||
| */ | ||
| environment: string; | ||
| /** | ||
| * Enable debug functionality in the SDK itself | ||
| */ | ||
| debug: boolean; | ||
| /** | ||
| * Sample rate to determine trace sampling. | ||
| * | ||
| * 0.0 = 0% chance of a given trace being sent (send no traces) 1.0 = 100% chance of a given trace being sent (send | ||
| * all traces) | ||
| * | ||
| * Tracing is enabled if either this or `tracesSampler` is defined. If both are defined, `tracesSampleRate` is | ||
| * ignored. | ||
| */ | ||
| tracesSampleRate: number; | ||
| /** | ||
| * Array of all the origin to browser trace. | ||
| */ | ||
| tracingOrigins: string[]; | ||
| } | ||
|
|
||
| /** | ||
| * Monitoring context interface | ||
| */ | ||
| export interface MonitoringContext { | ||
souyahia-monk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * Set current user for sentry. | ||
| */ | ||
| setMonitoringUser: (id: string) => void; | ||
|
|
||
| /** | ||
| * Store the error in the monitoring application. | ||
| */ | ||
| errorHandler: (error: Error | string) => string; | ||
|
|
||
| /** | ||
| * Start Measure Performance | ||
| */ | ||
| measurePerformance: (name: string, op: string, data: { [key: string]: number | string } | null) => (() => void); | ||
|
|
||
| /** | ||
| * Set custom measurement value | ||
| */ | ||
| setMeasurement: (transactionName: string, name: string, value: number, unit: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Monitoring configuration interface | ||
| */ | ||
| export interface MonitoringProps { | ||
| /** | ||
| * Configuration to initialize Sentry | ||
| */ | ||
| config: MonitoringConfig; | ||
| } | ||
|
|
||
| export const SentryTransactionStatus = 'success'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.