44
55import io .micrometer .core .instrument .Counter ;
66import io .micrometer .core .instrument .DistributionSummary ;
7+ import io .micrometer .core .instrument .Gauge ;
78import io .micrometer .core .instrument .MeterRegistry ;
89import io .micrometer .core .instrument .Timer ;
910import java .time .Duration ;
@@ -19,7 +20,6 @@ public class OpenMetadataMetrics {
1920 private final DistributionSummary httpResponseSize ;
2021
2122 private final Timer jdbiQueryTimer ;
22- private final Counter jdbiConnectionCounter ;
2323 private final Counter jdbiErrorCounter ;
2424
2525 private final Counter entityCreatedCounter ;
@@ -50,10 +50,9 @@ public OpenMetadataMetrics(MeterRegistry meterRegistry) {
5050 .sla (LATENCY_SLA_BUCKETS )
5151 .register (meterRegistry );
5252
53- this .jdbiConnectionCounter =
54- Counter .builder ("db.connections.total" )
55- .description ("Total database connections created" )
56- .register (meterRegistry );
53+ Gauge .builder ("db.connections" , () -> poolTotalConnections ())
54+ .description ("Total connections in the database connection pool" )
55+ .register (meterRegistry );
5756
5857 this .jdbiErrorCounter =
5958 Counter .builder ("db.errors.total" )
@@ -135,10 +134,6 @@ public void recordDatabaseQuery(String queryType, long durationMs) {
135134 .record (Duration .ofMillis (durationMs ));
136135 }
137136
138- public void incrementDatabaseConnections () {
139- jdbiConnectionCounter .increment ();
140- }
141-
142137 public void incrementDatabaseErrors (String errorType ) {
143138 meterRegistry .counter ("db.errors.total" , "type" , errorType ).increment ();
144139 }
@@ -200,12 +195,20 @@ public void recordAuthenticationFailure(String authType, String reason) {
200195 // Gauge registration methods
201196 public void registerGauge (
202197 String name , java .util .function .Supplier <Number > supplier , String description ) {
203- io . micrometer . core . instrument . Gauge .builder (name , () -> supplier .get ().doubleValue ())
198+ Gauge .builder (name , () -> supplier .get ().doubleValue ())
204199 .description (description )
205200 .register (meterRegistry );
206201 }
207202
208203 public MeterRegistry getMeterRegistry () {
209204 return meterRegistry ;
210205 }
206+
207+ private double poolTotalConnections () {
208+ Gauge active = meterRegistry .find ("hikaricp.connections.active" ).gauge ();
209+ Gauge idle = meterRegistry .find ("hikaricp.connections.idle" ).gauge ();
210+ double activeVal = active != null ? active .value () : 0.0 ;
211+ double idleVal = idle != null ? idle .value () : 0.0 ;
212+ return activeVal + idleVal ;
213+ }
211214}
0 commit comments