Added monitoring counter for ocpp
MODIFY
.gitignore
.gitignore
@@ -3,6 +3,7 @@ config
users.json
users-imported.json
src/assets/config.json
+src/assets/config_*.json
manifest.yml
test/results
test/results-eslint
@@ -89,3 +90,4 @@ Thumbs.db
# MTA
*.mta
mta_archives/
+

MODIFY
package.json
package.json
"start": "npm version && npm run start:prod",
"start:dev": "npm version && cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" NODE_ENV=development-build node -r source-map-support/register dist/start.js",
"start:dev:debug": "npm version && cross-env NODE_ENV=development ts-node-dev --inspect --files --max-old-space-size=4096 -- src/start.ts",
- "start:dev:debug:nodemon": "npm version && nodemon --exec 'ts-node --files' src/start.ts 9229",
- "start:dev:nodemon": "npm version && nodemon --exec 'ts-node --files' src/start.ts",
+ "start:dev:debug:ocppj": "npm version && cross-env SERVER_ROLE=ocppj NODE_ENV=development ts-node-dev --inspect --files --max-old-space-size=4096 -- src/start.ts",
+ "start:dev:debug:rest": "npm version && cross-env SERVER_ROLE=rest NODE_ENV=development ts-node-dev --inspect --files --max-old-space-size=4096 -- src/start.ts",
+ "start:dev:debug:nodemon": "npm version && nodemon --exec \"ts-node --files\" src/start.ts 9229",
+ "start:dev:nodemon": "npm version && nodemon --exec \"ts-node --files\" src/start.ts",
"start:dev:prof": "npm version && cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" NODE_ENV=development node -r source-map-support/register --prof -- dist/start.js",
"start:dev:doctorprof": "npm version && cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" NODE_ENV=development clinic doctor -- node -r source-map-support/register dist/start.js",
"start:dev:flameprof": "npm version && cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" NODE_ENV=development clinic flame -- node -r source-map-support/register dist/start.js",

