Skip to content

Commit

Permalink
[AC-2507] Add Datadog monitoring hook (#151)
Browse files Browse the repository at this point in the history
* Add datadog monitoring hook

* Address @bang9 comment
  • Loading branch information
AhyoungRyu authored Apr 19, 2024
1 parent c26dba2 commit 2c5c0cf
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
# Sendbird App ID
# VITE_CHAT_WIDGET_APP_ID=
# VITE_CHAT_WIDGET_BOT_ID=

# Only for internal use
# VITE_CHAT_AI_WIDGET_KEY=
# VITE_CHAT_AI_WIDGET_DATADOG_CLIENT_TOKEN=
# VITE_CHAT_AI_WIDGET_DATADOG_APP_ID=
4 changes: 3 additions & 1 deletion .github/workflows/gh-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ jobs:
run: |
npm install
cp .env .env.production
echo "VITE_CHAT_AI_WIDGET_KEY=${{ secrets.chat_ai_widget_key }}" >> .env.production
echo "VITE_CHAT_AI_WIDGET_KEY=${{ secrets.chat_ai_widget_key }}" >> .env.production && \
echo "VITE_CHAT_AI_WIDGET_DATADOG_CLIENT_TOKEN=${{ secrets.datadog_client_token }}" >> .env.production && \
echo "VITE_CHAT_AI_WIDGET_DATADOG_APP_ID=${{ secrets.datadog_app_id }}" >> .env.production
npm run build:pages
- name: Deploy 🚀
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/package-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ jobs:
run: |
npm install
touch .env.production
echo "VITE_CHAT_AI_WIDGET_KEY=${{ secrets.chat_ai_widget_key }}" >> .env.production
echo "VITE_CHAT_AI_WIDGET_KEY=${{ secrets.chat_ai_widget_key }}" >> .env.production && \
echo "VITE_CHAT_AI_WIDGET_DATADOG_CLIENT_TOKEN=${{ secrets.datadog_client_token }}" >> .env.production && \
echo "VITE_CHAT_AI_WIDGET_DATADOG_APP_ID=${{ secrets.datadog_app_id }}" >> .env.production
npm run build:npm
- name: Publish to npm
run: |
Expand Down
191 changes: 36 additions & 155 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"styled-components": "^5.3.11"
},
"devDependencies": {
"@tanstack/eslint-plugin-query": "^5.17.22",
"@datadog/browser-rum": "^5.15.0",
"@types/dompurify": "^3.0.5",
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
Expand Down
1 change: 1 addition & 0 deletions packages/self-service/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function App() {
customUserAgentParam={{
'chat-ai-widget-deployed': 'True',
}}
serviceName="genai-self-service"
{...chatbotConfigs}
/>
);
Expand Down
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const App = (props: Props) => {
customUserAgentParam={props.customUserAgentParam}
autoOpen={props.autoOpen}
renderWidgetToggleButton={props.renderWidgetToggleButton}
serviceName={props.serviceName}
/>
);
};
Expand Down
2 changes: 2 additions & 0 deletions src/components/ProviderContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../context/ConstantContext';
import { WidgetOpenProvider } from '../context/WidgetOpenContext';
import { useChannelStyle } from '../hooks/useChannelStyle';
import useDatadogRum from '../hooks/useDatadog';
import useWidgetLocalStorage from '../hooks/useWidgetLocalStorage';
import { getTheme } from '../theme';
import { isMobile } from '../utils';
Expand All @@ -28,6 +29,7 @@ const SBComponent = ({ children }: { children: React.ReactElement }) => {
stringSet,
...restConstantProps
} = useConstantState();
useDatadogRum();

const userAgentCustomParams = useRef({
...customUserAgentParam,
Expand Down
1 change: 1 addition & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface Constant {
onClick: () => void;
isOpen: boolean;
}) => React.ReactElement;
serviceName?: string;
}

export interface SuggestedReply {
Expand Down
1 change: 1 addition & 0 deletions src/context/ConstantContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const ConstantStateProvider = (props: ProviderProps) => {
enableMobileView: props.enableMobileView ?? initialState.enableMobileView,
autoOpen: props.autoOpen,
renderWidgetToggleButton: props.renderWidgetToggleButton,
serviceName: props.serviceName,
}),
[props]
);
Expand Down
38 changes: 38 additions & 0 deletions src/hooks/useDatadog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { datadogRum } from '@datadog/browser-rum';
import { useEffect } from 'react';

import { useConstantState } from '../context/ConstantContext';

const DATADOG_APP_ID = import.meta.env.VITE_CHAT_AI_WIDGET_DATADOG_APP_ID;
const DATADOG_CLIENT_TOKEN = import.meta.env
.VITE_CHAT_AI_WIDGET_DATADOG_CLIENT_TOKEN;
const isProd = import.meta.env.PROD ? 'prod' : 'dev';

const useDatadogRum = () => {
const { serviceName } = useConstantState();
useEffect(() => {
if (DATADOG_APP_ID != null && DATADOG_CLIENT_TOKEN != null) {
datadogRum.init({
applicationId: DATADOG_APP_ID,
clientToken: DATADOG_CLIENT_TOKEN,
site: 'datadoghq.com',
sessionSampleRate: 100,
sessionReplaySampleRate: 100,
trackResources: true,
trackLongTasks: true,
trackUserInteractions: true,
service: serviceName || 'genai-chatbot-widget',
version: APP_VERSION,
env: isProd ? 'production' : 'development',
});

datadogRum.startSessionReplayRecording();
}

return () => {
datadogRum.stopSessionReplayRecording();
};
}, []);
};

export default useDatadogRum;
1 change: 1 addition & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference types="vite/client" />
declare const APP_VERSION: string;
5 changes: 5 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export default defineConfig({
svgr(),
dts(),
],
define: {
APP_VERSION: JSON.stringify(process.env.npm_package_version),
},
// to point to correct path for gh-pages
base: '/chat-ai-widget',
build: {
Expand All @@ -29,11 +32,13 @@ export default defineConfig({
external: [
'react',
'react-dom',
'@datadog/browser-rum'
],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'@datadog/browser-rum': 'datadogRum'
}
}
}
Expand Down

0 comments on commit 2c5c0cf

Please sign in to comment.