@@ -18,6 +18,15 @@ export class PgPoolPrometheusExporter {
1818 private readonly poolPort : number
1919 private readonly poolDatabase : string | undefined
2020
21+ private readonly PG_POOL_CONNECTIONS_CREATED_TOTAL = 'pg_pool_connections_created_total'
22+ private readonly PG_POOL_SIZE = 'pg_pool_size'
23+ private readonly PG_POOL_MAX = 'pg_pool_max'
24+ private readonly PG_POOL_ACTIVE_CONNECTIONS = 'pg_pool_active_connections'
25+ private readonly PG_POOL_WAITING_CONNECTIONS = 'pg_pool_waiting_connections'
26+ private readonly PG_POOL_IDLE_CONNECTIONS = 'pg_pool_idle_connections'
27+ private readonly PG_POOL_ERRORS_TOTAL = 'pg_pool_errors_total'
28+ private readonly PG_POOL_CONNECTIONS_REMOVED_TOTAL = 'pg_pool_connections_removed_total'
29+
2130 private readonly poolConnectionsCreatedTotal : Counter
2231 private readonly poolSize : Gauge
2332 private readonly poolSizeMax : Gauge
@@ -32,88 +41,96 @@ export class PgPoolPrometheusExporter {
3241 this . register = register
3342 this . options = { ...this . defaultOptions , ...options }
3443
35- this . poolConnectionsCreatedTotal = new Counter ( {
36- name : 'pg_pool_connections_created_total' ,
37- help : 'The total number of created connections.' ,
38- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
39- registers : [ this . register ]
40- } )
41-
42- this . poolSize = new Gauge ( {
43- name : 'pg_pool_size' ,
44- help : 'The current size of the connection pool, including active and idle members.' ,
45- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
46- registers : [ this . register ]
47- } )
48-
49- this . poolSizeMax = new Gauge ( {
50- name : 'pg_pool_max' ,
51- help : 'The maximum size of the connection pool.' ,
52- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
53- registers : [ this . register ] ,
54- collect : ( ) => {
55- this . poolSizeMax . set (
56- mergeLabelsWithStandardLabels (
57- { host : this . poolHost + ':' + this . poolPort . toString ( ) , database : this . poolDatabase } ,
58- this . options . defaultLabels
59- ) ,
60- this . poolMaxSize !
61- )
62- }
63- } )
64-
65- this . poolActiveConnections = new Gauge ( {
66- name : 'pg_pool_active_connections' ,
67- help : 'The total number of active connections.' ,
68- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
69- registers : [ this . register ]
70- } )
71-
72- this . poolWaitingConnections = new Gauge ( {
73- name : 'pg_pool_waiting_connections' ,
74- help : 'The total number of waiting connections.' ,
75- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
76- registers : [ this . register ] ,
77- collect : ( ) => {
78- this . poolWaitingConnections . set (
79- mergeLabelsWithStandardLabels (
80- { host : this . poolHost + ':' + this . poolPort . toString ( ) , database : this . poolDatabase } ,
81- this . options . defaultLabels
82- ) ,
83- this . pool . waitingCount
84- )
85- }
86- } )
87-
88- this . poolIdleConnections = new Gauge ( {
89- name : 'pg_pool_idle_connections' ,
90- help : 'The total number of idle connections.' ,
91- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
92- registers : [ this . register ] ,
93- collect : ( ) => {
94- this . poolIdleConnections . set (
95- mergeLabelsWithStandardLabels (
96- { host : this . poolHost + ':' + this . poolPort . toString ( ) , database : this . poolDatabase } ,
97- this . options . defaultLabels
98- ) ,
99- this . pool . idleCount
100- )
101- }
102- } )
103-
104- this . poolErrors = new Counter ( {
105- name : 'pg_pool_errors_total' ,
106- help : 'The total number of connection errors with a database.' ,
107- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' , 'error' ] , this . options . defaultLabels ) ,
108- registers : [ this . register ]
109- } )
110-
111- this . poolConnectionsRemovedTotal = new Counter ( {
112- name : 'pg_pool_connections_removed_total' ,
113- help : 'The total number of removed connections.' ,
114- labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
115- registers : [ this . register ]
116- } )
44+ this . poolConnectionsCreatedTotal = ( this . register . getSingleMetric ( this . PG_POOL_CONNECTIONS_CREATED_TOTAL ) ??
45+ new Counter ( {
46+ name : this . PG_POOL_CONNECTIONS_CREATED_TOTAL ,
47+ help : 'The total number of created connections.' ,
48+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
49+ registers : [ this . register ]
50+ } ) ) as Counter
51+
52+ this . poolSize = ( this . register . getSingleMetric ( this . PG_POOL_SIZE ) ??
53+ new Gauge ( {
54+ name : this . PG_POOL_SIZE ,
55+ help : 'The current size of the connection pool, including active and idle members.' ,
56+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
57+ registers : [ this . register ]
58+ } ) ) as Gauge
59+
60+ this . poolSizeMax = ( this . register . getSingleMetric ( this . PG_POOL_MAX ) ??
61+ new Gauge ( {
62+ name : this . PG_POOL_MAX ,
63+ help : 'The maximum size of the connection pool.' ,
64+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
65+ registers : [ this . register ] ,
66+ collect : ( ) => {
67+ this . poolSizeMax . set (
68+ mergeLabelsWithStandardLabels (
69+ { host : this . poolHost + ':' + this . poolPort . toString ( ) , database : this . poolDatabase } ,
70+ this . options . defaultLabels
71+ ) ,
72+ this . poolMaxSize !
73+ )
74+ }
75+ } ) ) as Gauge
76+
77+ this . poolActiveConnections = ( this . register . getSingleMetric ( this . PG_POOL_ACTIVE_CONNECTIONS ) ??
78+ new Gauge ( {
79+ name : this . PG_POOL_ACTIVE_CONNECTIONS ,
80+ help : 'The total number of active connections.' ,
81+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
82+ registers : [ this . register ]
83+ } ) ) as Gauge
84+
85+ this . poolWaitingConnections = ( this . register . getSingleMetric ( this . PG_POOL_WAITING_CONNECTIONS ) ??
86+ new Gauge ( {
87+ name : this . PG_POOL_WAITING_CONNECTIONS ,
88+ help : 'The total number of waiting connections.' ,
89+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
90+ registers : [ this . register ] ,
91+ collect : ( ) => {
92+ this . poolWaitingConnections . set (
93+ mergeLabelsWithStandardLabels (
94+ { host : this . poolHost + ':' + this . poolPort . toString ( ) , database : this . poolDatabase } ,
95+ this . options . defaultLabels
96+ ) ,
97+ this . pool . waitingCount
98+ )
99+ }
100+ } ) ) as Gauge
101+
102+ this . poolIdleConnections = ( this . register . getSingleMetric ( this . PG_POOL_IDLE_CONNECTIONS ) ??
103+ new Gauge ( {
104+ name : this . PG_POOL_IDLE_CONNECTIONS ,
105+ help : 'The total number of idle connections.' ,
106+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
107+ registers : [ this . register ] ,
108+ collect : ( ) => {
109+ this . poolIdleConnections . set (
110+ mergeLabelsWithStandardLabels (
111+ { host : this . poolHost + ':' + this . poolPort . toString ( ) , database : this . poolDatabase } ,
112+ this . options . defaultLabels
113+ ) ,
114+ this . pool . idleCount
115+ )
116+ }
117+ } ) ) as Gauge
118+
119+ this . poolErrors = ( this . register . getSingleMetric ( this . PG_POOL_ERRORS_TOTAL ) ??
120+ new Counter ( {
121+ name : this . PG_POOL_ERRORS_TOTAL ,
122+ help : 'The total number of connection errors with a database.' ,
123+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' , 'error' ] , this . options . defaultLabels ) ,
124+ registers : [ this . register ]
125+ } ) ) as Counter
126+
127+ this . poolConnectionsRemovedTotal = ( this . register . getSingleMetric ( this . PG_POOL_CONNECTIONS_REMOVED_TOTAL ) ??
128+ new Counter ( {
129+ name : this . PG_POOL_CONNECTIONS_REMOVED_TOTAL ,
130+ help : 'The total number of removed connections.' ,
131+ labelNames : mergeLabelNamesWithStandardLabels ( [ 'host' , 'database' ] , this . options . defaultLabels ) ,
132+ registers : [ this . register ]
133+ } ) ) as Counter
117134
118135 this . poolMaxSize = getMaxPoolSize ( pool )
119136 this . poolHost = getHost ( pool )
0 commit comments