Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance queue #4025

Open
wants to merge 7 commits into
base: master-qa
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
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@
"test:jest": "npm version && cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" TS_NODE_FILES=true jest --config='./test/jest.config.ts' --runInBand --logHeapUsage --verbose --colors --silent --forceExit"
},
"dependencies": {
"@types/better-queue": "^3.8.3",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"ajv-keywords": "^5.1.0",
"axios": "^0.27.2",
"axios-retry": "^3.3.1",
"basic-auth": "^2.0.1",
"bcryptjs": "^2.4.3",
"better-queue": "^3.8.12",
"bluebird": "^3.7.2",
"body-parser": "^1.20.0",
"body-parser-xml": "^2.0.3",
Expand Down
45 changes: 45 additions & 0 deletions src/monitoring/ComposedMonitoringMetric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import client, { Gauge, LabelValues } from 'prom-client';

class ComposedMonitoringMetric {
private gaugeMetricAvg: client.Gauge;
private gaugeMetricCount: client.Gauge;
private metricCount = 0;
private metricAvg = 0;

// Normal signature with defaults
public constructor(prefix: string, metricname: string, suffix: number, metrichelp: string, labelNames: string[]) {
this.gaugeMetricAvg = new client.Gauge({
name: prefix + '_' + metricname + '_avg_' + suffix,
help: metrichelp,
labelNames: labelNames,
});
this.gaugeMetricCount = new client.Gauge({
name: prefix + '_' + metricname + '_count_' + suffix,
help: metrichelp,
labelNames: labelNames,
});
}

public register(registry: client.Registry): void {
registry.registerMetric(this.gaugeMetricAvg);
registry.registerMetric(this.gaugeMetricCount);
}

public setValue(labels: LabelValues<string>, value: number) {
if (this.metricCount === 0) {
this.metricAvg = 0;
}
this.metricAvg = (this.metricAvg * this.metricCount + value) / (this.metricCount + 1);
this.metricCount++;
this.gaugeMetricCount.labels(labels).set(this.metricCount);
this.gaugeMetricAvg.labels(labels).set(this.metricAvg);
}

public clear() {
this.metricAvg = 0;
this.metricCount = 0;
this.gaugeMetricCount.reset();
this.gaugeMetricAvg.reset();
}
}
export { ComposedMonitoringMetric };
18 changes: 18 additions & 0 deletions src/monitoring/MonitoringServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import client from 'prom-client';
import { ComposedMonitoringMetric } from './ComposedMonitoringMetric';

export default abstract class MonitoringServer {
public abstract start(): void;

public abstract getGauge(name: string): client.Gauge | undefined;

public abstract createGaugeMetric(
metricname: string,
metrichelp: string,
labelNames?: string[]
): client.Gauge;

public abstract getComposedMetric(
prefix: string,
metricname: string,
suffix: number,
metrichelp: string,
labelNames: string[]
): ComposedMonitoringMetric;
}
69 changes: 59 additions & 10 deletions src/monitoring/prometheus/PrometheusMonitoringServer.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
import { Application, NextFunction, Request, Response } from 'express';
import { ServerAction, ServerType } from '../../types/Server';
import client, { Gauge } from 'prom-client';

import Constants from '../../utils/Constants';
import ExpressUtils from '../../server/ExpressUtils';
import Logging from '../../utils/Logging';
import MonitoringConfiguration from '../../types/configuration/MonitoringConfiguration';
import { ComposedMonitoringMetric } from '../ComposedMonitoringMetric';
import MonitoringServer from '../MonitoringServer';
import { ServerUtils } from '../../server/ServerUtils';
import client from 'prom-client';
import global from '../../types/GlobalType';

const MODULE_NAME = 'PrometheusMonitoringServer';

export default class PrometheusMonitoringServer extends MonitoringServer {
private monitoringConfig: MonitoringConfiguration;
private expressApplication: Application;
private mapGauge = new Map<string, Gauge>();
private mapComposedMetric = new Map<string, ComposedMonitoringMetric>();
private clientRegistry = new client.Registry();

public constructor(monitoringConfig: MonitoringConfiguration) {
super();
// 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'
});
// Enable the collection of default metrics
client.collectDefaultMetrics({
register,
});
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.WEB_SOCKET_CURRRENT_REQUEST, 'JSON WS Requests in cache');
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
Expand All @@ -37,8 +47,11 @@ 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());
for (const val of this.mapComposedMetric.values()) {
val.clear();
}
next();
// Trace Response
Logging.traceExpressResponse(req, res, next, ServerAction.MONITORING);
Expand All @@ -48,8 +61,44 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
ExpressUtils.postInitApplication(this.expressApplication);
}

public getGauge(name: string): client.Gauge | undefined {
return this.mapGauge.get(name) ;
}

public start(): void {
global.monitoringServer = this;
ServerUtils.startHttpServer(this.monitoringConfig,
ServerUtils.createHttpServer(this.monitoringConfig, this.expressApplication), MODULE_NAME, ServerType.MONITORING_SERVER);
}

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,
labelNames: labelNames
});
} else {
gaugeMetric = new client.Gauge({
name: metricname,
help: metrichelp
});
}
this.mapGauge.set(metricname, gaugeMetric);
this.clientRegistry.registerMetric(gaugeMetric);
return gaugeMetric;
}

public getComposedMetric(prefix : string, metricname: string, suffix: number, metrichelp: string, labelNames: string[]) : ComposedMonitoringMetric {
const key = prefix + '_' + metricname + '_' + suffix;
let composedMetric : ComposedMonitoringMetric = this.mapComposedMetric.get(key);
if (composedMetric) {
return composedMetric;
}
composedMetric = new ComposedMonitoringMetric(prefix, metricname,suffix,metrichelp,labelNames);
composedMetric.register(this.clientRegistry);
this.mapComposedMetric.set(key, composedMetric);
return composedMetric;
}
}
Loading