Add PostgreSQL connection pool pressure metrics (#23251)

## Summary
- Add pool gauges for total, idle, waiting, and maximum connections
- Record PostgreSQL connection acquisition duration and failures
- Instrument core, workspace primary, and optional replica data sources
- Add unit tests covering gauges, acquisition timing, failures, and
deduplication


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/23251?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
This commit is contained in:
Weiko
2026-07-24 13:03:12 +02:00
committed by GitHub
parent d0863dd1f7
commit e5c9fcf058
6 changed files with 374 additions and 2 deletions
@@ -1,6 +1,7 @@
import { Global, Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { ApplicationEntity } from 'src/engine/core-modules/application/application.entity';
import { TwentyConfigModule } from 'src/engine/core-modules/twenty-config/twenty-config.module';
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
@@ -22,6 +23,7 @@ import { WorkspaceEventEmitterModule } from 'src/engine/workspace-event-emitter/
@Global()
@Module({
imports: [
TypeORMModule,
TypeOrmModule.forFeature([
WorkspaceEntity,
ObjectMetadataEntity,
@@ -8,6 +8,10 @@ import { InjectDataSource } from '@nestjs/typeorm';
import { isDefined } from 'twenty-shared/utils';
import { DataSource } from 'typeorm';
import {
DatabasePoolMetricsService,
DatabasePoolName,
} from 'src/database/typeorm/database-pool-metrics.service';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { GlobalWorkspaceDataSource } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-datasource';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
@@ -25,6 +29,7 @@ export class GlobalWorkspaceDataSourceService
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
@InjectDataSource()
private readonly coreDataSource: DataSource,
private readonly databasePoolMetricsService: DatabasePoolMetricsService,
) {}
async onModuleInit(): Promise<void> {
@@ -57,6 +62,10 @@ export class GlobalWorkspaceDataSourceService
);
await this.globalWorkspaceDataSource.initialize();
this.databasePoolMetricsService.registerDataSource({
poolName: DatabasePoolName.WorkspacePrimary,
dataSource: this.globalWorkspaceDataSource,
});
const shouldInitializeReplicaDataSource = isDefined(
this.twentyConfigService.get('PG_DATABASE_REPLICA_URL'),
@@ -91,6 +100,10 @@ export class GlobalWorkspaceDataSourceService
this.coreDataSource,
);
await this.globalWorkspaceDataSourceReplica.initialize();
this.databasePoolMetricsService.registerDataSource({
poolName: DatabasePoolName.WorkspaceReplica,
dataSource: this.globalWorkspaceDataSourceReplica,
});
}
}