Skip to content

Commit ad825fb

Browse files
fix: only create pool metrics if database and pool metrics are available.
1 parent 9de04dc commit ad825fb

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/pgPoolPrometheusExporter.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class PgPoolPrometheusExporter {
1414
private readonly defaultOptions: PgPoolExporterOptions = {}
1515

1616
private readonly poolMaxSize: number | undefined
17-
private readonly poolHost: string
17+
private readonly poolHost: string | undefined
1818
private readonly poolPort: number
1919
private readonly poolDatabase: string | undefined
2020

@@ -104,13 +104,15 @@ export class PgPoolPrometheusExporter {
104104
labelNames: mergeLabelNamesWithStandardLabels(['host', 'database'], this.options.defaultLabels),
105105
registers: [this.register],
106106
collect: () => {
107-
this.poolWaitingConnections.set(
108-
mergeLabelsWithStandardLabels(
109-
{ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase },
110-
this.options.defaultLabels
111-
),
112-
this.pool.waitingCount
113-
)
107+
if ((this.poolHost != null) && this.poolDatabase != null) {
108+
this.poolWaitingConnections.set(
109+
mergeLabelsWithStandardLabels(
110+
{ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase },
111+
this.options.defaultLabels
112+
),
113+
this.pool.waitingCount
114+
)
115+
}
114116
}
115117
})
116118

@@ -124,13 +126,15 @@ export class PgPoolPrometheusExporter {
124126
labelNames: mergeLabelNamesWithStandardLabels(['host', 'database'], this.options.defaultLabels),
125127
registers: [this.register],
126128
collect: () => {
127-
this.poolIdleConnections.set(
128-
mergeLabelsWithStandardLabels(
129-
{ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase },
130-
this.options.defaultLabels
131-
),
132-
this.pool.idleCount
133-
)
129+
if ((this.poolHost != null) && this.poolDatabase != null) {
130+
this.poolIdleConnections.set(
131+
mergeLabelsWithStandardLabels(
132+
{ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase },
133+
this.options.defaultLabels
134+
),
135+
this.pool.idleCount
136+
)
137+
}
134138
}
135139
})
136140

@@ -181,6 +185,7 @@ export class PgPoolPrometheusExporter {
181185
}
182186

183187
onPoolConnect(client: PoolClient): void {
188+
184189
this.poolConnectionsCreatedTotal.inc(
185190
mergeLabelsWithStandardLabels({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels)
186191
)

src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ export function getMaxPoolSize(pool: Pool): number | undefined {
4242
* @param pool the pool from which to get the property
4343
* @returns the configured host or undefined
4444
*/
45-
export function getHost(pool: Pool): string {
46-
return pool.options.host ?? ''
45+
export function getHost(pool: Pool): string | undefined {
46+
return pool.options.host ?? undefined
4747
}
4848

4949
/**
5050
* Tries to determine the port configuration from the pool via direct property access as the configuration is not exported
5151
* @param pool the pool from which to get the property
52-
* @returns the configured port or undefined
52+
* @returns the configured port or 5432 if not set
53+
* @see https://node-postgres.com/api/pool#pool-connection-parameters
5354
*/
5455
export function getPort(pool: Pool): number {
5556
// eslint-disable-next-line @typescript-eslint/no-magic-numbers

0 commit comments

Comments
 (0)