-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪟🔧 AppMonitoringService for custom datadog RUM events (#19287)
- Loading branch information
Showing
6 changed files
with
123 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
56 changes: 56 additions & 0 deletions
56
airbyte-webapp/src/hooks/services/AppMonitoringService/AppMonitoringService.tsx
This file contains 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,56 @@ | ||
import { datadogRum } from "@datadog/browser-rum"; | ||
import React, { createContext, useContext } from "react"; | ||
|
||
import { AppActionCodes } from "./actionCodes"; | ||
|
||
const appMonitoringContext = createContext<AppMonitoringServiceProviderValue | null>(null); | ||
|
||
/** | ||
* The AppMonitoringService exposes methods for tracking actions and errors from the webapp. | ||
* These methods are particularly useful for tracking when unexpected or edge-case conditions | ||
* are encountered in production. | ||
*/ | ||
interface AppMonitoringServiceProviderValue { | ||
/** | ||
* Log a custom action in datadog. Useful for tracking edge cases or unexpected application states. | ||
*/ | ||
trackAction: (actionCode: AppActionCodes, context?: Record<string, unknown>) => void; | ||
/** | ||
* Log a custom error in datadog. Useful for tracking edge case errors while handling them in the UI. | ||
*/ | ||
trackError: (error: Error, context?: Record<string, unknown>) => void; | ||
} | ||
|
||
export const useAppMonitoringService = (): AppMonitoringServiceProviderValue => { | ||
const context = useContext(appMonitoringContext); | ||
if (context === null) { | ||
throw new Error("useAppMonitoringService must be used within a AppMonitoringServiceProvider"); | ||
} | ||
|
||
return context; | ||
}; | ||
|
||
/** | ||
* This implementation of the AppMonitoringService uses the datadog SDK to track errors and actions | ||
*/ | ||
export const AppMonitoringServiceProvider: React.FC<React.PropsWithChildren<unknown>> = ({ children }) => { | ||
const trackAction = (action: string, context?: Record<string, unknown>) => { | ||
if (!datadogRum.getInternalContext()) { | ||
console.debug(`trackAction(${action}) failed because RUM is not initialized.`); | ||
return; | ||
} | ||
|
||
datadogRum.addAction(action, context); | ||
}; | ||
|
||
const trackError = (error: Error, context?: Record<string, unknown>) => { | ||
if (!datadogRum.getInternalContext()) { | ||
console.debug(`trackError() failed because RUM is not initialized. \n`, error); | ||
return; | ||
} | ||
|
||
datadogRum.addError(error, context); | ||
}; | ||
|
||
return <appMonitoringContext.Provider value={{ trackAction, trackError }}>{children}</appMonitoringContext.Provider>; | ||
}; |
11 changes: 11 additions & 0 deletions
11
airbyte-webapp/src/hooks/services/AppMonitoringService/actionCodes.ts
This file contains 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,11 @@ | ||
/** | ||
* Action codes are used to log specific runtime events that we want to analyse in datadog. | ||
* This is useful for tracking when and how frequently certain code paths on the frontend are | ||
* encountered in production. | ||
*/ | ||
export enum AppActionCodes { | ||
/** | ||
* LaunchDarkly did not load in time and was ignored | ||
*/ | ||
LD_LOAD_TIMEOUT = "LD_LOAD_TIMEOUT", | ||
} |
2 changes: 2 additions & 0 deletions
2
airbyte-webapp/src/hooks/services/AppMonitoringService/index.ts
This file contains 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,2 @@ | ||
export { AppMonitoringServiceProvider, useAppMonitoringService } from "./AppMonitoringService"; | ||
export { AppActionCodes } from "./actionCodes"; |
This file contains 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 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