Monitoring

Monitoring the state of the database in my ORM system

Database monitoring in my ORM system can be enabled by adding the monitoring: true parameter when creating an instance of DatabaseManager. All monitoring results will be stored in the monitoring.log file. The system automatically collects and records metrics such as CPU usage, memory usage, disk space, the number of active and pending connections, as well as information about executed SQL queries, including their duration and any potential errors.

Example:

export const databaseManager = new DatabaseManager<DatabasesTypes.POSTGRES>(
    {
        monitoring: true,
})

In addition to automatic metric recording, the ORM system supports manual performance monitoring, which can be triggered using the getMonitoringMetrics method. This method performs monitoring at a specified time interval (e.g., every 15 minutes) and writes the results to a file. Metrics include information about processes such as CPU Usage, Memory Usage, Disk Usage, Active Connections, Waiting Connections, and other parameters. Monitoring also shows the time taken for each query, which can be useful for optimizing the system and speeding up slow queries.

Example content of the monitoring.log file:

CPU Usage: [{"pid":10420,"usename":null,"application_name":"","client_addr":null,"backend_start":"2024-08-08T13:04:49.538Z","state":null,"state_change":null,"query":""},{"pid":10440,"usename":"postgres","application_name":"","client_addr":null,"backend_start":"2024-08-08T13:04:49.542Z","state":null,"state_change":null,"query":""}]
Memory Usage: {"database_size":"8548 kB","total_table_size":"3408 kB","total_index_size":"3104 kB","total_toast_size":"2400 kB"}
Disk Usage: [{"database_name":"first","size":"8548 kB"},{"database_name":"postgres","size":"7828 kB"}]
Active Connections: {"active_connections":"1"}
Waiting Connections: {"waiting_connections":"7"}
Timestamp: 2024-08-09T09:57:01.473Z

Executed in 2.0394000000014785 ms, TYPE: Regular Query, SQL: SELECT * FROM users;, PARAMS: []
Executed in 1.4667999999983294 ms, TYPE: Regular Query, SQL: SELECT * FROM users WHERE is_active = $1;, PARAMS: [true]
Executed in 3.8081999999994878 ms, TYPE: Regular Query, ERROR: error: повторяющееся значение ключа нарушает ограничение уникальности "tasks_pkey", SQL: INSERT INTO tasks (task_id, title, description, is_completed, due_date, price) VALUES ($1, $2, $3, $4, $5, $6);, PARAMS: 5,Новий таск,Опис нового таску,false,2024-12-31,100.5

Database monitoring is an important tool for tracking the performance and status of the system. In my ORM system, all key metrics are automatically stored in the monitoring.log file. This helps to promptly identify issues and optimize the performance of the database.

Last updated