- Added `dotenv` and `prom-client` dependencies for environment variable management and metrics collection. - Updated `package.json` to include new dependencies and types for `dotenv`. - Enhanced `README.md` with bilingual features, usage instructions, and project structure. - Improved `app.config.ts` to validate environment variables using Zod. - Implemented graceful shutdown in `main.ts` for better service reliability. - Added metrics middleware and updated routes to include metrics endpoint. - Enhanced error handling middleware with detailed logging and responses. - Updated feature and health controllers with bilingual comments and improved structure.
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import { register } from 'prom-client';
|
|
import { logger } from '@goodgo/logger';
|
|
|
|
/**
|
|
* EN: Controller for handling metrics requests
|
|
* VI: Controller xử lý các request liên quan đến metrics
|
|
*/
|
|
export class MetricsController {
|
|
/**
|
|
* EN: Get current metrics in Prometheus format
|
|
* VI: Lấy metrics hiện tại theo định dạng Prometheus
|
|
*
|
|
* @param _req - Express request (unused/chưa dùng)
|
|
* @param res - Express response
|
|
*/
|
|
public async getMetrics(_req: Request, res: Response): Promise<void> {
|
|
|
|
try {
|
|
// EN: Set content type for Prometheus
|
|
// VI: Thiết lập content type cho Prometheus
|
|
res.set('Content-Type', register.contentType);
|
|
|
|
// EN: Return metrics
|
|
// VI: Trả về metrics
|
|
res.end(await register.metrics());
|
|
} catch (error) {
|
|
// EN: Log error and return 500
|
|
// VI: Ghi log lỗi và trả về 500
|
|
logger.error('Error getting metrics', { error });
|
|
res.status(500).send('Error getting metrics');
|
|
}
|
|
}
|
|
}
|
|
|