Added labels(2)
MODIFY
package-lock.json
package-lock.json
"chalk": "4.1.2",
"cors": "^2.8.5",
"countries-list": "^2.6.1",
+ "crypto": "^1.0.1",
"csvtojson": "^2.0.10",
"curvereduce": "^0.0.4",
"decimal.js": "^10.4.2",
"node": "*"
}
},
+ "node_modules/crypto": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
+ "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==",
+ "deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in."
+ },
"node_modules/crypto-browserify": {
"version": "3.12.0",
"resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz",
"resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz",
"integrity": "sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow=="
},
+ "crypto": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
+ "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig=="
+ },
"crypto-browserify": {
"version": "3.12.0",
"resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz",

MODIFY
package.json
package.json
"chalk": "4.1.2",
"cors": "^2.8.5",
"countries-list": "^2.6.1",
+ "crypto": "^1.0.1",
"csvtojson": "^2.0.10",
"curvereduce": "^0.0.4",
"decimal.js": "^10.4.2",

MODIFY
src/monitoring/MonitoringServer.ts
src/monitoring/MonitoringServer.ts
@@ -3,6 +3,6 @@ import client from 'prom-client';
export default abstract class MonitoringServer {
public abstract start(): void;
public abstract getGauge(name: string): client.Gauge | undefined;
- public abstract createGaugeMetric(metricname : string, metrichelp : string) : client.Gauge;
+ public abstract createGaugeMetric(metricname : string, metrichelp : string, labelNames? : string[]) : client.Gauge;
}

MODIFY
src/monitoring/prometheus/PrometheusMonitoringServer.ts
src/monitoring/prometheus/PrometheusMonitoringServer.ts
@@ -9,6 +9,7 @@ import MonitoringConfiguration from '../../types/configuration/MonitoringConfigu
import MonitoringServer from '../MonitoringServer';
import { ServerUtils } from '../../server/ServerUtils';
import global from '../../types/GlobalType';
+import { isArray } from 'lodash';
const MODULE_NAME = 'PrometheusMonitoringServer';
@@ -65,11 +66,23 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
ServerUtils.createHttpServer(this.monitoringConfig, this.expressApplication), MODULE_NAME, ServerType.MONITORING_SERVER);
}
- public createGaugeMetric(metricname : string, metrichelp : string) : Gauge {
- const gaugeMetric : client.Gauge = new client.Gauge({
+ public createGaugeMetric(metricname : string, metrichelp : string, labelNames? : string[]) : Gauge {
+
+ let gaugeMetric : client.Gauge;
+
+
+ if (Array.isArray(labelNames)) {
+ gaugeMetric = new client.Gauge({
name: metricname,
- help: metrichelp
+ help: metrichelp,
+ labelNames: labelNames
});
+ } else {
+ gaugeMetric = new client.Gauge({
+ name: metricname,
+ help: metrichelp });
+ }
+
this.mapGauge.set(metricname, gaugeMetric);
this.clientRegistry.registerMetric(gaugeMetric);
return gaugeMetric;

MODIFY
src/server/ocpp/json/JsonOCPPServer.ts
src/server/ocpp/json/JsonOCPPServer.ts
@@ -29,12 +29,9 @@ export default class JsonOCPPServer extends OCPPServer {
private waitingWSMessages = 0;
private runningWSMessages = 0;
private runningWSRequestsMessages: Record<string, boolean> = {};
- private freedWSRequestsMessages: Record<string, boolean> = {};
- private onOpenTime: Record<string, number> = {};
private jsonWSConnections: Map<string, JsonWSConnection> = new Map();
private jsonRestWSConnections: Map<string, JsonRestWSConnection> = new Map();
-
public constructor(centralSystemConfig: CentralSystemConfiguration, chargingStationConfig: ChargingStationConfiguration) {
super(centralSystemConfig, chargingStationConfig);
// Start job to clean WS connections
@@ -58,7 +55,7 @@ export default class JsonOCPPServer extends OCPPServer {
},
open: async (ws: WebSocket) => {
// Delegate
- void await this.onOpen(ws);
+ await this.onOpen(ws);
},
message: async (ws: WebSocket, message: ArrayBuffer, isBinary: boolean) => {
// Delegate
@@ -222,38 +219,24 @@ export default class JsonOCPPServer extends OCPPServer {
private async onOpen(ws: uWS.WebSocket) {
// Create WS Wrapper
-
-
- const start = Date.now();
const wsWrapper = new WSWrapper(ws);
-
- this.onOpenTime[wsWrapper.url] = 0;
- wsWrapper.finishedOnOpen = false;
- wsWrapper.insideOnOpen = true;
// Keep it on the ws
ws.wsWrapper = wsWrapper;
// Lock incoming WS messages
await this.aquireLockForWSRequest(WebSocketAction.OPEN, ServerAction.WS_SERVER_CONNECTION_OPEN, wsWrapper);
-
-
try {
-
-
this.runningWSMessages++;
-
// Path must contain /OCPP16 or /REST as it is already checked during the Upgrade process
// Check OCPP16 connection
if (wsWrapper.url.startsWith('/OCPP16')) {
// Create and Initialize WS Connection
- await this.checkAndStoreWSOpenedConnection(WSServerProtocol.OCPP16, wsWrapper, start);
+ await this.checkAndStoreWSOpenedConnection(WSServerProtocol.OCPP16, wsWrapper);
}
// Check REST connection
if (wsWrapper.url.startsWith('/REST')) {
// Create and Initialize WS Connection
- await this.checkAndStoreWSOpenedConnection(WSServerProtocol.REST, wsWrapper, start);
+ await this.checkAndStoreWSOpenedConnection(WSServerProtocol.REST, wsWrapper);
}
-
-
} catch (error) {
await Logging.logException(error, ServerAction.WS_SERVER_CONNECTION_OPEN, MODULE_NAME, 'onOpen', Constants.DEFAULT_TENANT_ID);
if (wsWrapper.tenantID) {
@@ -265,14 +248,10 @@ export default class JsonOCPPServer extends OCPPServer {
} finally {
this.runningWSMessages--;
this.releaseLockForWSMessageRequest(wsWrapper);
-
- const millis = Date.now() - start;
- this.onOpenTime[wsWrapper.url] = millis;
- wsWrapper.finishedOnOpen = true;
}
}
- private async checkAndStoreWSOpenedConnection(protocol: WSServerProtocol, wsWrapper: WSWrapper, start : any): Promise<void> {
+ private async checkAndStoreWSOpenedConnection(protocol: WSServerProtocol, wsWrapper: WSWrapper): Promise<void> {
let wsConnection: WSConnection;
const timeStart = Date.now();
// Set the protocol
@@ -291,11 +270,7 @@ export default class JsonOCPPServer extends OCPPServer {
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
// Initialize (check of Tenant, Token, Charging Station -> Can take time)
-
-
await wsConnection.initialize();
-
-
// Check if WS is still opened (long time initialization when thousand of WS are connecting at the same time)
if (!wsWrapper.closed) {
// Keep common data (Set here to get Tenant info in case of exception in Logs)
@@ -306,12 +281,8 @@ export default class JsonOCPPServer extends OCPPServer {
wsWrapper.siteID = wsConnection.getSiteID();
wsWrapper.siteAreaID = wsConnection.getSiteAreaID();
wsWrapper.companyID = wsConnection.getCompanyID();
-
-
// Check already existing WS Connection
await this.checkAndCloseIdenticalOpenedWSConnection(wsWrapper, wsConnection);
-
-
const message = `${WebSocketAction.OPEN} > WS Connection ID '${wsWrapper.guid}' has been accepted in ${Utils.computeTimeDurationSecs(timeStart)} secs`;
await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT_ID,
@@ -324,18 +295,12 @@ export default class JsonOCPPServer extends OCPPServer {
action: ServerAction.WS_SERVER_CONNECTION_OPEN, module: MODULE_NAME, method: 'checkAndStoreWSOpenedConnection',
message, detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
-
-
// Keep WS connection in cache
await this.setWSConnection(WebSocketAction.OPEN, ServerAction.WS_SERVER_CONNECTION_OPEN, wsConnection, wsWrapper);
-
-
} else {
await this.logWSConnectionClosed(wsWrapper, ServerAction.WS_SERVER_CONNECTION_OPEN, WebSocketCloseEventStatusCode.CLOSE_ABNORMAL,
`${WebSocketAction.OPEN} > WS Connection ID '${wsWrapper.guid}' has been closed during initialization in ${Utils.computeTimeDurationSecs(timeStart)} secs ('${wsWrapper.url}')`);
}
-
-
}
private async checkAndCloseIdenticalOpenedWSConnection(wsWrapper: WSWrapper, wsConnection: WSConnection): Promise<void> {
@@ -387,39 +352,13 @@ export default class JsonOCPPServer extends OCPPServer {
private async onMessage(ws: uWS.WebSocket, message: string, isBinary: boolean): Promise<void> {
const wsWrapper: WSWrapper = ws.wsWrapper;
-
-
try {
// Convert
const [ocppMessageType] = JSON.parse(message);
// Lock incoming WS messages
- this.freedWSRequestsMessages[wsWrapper.url] = false;
-
await this.aquireLockForWSRequest(WebSocketAction.MESSAGE, ServerAction.WS_SERVER_MESSAGE, wsWrapper, ocppMessageType);
try {
this.runningWSMessages++;
-
-
- if (!wsWrapper.wsConnection) {
-
-
- if (this.runningWSRequestsMessages[wsWrapper.url]) {
-
-
- if (this.freedWSRequestsMessages[wsWrapper.url]) {
- const openTime = this.onOpenTime[wsWrapper.url];
- const toto = true;
- } else {
- const toto3 = true;
- }
-
- } else {
- const toto2 = true;
- }
-
- }
-
-
// Check if connection is available in Map
await this.checkWSConnectionFromOnMessage(wsWrapper);
// OCPP Request?
@@ -470,11 +409,10 @@ export default class JsonOCPPServer extends OCPPServer {
chargingStationID: wsWrapper.chargingStationID,
action: ServerAction.WS_SERVER_MESSAGE,
module: MODULE_NAME, method: 'checkWSConnectionFromOnMessage',
- message: `${WebSocketAction.MESSAGE} > WS Connection ID '${wsWrapper.guid}' has sent a WS Message on an unreferenced WS Connection, it will be then added in the WS cache, closed: '${wsWrapper.closed.toString()}'` ,
+ message: `${WebSocketAction.MESSAGE} > WS Connection ID '${wsWrapper.guid}' has sent a WS Message on an unreferenced WS Connection, it will be then added in the WS cache`,
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
// Add WS connection from OnMessage in cache
-
await this.setWSConnection(WebSocketAction.MESSAGE, ServerAction.WS_SERVER_MESSAGE, wsConnection, wsWrapper);
return;
}
@@ -549,7 +487,6 @@ export default class JsonOCPPServer extends OCPPServer {
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
this.waitingWSMessages++;
- this.freedWSRequestsMessages[wsWrapper.url] = false;
// eslint-disable-next-line no-constant-condition
while (true) {
// Wait
@@ -565,7 +502,6 @@ export default class JsonOCPPServer extends OCPPServer {
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
// Free the lock
- this.freedWSRequestsMessages[wsWrapper.url] = true;
this.waitingWSMessages--;
break;
}
@@ -580,7 +516,6 @@ export default class JsonOCPPServer extends OCPPServer {
detailedMessages: { wsWrapper: this.getWSWrapperData(wsWrapper) }
});
// Free the lock
- this.freedWSRequestsMessages[wsWrapper.url] = true;
this.waitingWSMessages--;
break;
}

MODIFY
src/server/ocpp/json/web-socket/WSWrapper.ts
src/server/ocpp/json/web-socket/WSWrapper.ts
@@ -24,8 +24,7 @@ export default class WSWrapper {
public nbrPingFailed: number;
public lastPingDate: Date;
public lastPongDate: Date;
- public finishedOnOpen = true;
- public insideOnOpen = false;
+
private ws: WebSocket;
public constructor(ws: WebSocket) {

MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
@@ -26,6 +26,7 @@ import User from '../types/User';
import UserToken from '../types/UserToken';
import Utils from './Utils';
import chalk from 'chalk';
+import crypto from 'crypto';
import { duration } from 'moment';
import sizeof from 'object-sizeof';
@@ -102,24 +103,30 @@ export default class Logging {
}
if (global.monitoringServer) {
- const gaugeDurationMetricName = 'mongodb' + '_' + tenant.subdomain + '_DurationMs_' + `${module}_${method}`;
+
+ const gaugeDurationMetricName = 'mongodb' + '_Duration_' + crypto.randomBytes(8).toString('hex');
let gaugeDurationMetric:client.Gauge = global.monitoringServer.getGauge(gaugeDurationMetricName);
if (!gaugeDurationMetric) {
- gaugeDurationMetric = global.monitoringServer.createGaugeMetric(gaugeDurationMetricName, 'Db perf gauge duration ms');
+
+ gaugeDurationMetric = this.createMetric(gaugeDurationMetricName);
+ gaugeDurationMetric.labels({ tenant: tenant.subdomain, module: module, method: method }).set(executionDurationMillis);
}
- gaugeDurationMetric.set(executionDurationMillis);
- const gaugeRequestSizeMetricName = 'mongodb' + '_' + tenant.subdomain + '_RequestSize_' + `${module}_${method}`;
+
+ const gaugeRequestSizeMetricName = 'mongodb' + '_RequestSize_' + crypto.randomBytes(8).toString('hex');
let gaugeRequestSizeMetric :client.Gauge = global.monitoringServer.getGauge(gaugeRequestSizeMetricName);
if (!gaugeRequestSizeMetric) {
- gaugeRequestSizeMetric = global.monitoringServer.createGaugeMetric(gaugeRequestSizeMetricName, 'Db perf gauge request size');
+ gaugeRequestSizeMetric = this.createMetric(gaugeRequestSizeMetricName);
+ gaugeRequestSizeMetric.labels({ tenant: tenant.subdomain, module: module, method: method }).set(sizeOfRequestDataKB);
}
- gaugeRequestSizeMetric.set(sizeOfRequestDataKB);
- const gaugeResponseSizeMetricName = 'mongodb' + '_' + tenant.subdomain + '_ResponseSize_' + `${module}_${method}`;
+
+ const gaugeResponseSizeMetricName = 'mongodb' + '_ResponseSize_' + crypto.randomBytes(8).toString('hex');
let gaugeResponseSizeMetric :client.Gauge = global.monitoringServer.getGauge(gaugeResponseSizeMetricName);
if (!gaugeResponseSizeMetric) {
- gaugeResponseSizeMetric = global.monitoringServer.createGaugeMetric(gaugeResponseSizeMetricName, 'Db perf gauge response size');
+ gaugeResponseSizeMetric = this.createMetric(gaugeResponseSizeMetricName);
}
- gaugeResponseSizeMetric.set(sizeOfResponseDataKB);
+
+ gaugeResponseSizeMetric.labels({ tenant: tenant.subdomain, module: module, method: method }).set(sizeOfResponseDataKB);
+
}
await PerformanceStorage.savePerformanceRecord(
Utils.buildPerformanceRecord({
@@ -135,6 +142,7 @@ export default class Logging {
}
}
+
public static traceNotificationStart(): number {
if (Logging.getTraceConfiguration().traceNotification) {
return Date.now();
@@ -1084,4 +1092,8 @@ export default class Logging {
}
};
}
+
+ private static createMetric(metricName: string) : Gauge {
+ return global.monitoringServer.createGaugeMetric(metricName, 'Db perf gauge duration ms', ['tenant','module', 'method' ]);
+ }
}

Added labels(2)