Trace HTTP requests with Server Actions
MODIFY
src/monitoring/prometheus/PrometheusMonitoringServer.ts
src/monitoring/prometheus/PrometheusMonitoringServer.ts
import { Application, NextFunction, Request, Response } from 'express';
+import { ServerAction, ServerType } from '../../types/Server';
import ExpressUtils from '../../server/ExpressUtils';
import Logging from '../../utils/Logging';
import MonitoringConfiguration from '../../types/configuration/MonitoringConfiguration';
import MonitoringServer from '../MonitoringServer';
-import { ServerType } from '../../types/Server';
import { ServerUtils } from '../../server/ServerUtils';
import client from 'prom-client';
@@ -30,17 +30,18 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
});
// Create HTTP Server
this.expressApplication = ExpressUtils.initApplication();
- // Log Express Request
- this.expressApplication.use(
- async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
- );
// Handle requests
this.expressApplication.use(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
'/metrics', async (req: Request, res: Response, next: NextFunction) => {
+ // Trace Request
+ await Logging.traceExpressRequest(req, res, next, ServerAction.MONITORING);
+ // Process
res.setHeader('Content-Type', register.contentType);
res.end(await register.metrics());
next();
+ // Trace Response
+ Logging.traceExpressResponse(req, res, next, ServerAction.MONITORING);
}
);
// Post init

MODIFY
src/server/ExpressUtils.ts
src/server/ExpressUtils.ts
@@ -57,13 +57,7 @@ export default class ExpressUtils {
return app;
}
- public static postInitApplication(expressApplication: Application, traceHttpRequest = true): void {
- // Log Express Response
- if (traceHttpRequest) {
- expressApplication.use(
- async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressResponse(req, res, next)
- );
- }
+ public static postInitApplication(expressApplication: Application): void {
// Error Handling
expressApplication.use(Logging.traceExpressError.bind(this));
}

MODIFY
src/server/ocpi/OCPIServer.ts
src/server/ocpi/OCPIServer.ts
@@ -48,7 +48,7 @@ export default class OCPIServer {
}
});
// Post init
- ExpressUtils.postInitApplication(this.expressApplication, false);
+ ExpressUtils.postInitApplication(this.expressApplication);
}
// Start the server

MODIFY
src/server/ocpp/soap/SoapOCPPServer.ts
src/server/ocpp/soap/SoapOCPPServer.ts
@@ -28,25 +28,18 @@ export default class SoapOCPPServer extends OCPPServer {
super(centralSystemConfig, chargingStationConfig);
// Initialize express app
this.expressApplication = ExpressUtils.initApplication(null, centralSystemConfig.debug);
- // Log Express Request
- this.expressApplication.use(
- async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
- );
// Initialize the HTTP server
this.httpServer = ServerUtils.createHttpServer(this.centralSystemConfig, this.expressApplication);
}
- /**
- * Start the server and listen to all SOAP OCPP versions
- * Listen to external command to send request to charging stations
- */
public start(): void {
// Make it global for SOAP Services
global.centralSystemSoapServer = this;
ServerUtils.startHttpServer(this.centralSystemConfig, this.httpServer, MODULE_NAME, ServerType.SOAP_SERVER);
// Create Soap Servers
// OCPP 1.5 -----------------------------------------
- const soapServer15 = soap.listen(this.httpServer, `/${Utils.getOCPPServerVersionURLPath(OCPPVersion.VERSION_15)}`, centralSystemService15, this.readWsdl('OCPPCentralSystemService15.wsdl'));
+ const soapServer15 = soap.listen(this.httpServer, `/${Utils.getOCPPServerVersionURLPath(OCPPVersion.VERSION_15)}`,
+ centralSystemService15, this.readWsdl('OCPPCentralSystemService15.wsdl'));
if (this.centralSystemConfig.debug) {
// Listen
soapServer15.log = async (type, data) => {
@@ -58,7 +51,8 @@ export default class SoapOCPPServer extends OCPPServer {
});
}
// OCPP 1.6 -----------------------------------------
- const soapServer16 = soap.listen(this.httpServer, `/${Utils.getOCPPServerVersionURLPath(OCPPVersion.VERSION_16)}`, centralSystemService16, this.readWsdl('OCPPCentralSystemService16.wsdl'));
+ const soapServer16 = soap.listen(this.httpServer, `/${Utils.getOCPPServerVersionURLPath(OCPPVersion.VERSION_16)}`,
+ centralSystemService16, this.readWsdl('OCPPCentralSystemService16.wsdl'));
if (this.centralSystemConfig.debug) {
// Listen
soapServer16.log = async (type, data) => {

MODIFY
src/server/odata/ODataServer.ts
src/server/odata/ODataServer.ts
@@ -22,10 +22,6 @@ export default class ODataServer {
this.oDataServerConfig = oDataServerConfig;
// Initialize express app
this.expressApplication = ExpressUtils.initApplication(null, oDataServerConfig.debug);
- // Log Express Request
- this.expressApplication.use(
- async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
- );
// Get URL of the CentralSystemRestServer
const restConf = Configuration.getCentralSystemRestServerConfig();
const restServerUrl = `${restConf.protocol}://${restConf.host}:${restConf.port}/`;

MODIFY
src/server/oicp/OICPServer.ts
src/server/oicp/OICPServer.ts
-import { Application, NextFunction, Request, Response } from 'express';
+import { Application, NextFunction, Response } from 'express';
import ExpressUtils from '../ExpressUtils';
-import Logging from '../../utils/Logging';
import OICPServiceConfiguration from '../../types/configuration/OICPServiceConfiguration';
import OICPServices from './OICPServices';
import { ServerType } from '../../types/Server';
@@ -19,10 +18,6 @@ export default class OICPServer {
this.oicpRestConfig = oicpRestConfig;
// Initialize express app
this.expressApplication = ExpressUtils.initApplication(null, oicpRestConfig.debug);
- // Log Express Request
- this.expressApplication.use(
- async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
- );
// New OICP Services Instances
const oicpServices = new OICPServices(this.oicpRestConfig);
// Register all services in express

MODIFY
src/server/rest/RestServer.ts
src/server/rest/RestServer.ts
@@ -39,7 +39,7 @@ export default class RestServer {
RestServerService.restServiceUtil.bind(this)
);
// Post init
- ExpressUtils.postInitApplication(this.expressApplication, false);
+ ExpressUtils.postInitApplication(this.expressApplication);
// Create HTTP server to serve the express app
RestServer.restHttpServer = ServerUtils.createHttpServer(RestServer.centralSystemRestConfig, this.expressApplication);
}

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -220,6 +220,7 @@ export enum ServerAction {
DB_WATCH = 'DBWatch',
DB_MONITOR = 'DBMonitor',
+ MONITORING = 'Monitoring',
EXPRESS_SERVER = 'ExpressServer',
ODATA_SERVER = 'ODataServer',

Trace HTTP requests with Server Actions