Skip to content

Commit 7e2b0ef

Browse files
authored
[typeorm] increase/monitor connection pool (#18457)
1 parent 252db30 commit 7e2b0ef

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

components/gitpod-db/src/typeorm/typeorm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class TypeORM {
4646
namingStrategy: new DefaultNamingStrategy(),
4747
extra: {
4848
// default is 10 (see https://github.com/mysqljs/mysql#pool-options), which is too low for our use case
49-
connectionLimit: 20,
49+
connectionLimit: 40,
5050
},
5151
};
5252
}

components/server/src/init.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import { Container } from "inversify";
5656
import { Server } from "./server";
5757
import { log, LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging";
5858
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing";
59+
import { TypeORM } from "@gitpod/gitpod-db/lib";
60+
import { dbConnectionsFree, dbConnectionsTotal } from "./prometheus-metrics";
5961
if (process.env.NODE_ENV === "development") {
6062
require("longjohn");
6163
}
@@ -76,9 +78,24 @@ export async function start(container: Container) {
7678
}
7779
});
7880

81+
const interval = setInterval(async () => {
82+
try {
83+
const connection = await container.get(TypeORM).getConnection();
84+
const pool: any = (connection.driver as any).pool;
85+
const activeConnections = pool._allConnections.length;
86+
const freeConnections = pool._freeConnections.length;
87+
88+
dbConnectionsTotal.set(activeConnections);
89+
dbConnectionsFree.set(freeConnections);
90+
} catch (error) {
91+
log.error("Error updating TypeORM metrics", error);
92+
}
93+
}, 5000);
94+
7995
process.on("SIGTERM", async () => {
8096
log.info("SIGTERM received, stopping");
8197
await server.stop();
98+
clearInterval(interval);
8299
});
83100

84101
const tracing = container.get(TracingManager);

components/server/src/prometheus-metrics.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ export function registerServerMetrics(registry: prometheusClient.Registry) {
3333
registry.registerMetric(redisUpdatesReceived);
3434
registry.registerMetric(redisUpdatesCompletedTotal);
3535
registry.registerMetric(updateSubscribersRegistered);
36+
registry.registerMetric(dbConnectionsTotal);
37+
registry.registerMetric(dbConnectionsFree);
3638
}
3739

40+
export const dbConnectionsTotal = new prometheusClient.Gauge({
41+
name: "gitpod_typeorm_total_connections",
42+
help: "Total number of connections in TypeORM pool",
43+
});
44+
45+
export const dbConnectionsFree = new prometheusClient.Gauge({
46+
name: "gitpod_typeorm_free_connections",
47+
help: "Number of free connections in TypeORM pool",
48+
});
49+
3850
const loginCompletedTotal = new prometheusClient.Counter({
3951
name: "gitpod_login_completed_total",
4052
help: "Total number of logins completed into gitpod, by status",

0 commit comments

Comments
 (0)