Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

added code for performance mark in binary #312

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
setConfigAppID,
} from '@storage';
import DerivAPIBasic from '@deriv/deriv-api/dist/DerivAPIBasic';
import { observer as globalObserver } from '@utilities/observer';
import APIMiddleware from './api-middleware';
import { observer as globalObserver } from '@utilities/observer';

Check failure on line 12 in src/api-base.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

`@utilities/observer` import should occur before import of `./api-middleware`

Check failure on line 12 in src/api-base.js

View workflow job for this annotation

GitHub Actions / test

`@utilities/observer` import should occur before import of `./api-middleware`

const socket_url = `wss://${getServerAddressFallback()}/websockets/v3?app_id=${getAppIdFallback()}&l=${getLanguage().toUpperCase()}&brand=deriv`;

Expand Down
94 changes: 25 additions & 69 deletions src/api-middleware.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,24 @@
import { datadogLogs } from '@datadog/browser-logs';
import { formatDate } from './utilities/utility-functions';

const DATADOG_CLIENT_LOGS_TOKEN = process.env.DATADOG_CLIENT_LOGS_TOKEN ?? '';
const isProduction = process.env.NODE_ENV === 'production';
const isStaging = process.env.NODE_ENV === 'staging';
let dataDogSessionSampleRate = 0;

dataDogSessionSampleRate = process.env.DATADOG_LOGS_SESSION_SAMPLE_RATE
? +process.env.DATADOG_LOGS_SESSION_SAMPLE_RATE
: 1;
let dataDogVersion = '';
let dataDogEnv = '';

if (isProduction) {
dataDogVersion = `binary-bot-${process.env.REF_NAME}`;
dataDogEnv = 'production';
} else if (isStaging) {
dataDogVersion = `binary-bot-staging-v${formatDate(new Date())}`;
dataDogEnv = 'staging';
}

if (DATADOG_CLIENT_LOGS_TOKEN) {
datadogLogs.init({
clientToken: DATADOG_CLIENT_LOGS_TOKEN,
site: 'datadoghq.com',
forwardErrorsToLogs: false,
service: 'BinaryBot',
sessionSampleRate: dataDogSessionSampleRate,
version: dataDogVersion,
env: dataDogEnv,
});
}

export const REQUESTS = [
'active_symbols',
'authorize',
'balance',
'buy',
'proposal',
'proposal_open_contract',
'run-proposal',
'transaction',
'ticks_history',
'history',
'history'

Check failure on line 11 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Missing trailing comma

Check failure on line 11 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / test

Missing trailing comma
];

class APIMiddleware {
constructor(config) {
this.config = config;
this.debounced_calls = {};
this.addGlobalMethod();
}

/* eslint-disable class-methods-use-this */
getRequestType = request => {

Check failure on line 21 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Expected 'this' to be used by class method 'getRequestType'

Check failure on line 21 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / test

Expected 'this' to be used by class method 'getRequestType'
let req_type;
REQUESTS.forEach(type => {
if (type in request && !req_type) req_type = type;
Expand All @@ -60,31 +27,27 @@
return req_type;
};

/* eslint-disable class-methods-use-this */
log = (measures = [], is_bot_running) => {
if (measures && measures.length) {
measures.forEach(measure => {
datadogLogs.logger.info(measure.name, {
name: measure.name,
startTime: measure.startTimeDate,
duration: measure.duration,
detail: measure.detail,
isBotRunning: is_bot_running,
});
});
}
};

/* eslint-disable class-methods-use-this */
defineMeasure = res_type => {

Check failure on line 30 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Expected 'this' to be used by class method 'defineMeasure'

Check failure on line 30 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / test

Expected 'this' to be used by class method 'defineMeasure'
if (res_type) {
let measure;
if (res_type === 'proposal') {
performance.mark('first_proposal_end');
if (performance.getEntriesByName('bot-start', 'mark').length) {
measure = performance.measure('run-proposal', 'bot-start', 'first_proposal_end');
performance.clearMarks('bot-start');
console.table('bot-first-run', measure.duration)

Check warning on line 38 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Unexpected console statement

Check warning on line 38 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / test

Unexpected console statement
}
}
if (res_type === 'history') {
performance.mark('ticks_history_end');
measure = performance.measure('ticks_history', 'ticks_history_start', 'ticks_history_end');
console.table('ticks_history', measure.duration)

Check warning on line 44 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Unexpected console statement

Check warning on line 44 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / test

Unexpected console statement
} else {
performance.mark(`${res_type}_end`);
measure = performance.measure(`${res_type}`, `${res_type}_start`, `${res_type}_end`);
if (res_type === 'proposal') {
console.table('proposal', measure.duration)

Check warning on line 49 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Unexpected console statement

Check warning on line 49 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / test

Unexpected console statement
}
}
return (measure.startTimeDate = new Date(Date.now() - measure.startTime));
}
Expand All @@ -94,26 +57,19 @@
sendIsCalled = ({ response_promise, args: [request] }) => {
const req_type = this.getRequestType(request);
if (req_type) performance.mark(`${req_type}_start`);
response_promise
.then(res => {
const res_type = this.getRequestType(res);
if (res_type) {
this.defineMeasure(res_type);
}
})
.catch(() => {});
response_promise.then(res => {
const res_type = this.getRequestType(res);
if (res_type) {
this.defineMeasure(res_type);
}
});
return response_promise;
};

sendRequestsStatistic = is_bot_running => {
REQUESTS.forEach(req_type => {
const measure = performance.getEntriesByName(req_type);
if (measure && measure.length) {
if (DATADOG_CLIENT_LOGS_TOKEN) {
this.log(measure, is_bot_running, req_type);
}
}
});
sendRequestsStatistic = () => {

Check failure on line 69 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Expected 'this' to be used by class method 'sendRequestsStatistic'

Check failure on line 69 in src/api-middleware.js

View workflow job for this annotation

GitHub Actions / test

Expected 'this' to be used by class method 'sendRequestsStatistic'
// REQUESTS.forEach(req_type => {
// const measure = performance.getEntriesByName(req_type);
// });
performance.clearMeasures();
};

Expand Down
5 changes: 4 additions & 1 deletion src/components/ToolBox/ToolBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ const ToolBox = ({
id_container='runButton'
tooltip={translate('Run the bot')}
position='bottom'
onClick={() => globalObserver.emit('blockly.start')}
onClick={() => {
globalObserver.emit("blockly.start")
performance.mark('bot-start');
}}
classes={classNames('toolbox-button icon-run', {
'toolbox-hide': !is_workspace_rendered,
})}
Expand Down
Loading