Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pool-logger-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slonik": patch
---

Add support for the `poolName` configuration property to make it easier to distinguish logs and traces when using multiple pools in the same application.
7 changes: 7 additions & 0 deletions packages/slonik/src/factories/createConnectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const createConnectionPool = ({
maximumConnectionAge,
maximumPoolSize,
minimumPoolSize,
poolName,
}: {
driver: Driver;
events: DatabasePoolEventEmitter;
Expand All @@ -102,6 +103,7 @@ export const createConnectionPool = ({
maximumConnectionAge: number;
maximumPoolSize: number;
minimumPoolSize: number;
poolName?: string;
}): ConnectionPool => {
// See test "waits for all connections to be established before attempting to terminate the pool"
// for explanation of why `pendingConnections` is needed.
Expand Down Expand Up @@ -478,6 +480,9 @@ export const createConnectionPool = ({
return tracer.startActiveSpan("slonik.connection.acquire", async (span) => {
try {
span.setAttribute("slonik.pool.id", id);
if (poolName) {
span.setAttribute("slonik.pool.name", poolName);
}
span.setAttribute("slonik.pool.connections.total", connections.size);
span.setAttribute("slonik.pool.connections.pending", pendingConnections.size);
span.setAttribute("slonik.pool.waitingClients", waitingClients.length);
Expand Down Expand Up @@ -623,6 +628,8 @@ export const createConnectionPool = ({
maximumPoolSize,
minimumPoolSize,
pendingConnections: pendingConnections.size,
poolId: id,
poolName,
waitingClients: waitingClients.length,
},
`connection pool full; client has been queued`,
Expand Down
1 change: 1 addition & 0 deletions packages/slonik/src/factories/createPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const createPool = async (
events,
Logger.child({
poolId: pool.id(),
poolName: clientConfiguration.poolName,
}),
pool,
clientConfiguration,
Expand Down
5 changes: 5 additions & 0 deletions packages/slonik/src/factories/createPoolConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type PoolConfiguration = {
maximumConnectionAge: number;
maximumPoolSize: number;
minimumPoolSize: number;
poolName?: string;
};

/**
Expand Down Expand Up @@ -66,5 +67,9 @@ export const createPoolConfiguration = (
poolConfiguration.minimumPoolSize = clientConfiguration.minimumPoolSize;
}

if (clientConfiguration.poolName) {
poolConfiguration.poolName = clientConfiguration.poolName;
}

return poolConfiguration;
};
5 changes: 5 additions & 0 deletions packages/slonik/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export type ClientConfiguration = {
* Ensure that at least this many connections are available in the pool. (Default: 0)
*/
readonly minimumPoolSize?: number;
/**
* Human-readable identifier for distinguishing multiple pools within the same application,
* e.g. "read", "write", "replica-us-east-1".
*/
readonly poolName?: string;
/**
* Number of times a query failing with Transaction Rollback class error, that doesn't belong to a transaction, is retried. (Default: 5)
*/
Expand Down
Loading