Log Axios Request perf logs
MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
@@ -406,7 +406,7 @@ export default class Logging {
console.warn(chalk.yellow('===================================='));
}
}
- void Logging.logSecurityDebug({
+ await Logging.logSecurityDebug({
tenantID: tenantID,
user: req.user,
action: ServerAction.HTTP_RESPONSE,
@@ -441,15 +441,31 @@ export default class Logging {
public static async traceAxiosRequest(tenant: Tenant, request: AxiosRequestConfig): Promise<void> {
request['timestamp'] = new Date();
+ // Compute Length
+ const sizeOfRequestDataKB = Utils.truncTo(Utils.createDecimal(
+ sizeof(request)).div(1024).toNumber(), 2);
+ const message = `Axios HTTP Request >> Req ${(sizeOfRequestDataKB > 0) ? sizeOfRequestDataKB : '?'} KB - ${request.method.toLocaleUpperCase()} '${request.url}'`;
+ Utils.isDevelopmentEnv() && console.debug(chalk.green(message));
await Logging.logSecurityDebug({
tenantID: tenant.id,
action: ServerAction.HTTP_REQUEST,
- message: `Axios HTTP Request >> ${request.method.toLocaleUpperCase()} '${request.url}'`,
module: Constants.MODULE_AXIOS, method: 'interceptor',
+ message,
detailedMessages: {
request: Utils.cloneObject(request),
}
});
+ const performanceID = await PerformanceStorage.savePerformanceRecord(
+ Utils.buildPerformanceRecord({
+ tenantSubdomain: tenant.subdomain,
+ group: Utils.getPerformanceRecordGroupFromURL(request.url),
+ httpUrl: request.url,
+ httpMethod: request.method.toLocaleUpperCase(),
+ reqSizeKb: sizeOfRequestDataKB,
+ action: ServerAction.HTTP_REQUEST,
+ })
+ );
+ request['performanceID'] = performanceID;
}
public static async traceAxiosResponse(tenant: Tenant, response: AxiosResponse): Promise<void> {
@@ -459,8 +475,6 @@ export default class Logging {
executionDurationMillis = (new Date().getTime() - response.config['timestamp'].getTime());
}
// Compute Length
- const sizeOfRequestDataKB = Utils.truncTo(Utils.createDecimal(
- sizeof(response.config)).div(1024).toNumber(), 2);
let sizeOfResponseDataKB = 0;
if (response.config.headers['Content-Length']) {
sizeOfResponseDataKB = Utils.truncTo(
@@ -469,7 +483,7 @@ export default class Logging {
sizeOfResponseDataKB = Utils.truncTo(
Utils.createDecimal(sizeof(response.data)).div(1024).toNumber(), 2);
}
- const message = `Axios HTTP Response - ${(executionDurationMillis > 0) ? executionDurationMillis : '?'} ms - Req ${(sizeOfRequestDataKB > 0) ? sizeOfRequestDataKB : '?'} KB - Res ${(sizeOfResponseDataKB > 0) ? sizeOfResponseDataKB : '?'} KB << ${response.config.method.toLocaleUpperCase()}/${response.status} '${response.config.url}'`;
+ const message = `Axios HTTP Response << ${(executionDurationMillis > 0) ? executionDurationMillis : '?'} ms - Res ${(sizeOfResponseDataKB > 0) ? sizeOfResponseDataKB : '?'} KB << ${response.config.method.toLocaleUpperCase()}/${response.status} '${response.config.url}'`;
Utils.isDevelopmentEnv() && console.log(chalk.green(message));
if (sizeOfResponseDataKB > Constants.PERF_MAX_DATA_VOLUME_KB) {
const error = new Error(`Data must be < ${Constants.PERF_MAX_DATA_VOLUME_KB}`);
@@ -521,24 +535,20 @@ export default class Logging {
response: Utils.cloneObject(response.data)
}
});
- await PerformanceStorage.savePerformanceRecord(
- Utils.buildPerformanceRecord({
- tenantSubdomain: tenant.subdomain,
- group: Utils.getPerformanceRecordGroupFromURL(response.config.url),
- httpUrl: response.config.url,
+ if (response.config['performanceID']) {
+ const performanceRecord = {
+ id: response.config['performanceID'],
httpResponseCode: response.status,
- httpMethod: response.config.method.toLocaleUpperCase(),
durationMs: executionDurationMillis,
- reqSizeKb: sizeOfRequestDataKB,
resSizeKb: sizeOfResponseDataKB,
- action: ServerAction.HTTP_RESPONSE,
- })
- );
+ } as PerformanceRecord;
+ await PerformanceStorage.updatePerformanceRecord(performanceRecord);
+ }
} catch (error) {
await Logging.logSecurityDebug({
tenantID: tenant.id,
action: ServerAction.HTTP_RESPONSE,
- message: `Axios HTTP Response - ${(executionDurationMillis > 0) ? executionDurationMillis : '?'} ms - Req ${(sizeOfRequestDataKB > 0) ? sizeOfRequestDataKB : '?'} KB - Res ${(sizeOfResponseDataKB > 0) ? sizeOfResponseDataKB : '?'} KB << ${response.config.method.toLocaleUpperCase()}/${response.status} '${response.config.url}'`,
+ message: `Axios HTTP Response - ${(executionDurationMillis > 0) ? executionDurationMillis : '?'} ms - Res ${(sizeOfResponseDataKB > 0) ? sizeOfResponseDataKB : '?'} KB << ${response.config.method.toLocaleUpperCase()}/${response.status} '${response.config.url}'`,
module: Constants.MODULE_AXIOS, method: 'logAxiosResponse',
detailedMessages: {
status: response.status,
@@ -564,6 +574,24 @@ export default class Logging {
axiosError: Utils.objectHasProperty(error, 'toJSON') ? error.toJSON() : null,
}
});
+ if (error.response?.config['performanceID']) {
+ let executionDurationMillis: number;
+ let sizeOfResponseDataKB = 0;
+ if (error.response?.config['timestamp']) {
+ executionDurationMillis = (new Date().getTime() - error.response?.config['timestamp'].getTime());
+ }
+ if (error.response?.data) {
+ sizeOfResponseDataKB = Utils.truncTo(
+ Utils.createDecimal(sizeof(error.response?.data)).div(1024).toNumber(), 2);
+ }
+ const performanceRecord = {
+ id: error.response?.config['performanceID'],
+ httpResponseCode: error.response?.status,
+ durationMs: executionDurationMillis,
+ resSizeKb: sizeOfResponseDataKB,
+ } as PerformanceRecord;
+ await PerformanceStorage.updatePerformanceRecord(performanceRecord);
+ }
}
// Used to log exception in catch(...) only

Log Axios Request perf logs