MODIFY
src/monitoring/prometheus/PrometheusMonitoringServer.ts
src/monitoring/prometheus/PrometheusMonitoringServer.ts
@@ -16,64 +16,27 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
private expressApplication: Application;
private mapGauge = new Map<string, Gauge>();
private mapCounter = new Map<string, Counter>();
+ private clientRegistry = new client.Registry();
public constructor(monitoringConfig: MonitoringConfiguration) {
super();
- const webSocketGaugeRest : client.Gauge = new client.Gauge({
- name: Constants.WEB_SOCKET_REST_OPEN_COUNT,
- help: 'The number of rest web sockets that are open'
- });
-
- const webSocketGaugeOcpp : client.Gauge = new client.Gauge({
- name: Constants.WEB_SOCKET_OCPP16_OPEN_COUNT,
- help: 'The number of ocpp web sockets that are open'
- });
// Keep params
this.monitoringConfig = monitoringConfig;
- // Create a Registry which registers the metrics
- const register = new client.Registry();
+ client.collectDefaultMetrics({ register: this.clientRegistry });
// Add a default label which is added to all metrics
- register.setDefaultLabels({
+ this.clientRegistry.setDefaultLabels({
app: 'e-Mobility'
});
- client.collectDefaultMetrics({
- register ,
- });
- const mongoDbConnectionCreated : client.Counter = new client.Counter({
- name: Constants.MONGODB_CONNECTION_CREATED,
- help: 'mongo db connection created'
- });
- register.registerMetric(mongoDbConnectionCreated);
- this.mapCounter.set(Constants.MONGODB_CONNECTION_CREATED, mongoDbConnectionCreated);
- const mongoDbConnectionPoolCreated : client.Counter = new client.Counter({
- name: Constants.MONGODB_CONNECTION_POOL_CREATED,
- help: 'mongo db connection pool created'
- });
- register.registerMetric(mongoDbConnectionPoolCreated);
- this.mapCounter.set(Constants.MONGODB_CONNECTION_POOL_CREATED, mongoDbConnectionPoolCreated);
- const mongoDbConnectionClosed : client.Counter = new client.Counter({
- name: Constants.MONGODB_CONNECTION_CLOSED,
- help: 'mongo db connection closed'
- });
- register.registerMetric(mongoDbConnectionClosed);
- this.mapCounter.set(Constants.MONGODB_CONNECTION_CLOSED, mongoDbConnectionClosed);
- const mongoDbConnectionPoolClosed : client.Counter = new client.Counter({
- name: Constants.MONGODB_CONNECTION_POOL_CLOSED,
- help: 'mongo db connection pool closed'
- });
- this.mapCounter.set(Constants.MONGODB_CONNECTION_POOL_CLOSED, mongoDbConnectionPoolClosed);
- register.registerMetric(mongoDbConnectionPoolClosed);
- const mongoDbConnectionReady : client.Gauge = new client.Gauge({
- name: Constants.MONGODB_CONNECTION_READY,
- help: 'mongo db connection ready'
- });
- this.mapGauge.set(Constants.MONGODB_CONNECTION_READY, mongoDbConnectionReady);
- register.registerMetric(mongoDbConnectionReady);
- // Enable the collection of default metrics
- register.registerMetric(webSocketGaugeOcpp);
- register.registerMetric(webSocketGaugeRest);
- this.mapGauge.set(Constants.WEB_SOCKET_OCPP16_OPEN_COUNT, webSocketGaugeOcpp);
- this.mapGauge.set(Constants.WEB_SOCKET_REST_OPEN_COUNT, webSocketGaugeRest);
+ this.createGaugeMetric(Constants.WEB_SOCKET_OCPP_CONNECTIONS_COUNT, 'The number of ocpp web sockets');
+ this.createGaugeMetric(Constants.WEB_SOCKET_REST_CONNECTIONS_COUNT, 'The number of rest web sockets');
+ this.createGaugeMetric(Constants.WEB_SOCKET_QUEUED_REQUEST, 'The number of web sockets that are queued');
+ this.createGaugeMetric(Constants.WEB_SOCKET_RUNNING_REQUEST, 'The number of web sockets that are running');
+ this.createGaugeMetric(Constants.WEB_SOCKET_RUNNING_REQUEST_RESPONSE, 'The number of web sockets request + response that are running');
+ this.createGaugeMetric(Constants.MONGODB_CONNECTION_READY, 'The number of connection that are ready');
+ this.createGaugeMetric(Constants.MONGODB_CONNECTION_CREATED, 'The number of connection created');
+ this.createGaugeMetric(Constants.MONGODB_CONNECTION_CLOSED, 'The number of connection closed');
+
+
// Create HTTP Server
this.expressApplication = ExpressUtils.initApplication();
// Handle requests
@@ -83,8 +46,8 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
// Trace Request
await Logging.traceExpressRequest(req, res, next, ServerAction.MONITORING);
// Process
- res.setHeader('Content-Type', register.contentType);
- res.end(await register.metrics());
+ res.setHeader('Content-Type', this.clientRegistry.contentType);
+ res.end(await this.clientRegistry.metrics());
next();
// Trace Response
Logging.traceExpressResponse(req, res, next, ServerAction.MONITORING);
@@ -98,6 +61,7 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
return this.mapGauge.get(name) ;
}
+
public getCounter(name: string): client.Counter | undefined {
return this.mapCounter.get(name) ;
}
@@ -107,5 +71,15 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
ServerUtils.startHttpServer(this.monitoringConfig,
ServerUtils.createHttpServer(this.monitoringConfig, this.expressApplication), MODULE_NAME, ServerType.MONITORING_SERVER);
}
+
+ private createGaugeMetric(metricname : string, metrichelp : string) : void {
+ const gaugeMetric : client.Gauge = new client.Gauge({
+ name: metricname,
+ help: metrichelp
+ });
+ this.mapGauge.set(metricname, gaugeMetric);
+ this.clientRegistry.registerMetric(gaugeMetric);
+
+ }
}

