36
36
* @author Kristof Depypere
37
37
* @author Jon Schneider
38
38
* @author Johnny Lim
39
+ * @author Markus Dobel
39
40
* @since 1.1.0
40
41
*/
41
42
@ NonNullApi
@@ -44,7 +45,7 @@ public class PostgreSQLDatabaseMetrics implements MeterBinder {
44
45
45
46
private static final String SELECT = "SELECT " ;
46
47
47
- private static final String QUERY_DEAD_TUPLE_COUNT = getUserTableQuery ("n_dead_tup" );
48
+ private static final String QUERY_DEAD_TUPLE_COUNT = getUserTableQuery ("SUM( n_dead_tup) " );
48
49
private static final String QUERY_TIMED_CHECKPOINTS_COUNT = getBgWriterQuery ("checkpoints_timed" );
49
50
private static final String QUERY_REQUESTED_CHECKPOINTS_COUNT = getBgWriterQuery ("checkpoints_req" );
50
51
private static final String QUERY_BUFFERS_CLEAN = getBgWriterQuery ("buffers_clean" );
@@ -78,7 +79,7 @@ public PostgreSQLDatabaseMetrics(DataSource postgresDataSource, String database,
78
79
this .beforeResetValuesCacheMap = new ConcurrentHashMap <>();
79
80
this .previousValueCacheMap = new ConcurrentHashMap <>();
80
81
81
- this .queryConnectionCount = getDBStatQuery (database , "SUM( numbackends) " );
82
+ this .queryConnectionCount = getDBStatQuery (database , "numbackends" );
82
83
this .queryReadCount = getDBStatQuery (database , "tup_fetched" );
83
84
this .queryInsertCount = getDBStatQuery (database , "tup_inserted" );
84
85
this .queryTempBytes = getDBStatQuery (database , "temp_bytes" );
@@ -95,38 +96,38 @@ private static Tag createDbTag(String database) {
95
96
96
97
@ Override
97
98
public void bindTo (MeterRegistry registry ) {
98
- Gauge .builder ("postgres.size" , postgresDataSource , dataSource -> getDatabaseSize ())
99
+ Gauge .builder (Names . SIZE , postgresDataSource , dataSource -> getDatabaseSize ())
99
100
.tags (tags )
100
101
.description ("The database size" )
101
102
.register (registry );
102
- Gauge .builder ("postgres.connections" , postgresDataSource , dataSource -> getConnectionCount ())
103
+ Gauge .builder (Names . CONNECTIONS , postgresDataSource , dataSource -> getConnectionCount ())
103
104
.tags (tags )
104
105
.description ("Number of active connections to the given db" )
105
106
.register (registry );
106
107
107
108
// Hit ratio can be derived from dividing hits/reads
108
- FunctionCounter .builder ("postgres.blocks.hits" , postgresDataSource ,
109
- dataSource -> resettableFunctionalCounter ("postgres.blocks.hits" , this ::getBlockHits ))
109
+ FunctionCounter .builder (Names . BLOCKS_HITS , postgresDataSource ,
110
+ dataSource -> resettableFunctionalCounter (Names . BLOCKS_HITS , this ::getBlockHits ))
110
111
.tags (tags )
111
112
.description ("Number of times disk blocks were found already in the buffer cache, so that a read was not necessary" )
112
113
.register (registry );
113
- FunctionCounter .builder ("postgres.blocks.reads" , postgresDataSource ,
114
- dataSource -> resettableFunctionalCounter ("postgres.blocks.reads" , this ::getBlockReads ))
114
+ FunctionCounter .builder (Names . BLOCKS_READS , postgresDataSource ,
115
+ dataSource -> resettableFunctionalCounter (Names . BLOCKS_READS , this ::getBlockReads ))
115
116
.tags (tags )
116
117
.description ("Number of disk blocks read in this database" )
117
118
.register (registry );
118
119
119
- FunctionCounter .builder ("postgres.transactions" , postgresDataSource ,
120
- dataSource -> resettableFunctionalCounter ("postgres.transactions" , this ::getTransactionCount ))
120
+ FunctionCounter .builder (Names . TRANSACTIONS , postgresDataSource ,
121
+ dataSource -> resettableFunctionalCounter (Names . TRANSACTIONS , this ::getTransactionCount ))
121
122
.tags (tags )
122
123
.description ("Total number of transactions executed (commits + rollbacks)" )
123
124
.register (registry );
124
- Gauge .builder ("postgres.locks" , postgresDataSource , dataSource -> getLockCount ())
125
+ Gauge .builder (Names . LOCKS , postgresDataSource , dataSource -> getLockCount ())
125
126
.tags (tags )
126
127
.description ("Number of locks on the given db" )
127
128
.register (registry );
128
- FunctionCounter .builder ("postgres.temp.writes" , postgresDataSource ,
129
- dataSource -> resettableFunctionalCounter ("postgres.temp.writes" , this ::getTempBytes ))
129
+ FunctionCounter .builder (Names . TEMP_WRITES , postgresDataSource ,
130
+ dataSource -> resettableFunctionalCounter (Names . TEMP_WRITES , this ::getTempBytes ))
130
131
.tags (tags )
131
132
.description ("The total amount of temporary writes to disk to execute queries" )
132
133
.baseUnit (BaseUnits .BYTES )
@@ -137,56 +138,56 @@ public void bindTo(MeterRegistry registry) {
137
138
}
138
139
139
140
private void registerRowCountMetrics (MeterRegistry registry ) {
140
- FunctionCounter .builder ("postgres.rows.fetched" , postgresDataSource ,
141
- dataSource -> resettableFunctionalCounter ("postgres.rows.fetched" , this ::getReadCount ))
141
+ FunctionCounter .builder (Names . ROWS_FETCHED , postgresDataSource ,
142
+ dataSource -> resettableFunctionalCounter (Names . ROWS_FETCHED , this ::getReadCount ))
142
143
.tags (tags )
143
144
.description ("Number of rows fetched from the db" )
144
145
.register (registry );
145
- FunctionCounter .builder ("postgres.rows.inserted" , postgresDataSource ,
146
- dataSource -> resettableFunctionalCounter ("postgres.rows.inserted" , this ::getInsertCount ))
146
+ FunctionCounter .builder (Names . ROWS_INSERTED , postgresDataSource ,
147
+ dataSource -> resettableFunctionalCounter (Names . ROWS_INSERTED , this ::getInsertCount ))
147
148
.tags (tags )
148
149
.description ("Number of rows inserted from the db" )
149
150
.register (registry );
150
- FunctionCounter .builder ("postgres.rows.updated" , postgresDataSource ,
151
- dataSource -> resettableFunctionalCounter ("postgres.rows.updated" , this ::getUpdateCount ))
151
+ FunctionCounter .builder (Names . ROWS_UPDATED , postgresDataSource ,
152
+ dataSource -> resettableFunctionalCounter (Names . ROWS_UPDATED , this ::getUpdateCount ))
152
153
.tags (tags )
153
154
.description ("Number of rows updated from the db" )
154
155
.register (registry );
155
- FunctionCounter .builder ("postgres.rows.deleted" , postgresDataSource ,
156
- dataSource -> resettableFunctionalCounter ("postgres.rows.deleted" , this ::getDeleteCount ))
156
+ FunctionCounter .builder (Names . ROWS_DELETED , postgresDataSource ,
157
+ dataSource -> resettableFunctionalCounter (Names . ROWS_DELETED , this ::getDeleteCount ))
157
158
.tags (tags )
158
159
.description ("Number of rows deleted from the db" )
159
160
.register (registry );
160
- Gauge .builder ("postgres.rows.dead" , postgresDataSource , dataSource -> getDeadTupleCount ())
161
+ Gauge .builder (Names . ROWS_DEAD , postgresDataSource , dataSource -> getDeadTupleCount ())
161
162
.tags (tags )
162
163
.description ("Total number of dead rows in the current database" )
163
164
.register (registry );
164
165
}
165
166
166
167
private void registerCheckpointMetrics (MeterRegistry registry ) {
167
- FunctionCounter .builder ("postgres.checkpoints.timed" , postgresDataSource ,
168
- dataSource -> resettableFunctionalCounter ("postgres.checkpoints.timed" , this ::getTimedCheckpointsCount ))
168
+ FunctionCounter .builder (Names . CHECKPOINTS_TIMED , postgresDataSource ,
169
+ dataSource -> resettableFunctionalCounter (Names . CHECKPOINTS_TIMED , this ::getTimedCheckpointsCount ))
169
170
.tags (tags )
170
171
.description ("Number of checkpoints timed" )
171
172
.register (registry );
172
- FunctionCounter .builder ("postgres.checkpoints.requested" , postgresDataSource ,
173
- dataSource -> resettableFunctionalCounter ("postgres.checkpoints.requested" , this ::getRequestedCheckpointsCount ))
173
+ FunctionCounter .builder (Names . CHECKPOINTS_REQUESTED , postgresDataSource ,
174
+ dataSource -> resettableFunctionalCounter (Names . CHECKPOINTS_REQUESTED , this ::getRequestedCheckpointsCount ))
174
175
.tags (tags )
175
176
.description ("Number of checkpoints requested" )
176
177
.register (registry );
177
178
178
- FunctionCounter .builder ("postgres.buffers.checkpoint" , postgresDataSource ,
179
- dataSource -> resettableFunctionalCounter ("postgres.buffers.checkpoint" , this ::getBuffersCheckpoint ))
179
+ FunctionCounter .builder (Names . BUFFERS_CHECKPOINT , postgresDataSource ,
180
+ dataSource -> resettableFunctionalCounter (Names . BUFFERS_CHECKPOINT , this ::getBuffersCheckpoint ))
180
181
.tags (tags )
181
182
.description ("Number of buffers written during checkpoints" )
182
183
.register (registry );
183
- FunctionCounter .builder ("postgres.buffers.clean" , postgresDataSource ,
184
- dataSource -> resettableFunctionalCounter ("postgres.buffers.clean" , this ::getBuffersClean ))
184
+ FunctionCounter .builder (Names . BUFFERS_CLEAN , postgresDataSource ,
185
+ dataSource -> resettableFunctionalCounter (Names . BUFFERS_CLEAN , this ::getBuffersClean ))
185
186
.tags (tags )
186
187
.description ("Number of buffers written by the background writer" )
187
188
.register (registry );
188
- FunctionCounter .builder ("postgres.buffers.backend" , postgresDataSource ,
189
- dataSource -> resettableFunctionalCounter ("postgres.buffers.backend" , this ::getBuffersBackend ))
189
+ FunctionCounter .builder (Names . BUFFERS_BACKEND , postgresDataSource ,
190
+ dataSource -> resettableFunctionalCounter (Names . BUFFERS_BACKEND , this ::getBuffersBackend ))
190
191
.tags (tags )
191
192
.description ("Number of buffers written directly by a backend" )
192
193
.register (registry );
@@ -282,7 +283,7 @@ private Long runQuery(String query) {
282
283
Statement statement = connection .createStatement ();
283
284
ResultSet resultSet = statement .executeQuery (query )) {
284
285
if (resultSet .next ()) {
285
- return resultSet .getObject ( 1 , Long . class );
286
+ return resultSet .getLong ( 1 );
286
287
}
287
288
} catch (SQLException ignored ) {
288
289
}
@@ -300,4 +301,34 @@ private static String getUserTableQuery(String statName) {
300
301
private static String getBgWriterQuery (String statName ) {
301
302
return SELECT + statName + " FROM pg_stat_bgwriter" ;
302
303
}
304
+
305
+ static final class Names {
306
+ public static final String SIZE = of ("size" );
307
+ public static final String CONNECTIONS = of ("connections" );
308
+ public static final String BLOCKS_HITS = of ("blocks.hits" );
309
+ public static final String BLOCKS_READS = of ("blocks.reads" );
310
+ public static final String TRANSACTIONS = of ("transactions" );
311
+ public static final String LOCKS = of ("locks" );
312
+ public static final String TEMP_WRITES = of ("temp.writes" );
313
+
314
+ public static final String ROWS_FETCHED = of ("rows.fetched" );
315
+ public static final String ROWS_INSERTED = of ("rows.inserted" );
316
+ public static final String ROWS_UPDATED = of ("rows.updated" );
317
+ public static final String ROWS_DELETED = of ("rows.deleted" );
318
+ public static final String ROWS_DEAD = of ("rows.dead" );
319
+
320
+ public static final String CHECKPOINTS_TIMED = of ("checkpoints.timed" );
321
+ public static final String CHECKPOINTS_REQUESTED = of ("checkpoints.requested" );
322
+
323
+ public static final String BUFFERS_CHECKPOINT = of ("buffers.checkpoint" );
324
+ public static final String BUFFERS_CLEAN = of ("buffers.clean" );
325
+ public static final String BUFFERS_BACKEND = of ("buffers.backend" );
326
+
327
+ private static String of (String name ) {
328
+ return "postgres." + name ;
329
+ }
330
+
331
+ private Names () {
332
+ }
333
+ }
303
334
}
0 commit comments