Skip to content

Commit

Permalink
Update app.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jagankumar-egov authored Dec 4, 2024
1 parent e19cbda commit eb786c9
Showing 1 changed file with 66 additions and 25 deletions.
91 changes: 66 additions & 25 deletions health-services/project-factory/src/server/app.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import express from 'express';
import * as bodyParser from 'body-parser';
import config from './config';
import { requestMiddleware } from './utils/middlewares';
import { errorLogger, errorResponder, invalidPathHandler } from './utils/genericUtils';
import { tracingMiddleware } from './tracing';
import { createProxyMiddleware } from 'http-proxy-middleware';
import * as v8 from 'v8';
import express from "express";
import * as bodyParser from "body-parser";
import config from "./config";
import { requestMiddleware } from "./utils/middlewares";
import {
errorLogger,
errorResponder,
invalidPathHandler,
} from "./utils/genericUtils";
import { tracingMiddleware } from "./tracing";
import { createProxyMiddleware } from "http-proxy-middleware";
import * as v8 from "v8";
import { logger } from "./utils/logger";

const printMemoryInMB=(memoryInBytes:number)=> {
const printMemoryInMB = (memoryInBytes: number) => {
const memoryInMB = memoryInBytes / (1024 * 1024); // Convert bytes to MB
return `${memoryInMB.toFixed(2)} MB`;
}
};

class App {
public app: express.Application;
Expand All @@ -25,31 +30,41 @@ class App {
this.app.use(invalidPathHandler);

// Global error handling for uncaught exceptions
process.on('uncaughtException', (err) => {
console.error('Unhandled Exception:', err);
process.on("uncaughtException", (err) => {
console.error("Unhandled Exception:", err);
});

// Global error handling for unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
});
}

private initializeMiddlewares() {
this.app.use(bodyParser.json({ limit: config.app.incomingRequestPayloadLimit }));
this.app.use(bodyParser.urlencoded({ limit: config.app.incomingRequestPayloadLimit, extended: true }));
this.app.use(
bodyParser.json({ limit: config.app.incomingRequestPayloadLimit })
);
this.app.use(
bodyParser.urlencoded({
limit: config.app.incomingRequestPayloadLimit,
extended: true,
})
);
this.app.use(bodyParser.json());
this.app.use(tracingMiddleware);
this.app.use(requestMiddleware);
this.app.use(errorLogger);
this.app.use(errorResponder);
this.app.use('/tracing', createProxyMiddleware({
target: 'http://localhost:16686',
changeOrigin: true,
pathRewrite: {
'^/tracing': '/',
},
}));
this.app.use(
"/tracing",
createProxyMiddleware({
target: "http://localhost:16686",
changeOrigin: true,
pathRewrite: {
"^/tracing": "/",
},
})
);
}

private initializeControllers(controllers: any) {
Expand All @@ -60,8 +75,34 @@ class App {

public listen() {
this.app.listen(this.port, () => {
console.log(`App listening on the port ${this.port}`);
console.log("Current App's Heap Configuration Total Available :", printMemoryInMB(v8.getHeapStatistics()?.total_available_size) ," max limit set to : ",printMemoryInMB(v8.getHeapStatistics()?.heap_size_limit) );
logger.info(`App listening on the port ${this.port}`);
// Add periodic monitoring
setInterval(() => {
const stats = v8.getHeapStatistics();
const usedHeapSize = stats.used_heap_size;
const heapLimit = stats.heap_size_limit;

logger.debug(
JSON.stringify({
"Heap Usage": {
used: printMemoryInMB(usedHeapSize),
limit: printMemoryInMB(heapLimit),
percentage: ((usedHeapSize / heapLimit) * 100).toFixed(2),
},
})
);

// Alert if memory usage is above 80%
if (usedHeapSize / heapLimit > 0.8) {
logger.warn("High memory usage detected");
}
}, 5 * 60 * 1000); // Every 5 minutes
logger.info(
"Current App's Heap Configuration Total Available :",
printMemoryInMB(v8.getHeapStatistics()?.total_available_size),
" max limit set to : ",
printMemoryInMB(v8.getHeapStatistics()?.heap_size_limit)
);
});
}
}
Expand Down

0 comments on commit eb786c9

Please sign in to comment.