MODIFY
src/server/ocpp/json/JsonOCPPServer.ts
src/server/ocpp/json/JsonOCPPServer.ts
-import { Counter } from 'prom-client';
import * as uWS from 'uWebSockets.js';
import { App, HttpRequest, HttpResponse, WebSocket, us_socket_context_t } from 'uWebSockets.js';
import { ServerAction, ServerType, WSServerProtocol } from '../../../types/Server';
import { WebSocketAction, WebSocketCloseEventStatusCode, WebSocketPingResult } from '../../../types/WebSocket';
+
import CentralSystemConfiguration from '../../../types/configuration/CentralSystemConfiguration';
import ChargingStation from '../../../types/ChargingStation';
import ChargingStationClient from '../../../client/ocpp/ChargingStationClient';
@@ -66,12 +66,6 @@ export default class JsonOCPPServer extends OCPPServer {
// Convert right away
const reason = Utils.convertBufferArrayToString(message);
const wsWrapper = ws.wsWrapper as WSWrapper;
- if (wsWrapper.url.startsWith('/OCPP16')) {
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_OCPP16_OPEN_COUNT).dec(1);
- }
- if (wsWrapper.url.startsWith('/REST')) {
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_REST_OPEN_COUNT).dec(1);
- }
// Close
wsWrapper.closed = true;
await this.logWSConnectionClosed(wsWrapper, ServerAction.WS_SERVER_CONNECTION_CLOSE, code,
@@ -237,13 +231,11 @@ export default class JsonOCPPServer extends OCPPServer {
if (wsWrapper.url.startsWith('/OCPP16')) {
// Create and Initialize WS Connection
await this.checkAndStoreWSOpenedConnection(WSServerProtocol.OCPP16, wsWrapper);
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_OCPP16_OPEN_COUNT).inc(1);
}
// Check REST connection
if (wsWrapper.url.startsWith('/REST')) {
// Create and Initialize WS Connection
await this.checkAndStoreWSOpenedConnection(WSServerProtocol.REST, wsWrapper);
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_REST_OPEN_COUNT).inc(1);
}
} catch (error) {
await Logging.logException(error, ServerAction.WS_SERVER_CONNECTION_OPEN, MODULE_NAME, 'onOpen', Constants.DEFAULT_TENANT_ID);
@@ -253,12 +245,6 @@ export default class JsonOCPPServer extends OCPPServer {
// Close WS
await this.closeWebSocket(WebSocketAction.OPEN, ServerAction.WS_SERVER_CONNECTION_OPEN, wsWrapper, WebSocketCloseEventStatusCode.CLOSE_ABNORMAL,
`${WebSocketAction.OPEN} > WS Connection ID '${wsWrapper.guid}' has been rejected and closed by server due to an exception: ${error.message as string}`);
- if (wsWrapper.url.startsWith('/OCPP16')) {
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_OCPP16_OPEN_COUNT).dec(1);
- }
- if (wsWrapper.url.startsWith('/REST')) {
- global.monitoringServer.getGauge(Constants.WEB_SOCKET_REST_OPEN_COUNT).dec(1);
- }
} finally {
this.runningWSMessages--;
this.releaseLockForWSMessageRequest(wsWrapper);
@@ -708,6 +694,12 @@ export default class JsonOCPPServer extends OCPPServer {
`${sizeOfCurrentRequestsBytes / 1000} kB used in JSON WS cache`
]
});
+ global.monitoringServer.getGauge(Constants.WEB_SOCKET_RUNNING_REQUEST_RESPONSE).set(this.runningWSMessages);
+ global.monitoringServer.getGauge(Constants.WEB_SOCKET_OCPP_CONNECTIONS_COUNT).set(this.jsonWSConnections.size);
+ global.monitoringServer.getGauge(Constants.WEB_SOCKET_REST_CONNECTIONS_COUNT).set(this.jsonRestWSConnections.size);
+ global.monitoringServer.getGauge(Constants.WEB_SOCKET_CURRRENT_REQUEST).set(numberOfCurrentRequests);
+ global.monitoringServer.getGauge(Constants.WEB_SOCKET_RUNNING_REQUEST).set(Object.keys(this.runningWSRequestsMessages).length);
+ global.monitoringServer.getGauge(Constants.WEB_SOCKET_QUEUED_REQUEST).set(this.waitingWSMessages);
if (this.isDebug()) {
Logging.logConsoleDebug('=====================================');
Logging.logConsoleDebug(`** ${this.jsonWSConnections.size} JSON Connection(s)`);

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -280,14 +280,12 @@ export default class MongoDBStorage {
const connections = new Set<any>();
// Get the EVSE DB
this.mongoDBClient = mongoDBClient;
- mongoDBClient.on('connectionPoolCreated',
- (event) => global.monitoringServer.getCounter(Constants.MONGODB_CONNECTION_POOL_CREATED).inc(1)
- );
+
mongoDBClient.on('connectionCreated',
- (event) => global.monitoringServer.getCounter(Constants.MONGODB_CONNECTION_CREATED).inc(1)
+ (event) => global.monitoringServer.getGauge(Constants.MONGODB_CONNECTION_CREATED).inc(1)
);
mongoDBClient.on('connectionClosed',
- (event) => global.monitoringServer.getCounter(Constants.MONGODB_CONNECTION_CLOSED).inc(1)
+ (event) => global.monitoringServer.getGauge(Constants.MONGODB_CONNECTION_CLOSED).inc(1)
);
mongoDBClient.on('connectionReady',
(event) => {

MODIFY
src/utils/Configuration.ts
src/utils/Configuration.ts
@@ -30,6 +30,7 @@ import WSDLEndpointConfiguration from '../types/configuration/WSDLEndpointConfig
import chalk from 'chalk';
import fs from 'fs';
import global from './../types/GlobalType';
+import Utils from './Utils';
const MODULE_NAME = 'Configuration';
@@ -288,6 +289,10 @@ export default class Configuration {
private static getConfig(): ConfigurationData {
if (!Configuration.config) {
let configuration: ConfigurationData;
+ if (process.env.SERVER_ROLE) {
+ configuration = JSON.parse(
+ fs.readFileSync(`${global.appRoot}/assets/config_` + process.env.SERVER_ROLE + '.json', 'utf8')) as ConfigurationData;
+ } else {
// K8s
if (fs.existsSync('/config/config.json')) {
configuration = JSON.parse(
@@ -297,7 +302,7 @@ export default class Configuration {
configuration = JSON.parse(
fs.readFileSync(`${global.appRoot}/assets/config.json`, 'utf8')) as ConfigurationData;
}
- // Validate
+ }
Configuration.config = ConfigurationValidatorStorage.getInstance().validateConfigurationSave(configuration);
}
return Configuration.config;

MODIFY
src/utils/Constants.ts
src/utils/Constants.ts
import { OCPPAttribute, OCPPLocation, OCPPMeasurand, OCPPPhase, OCPPReadingContext, OCPPUnitOfMeasure, OCPPValueFormat } from '../types/ocpp/OCPPServer';
import DbParams from '../types/database/DbParams';
+import Logging from './Logging';
import { OcppParameter } from '../types/ChargingStation';
import Tenant from '../types/Tenant';
@@ -473,9 +474,12 @@ export default class Constants {
public static readonly STRIPE_PARTNER_ID = 'TECH-000685';
public static readonly STRIPE_API_VERSION = '2020-08-27';
- public static readonly WEB_SOCKET_OCPP16_OPEN_COUNT = 'web_socket_ocpp16_open_count';
- public static readonly WEB_SOCKET_REST_OPEN_COUNT = 'web_socket_rest_open_count';
-
+ public static readonly WEB_SOCKET_OCPP_CONNECTIONS_COUNT = 'web_socket_ocpp_connections_count';
+ public static readonly WEB_SOCKET_CURRRENT_REQUEST = 'web_socket_current_request';
+ public static readonly WEB_SOCKET_REST_CONNECTIONS_COUNT = 'web_socket_rest_connections_count';
+ public static readonly WEB_SOCKET_RUNNING_REQUEST = 'web_socket_running_request';
+ public static readonly WEB_SOCKET_RUNNING_REQUEST_RESPONSE = 'web_socket_running_request_response';
+ public static readonly WEB_SOCKET_QUEUED_REQUEST = 'web_socket_queued_request';
public static readonly MONGODB_CONNECTION_POOL_CREATED = 'mongodb_connectionPoolCreated';
public static readonly MONGODB_CONNECTION_POOL_CLOSED = 'mongodb_connectionPoolClosed';

Added monitoring counter for ocpp