Skip to content

Commit

Permalink
fix: 🔥 updated the configs, improvements and restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeep-deriv committed Nov 21, 2024
1 parent fbb3d75 commit 8cdd80f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ jobs:
GD_APP_ID: ${{ secrets.GD_APP_ID }}
GD_CLIENT_ID: ${{ secrets.GD_CLIENT_ID }}
TRACKJS_TOKEN: ${{ secrets.TRACKJS_TOKEN }}
DATADOG_APPLICATION_ID: ${{ secrets.DATADOG_APPLICATION_ID }}
DATADOG_CLIENT_TOKEN: ${{ secrets.DATADOG_CLIENT_TOKEN }}
DATADOG_APPLICATION_ID: ${{ vars.DATADOG_APPLICATION_ID }}
DATADOG_CLIENT_TOKEN: ${{ vars.DATADOG_CLIENT_TOKEN }}
DATADOG_SESSION_REPLAY_SAMPLE_RATE: ${{ vars.DATADOG_SESSION_REPLAY_SAMPLE_RATE }}
DATADOG_SESSION_SAMPLE_RATE: ${{ vars.DATADOG_SESSION_SAMPLE_RATE }}
REF_NAME: ${{ github.ref_name }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-and-deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ jobs:
GD_API_KEY: ${{ secrets.GD_API_KEY }}
GD_APP_ID: ${{ secrets.GD_APP_ID }}
GD_CLIENT_ID: ${{ secrets.GD_CLIENT_ID }}
DATADOG_APPLICATION_ID: ${{ secrets.DATADOG_APPLICATION_ID }}
DATADOG_CLIENT_TOKEN: ${{ secrets.DATADOG_CLIENT_TOKEN }}
DATADOG_APPLICATION_ID: ${{ vars.DATADOG_APPLICATION_ID }}
DATADOG_CLIENT_TOKEN: ${{ vars.DATADOG_CLIENT_TOKEN }}
DATADOG_SESSION_REPLAY_SAMPLE_RATE: ${{ vars.DATADOG_SESSION_REPLAY_SAMPLE_RATE }}
DATADOG_SESSION_SAMPLE_RATE: ${{ vars.DATADOG_SESSION_SAMPLE_RATE }}
REF_NAME: ${{ github.ref_name }}
Expand Down
4 changes: 1 addition & 3 deletions src/app/app-content.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const AppContent = observer(() => {
const { connectionStatus } = useApiBase();
const { initTrackJS } = useTrackjs();

console.log(client);
console.log('before initTrackJS');
initTrackJS();
initTrackJS(client.loginid);

useEffect(() => {
if (connectionStatus === CONNECTION_STATUS.OPENED) {
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useTrackjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ const { TRACKJS_TOKEN } = process.env;
* @returns {Object} An object containing the `init` function.
*/
const useTrackjs = () => {
const activeLoginid = '';
const isProduction = process.env.NODE_ENV === 'production';
const initTrackJS = () => {
const initTrackJS = (loginid: string) => {
try {
if (!TrackJS.isInstalled()) {
TrackJS.install({
application: 'standalone-deriv-bot',
dedupe: false,
enabled: isProduction,
token: TRACKJS_TOKEN!,
userId: activeLoginid ?? 'undefined',
userId: loginid,
version: (document.querySelector('meta[name=version]') as HTMLMetaElement)?.content ?? 'undefined',
});
}
Expand Down
88 changes: 36 additions & 52 deletions src/utils/datadog.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,55 @@
import { datadogRum } from '@datadog/browser-rum';

/**
* Gets the configuration values for datadog based on the environment.
* It returns null if the environment is not staging or production.
* @param {string} environment - The environment to get the configuration values for.
* @example getConfigValues('staging');
* @returns {object|null} - The configuration values for datadog.
* **/
const getConfigValues = (environment: string) => {
if (environment === 'production') {
const getConfigValues = (is_production: boolean) => {
if (is_production) {
return {
serviceName: 'deriv-bot',
dataDogVersion: `deriv-bot-${process.env.REF_NAME}`,
dataDogSessionReplaySampleRate: Number(process.env.DATADOG_SESSION_REPLAY_SAMPLE_RATE ?? 1),
dataDogSessionSampleRate: Number(process.env.DATADOG_SESSION_SAMPLE_RATE ?? 10),
dataDogEnv: 'production',
};
} else if (environment === 'staging') {
return {
serviceName: 'staging-dbot.deriv.com',
dataDogVersion: `deriv-dbot-staging-v${process.env.REF_NAME}`,
dataDogSessionReplaySampleRate: 0,
dataDogSessionSampleRate: 100,
dataDogEnv: 'staging',
service: 'dbot',
version: `v${process.env.REF_NAME}`,
sessionReplaySampleRate: Number(process.env.DATADOG_SESSION_REPLAY_SAMPLE_RATE ?? 1),
sessionSampleRate: Number(process.env.DATADOG_SESSION_SAMPLE_RATE ?? 10),
env: 'production',
applicationId: process.env.DATADOG_APPLICATION_ID ?? '',
clientToken: process.env.DATADOG_CLIENT_TOKEN ?? '',
};
}
return {
service: 'staging-dbot',
version: `v${process.env.REF_NAME}`,
sessionReplaySampleRate: 0,
sessionSampleRate: 100,
env: 'staging',
applicationId: process.env.DATADOG_APPLICATION_ID ?? '',
clientToken: process.env.DATADOG_CLIENT_TOKEN ?? '',
};
};

/**
* Initializes datadog for tracking user interactions, resources, long tasks, and frustrations on production or staging environments, conditionally based on environment variables.
* It also masks user input and redacts sensitive data from the URL.
*
* @param {boolean} is_datadog_enabled - The parameter to enable or disable datadog tracking.
* @example initDatadog(true);
* @returns {void}
* **/
const initDatadog = (is_datadog_enabled: boolean) => {
if (!is_datadog_enabled) return;
const DATADOG_APP_ID = process.env.DATADOG_APPLICATION_ID ?? '';
const DATADOG_CLIENT_TOKEN = process.env.DATADOG_CLIENT_TOKEN ?? '';
const isProduction = process.env.NODE_ENV === 'production';
const isStaging = process.env.NODE_ENV === 'staging';

const {
dataDogSessionSampleRate = 0,
dataDogSessionReplaySampleRate = 0,
dataDogVersion = '',
dataDogEnv = '',
serviceName = '',
} = getConfigValues(process.env.NODE_ENV ?? '') ?? {};
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') {
const is_production = process.env.NODE_ENV === 'production';
const {
service,
version,
sessionReplaySampleRate,
sessionSampleRate,
env,
applicationId = '',
clientToken = '',
} = getConfigValues(is_production) ?? {};

// we do it in order avoid error "application id is not configured, no RUM data will be collected"
// for test-links where application ID has not been configured and therefore RUM data will not be collected
if (isProduction || isStaging) {
datadogRum.init({
applicationId: isStaging || isProduction ? DATADOG_APP_ID : '',
clientToken: isStaging || isProduction ? DATADOG_CLIENT_TOKEN : '',
service,
version,
sessionReplaySampleRate,
sessionSampleRate,
env,
applicationId,
clientToken,
site: 'datadoghq.com',
service: serviceName,
env: dataDogEnv,
sessionSampleRate: dataDogSessionSampleRate,
sessionReplaySampleRate: dataDogSessionReplaySampleRate,
trackUserInteractions: true,
trackResources: true,
trackLongTasks: true,
defaultPrivacyLevel: 'mask-user-input',
version: dataDogVersion,
enableExperimentalFeatures: ['clickmap'],
});
}
Expand Down

0 comments on commit 8cdd80f

Please sign in to comment.