Skip to content

Commit f48321c

Browse files
test: check if database parameter are initialized before emitting metrics
1 parent 8818c11 commit f48321c

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

src/pgClientPrometheusExporter.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,24 @@ export class PgClientPrometheusExporter {
5656
}
5757

5858
onClientError(_error: Error): void {
59-
this.clientErrors.inc(
60-
mergeLabelsWithStandardLabels(
61-
{ host: this.client.host + ':' + this.client.port.toString(), database: this.client.database! },
62-
this.options.defaultLabels
59+
if (this.client.database != null) {
60+
this.clientErrors.inc(
61+
mergeLabelsWithStandardLabels(
62+
{ host: this.client.host + ':' + this.client.port.toString(), database: this.client.database },
63+
this.options.defaultLabels
64+
)
6365
)
64-
)
66+
}
6567
}
6668

6769
onClientEnd(): void {
68-
this.clientDisconnects.inc(
69-
mergeLabelsWithStandardLabels(
70-
{ host: this.client.host + ':' + this.client.port.toString(), database: this.client.database! },
71-
this.options.defaultLabels
70+
if (this.client.database != null) {
71+
this.clientDisconnects.inc(
72+
mergeLabelsWithStandardLabels(
73+
{ host: this.client.host + ':' + this.client.port.toString(), database: this.client.database },
74+
this.options.defaultLabels
75+
)
7276
)
73-
)
77+
}
7478
}
7579
}

src/pgPoolPrometheusExporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ export class PgPoolPrometheusExporter {
7373
labelNames: mergeLabelNamesWithStandardLabels(['host', 'database'], this.options.defaultLabels),
7474
registers: [this.register],
7575
collect: () => {
76-
if (this.poolHost != null && this.poolDatabase != null) {
76+
if (this.poolHost != null && this.poolDatabase != null && this.poolMaxSize != null) {
7777
this.poolSizeMax.set(
7878
mergeLabelsWithStandardLabels(
7979
{ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase },
8080
this.options.defaultLabels
8181
),
82-
this.poolMaxSize!
82+
this.poolMaxSize
8383
)
8484
}
8585
}

src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export function getHost(pool: Pool): string | undefined {
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
5252
* @returns the configured port or 5432 if not set
53-
* @see https://node-postgres.com/api/pool#pool-connection-parameters
5453
*/
5554
export function getPort(pool: Pool): number {
5655
// eslint-disable-next-line @typescript-eslint/no-magic-numbers

0 commit comments

Comments
 (0)