Skip to content

Commit 58bb55e

Browse files
Fixes #26555: Fix db_connections_total Prometheus metric always zero
1 parent ad1a2bd commit 58bb55e

2 files changed

Lines changed: 67 additions & 10 deletions

File tree

openmetadata-service/src/main/java/org/openmetadata/service/monitoring/OpenMetadataMetrics.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import io.micrometer.core.instrument.Counter;
66
import io.micrometer.core.instrument.DistributionSummary;
7+
import io.micrometer.core.instrument.Gauge;
78
import io.micrometer.core.instrument.MeterRegistry;
89
import io.micrometer.core.instrument.Timer;
910
import 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
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.openmetadata.service.monitoring;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import io.micrometer.core.instrument.Gauge;
7+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
8+
import java.util.concurrent.atomic.AtomicInteger;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
class OpenMetadataMetricsTest {
13+
14+
private SimpleMeterRegistry registry;
15+
16+
@BeforeEach
17+
void setUp() {
18+
registry = new SimpleMeterRegistry();
19+
}
20+
21+
@Test
22+
void dbConnectionsTotalReflectsHikariCPPoolSize() {
23+
AtomicInteger activeConnections = new AtomicInteger(5);
24+
AtomicInteger idleConnections = new AtomicInteger(15);
25+
26+
Gauge.builder("hikaricp.connections.active", activeConnections, AtomicInteger::doubleValue)
27+
.register(registry);
28+
Gauge.builder("hikaricp.connections.idle", idleConnections, AtomicInteger::doubleValue)
29+
.register(registry);
30+
31+
new OpenMetadataMetrics(registry);
32+
33+
Gauge total = registry.find("db.connections").gauge();
34+
assertNotNull(total, "db.connections gauge should be registered");
35+
assertEquals(20.0, total.value(), 0.01, "Should equal active + idle");
36+
37+
activeConnections.set(10);
38+
idleConnections.set(10);
39+
assertEquals(20.0, total.value(), 0.01, "Should reflect updated pool state");
40+
41+
activeConnections.set(0);
42+
idleConnections.set(0);
43+
assertEquals(0.0, total.value(), 0.01, "Should be zero when pool is empty");
44+
}
45+
46+
@Test
47+
void dbConnectionsTotalReturnsZeroWithoutHikariCP() {
48+
new OpenMetadataMetrics(registry);
49+
50+
Gauge total = registry.find("db.connections").gauge();
51+
assertNotNull(total, "db.connections gauge should be registered");
52+
assertEquals(0.0, total.value(), 0.01, "Should be zero when HikariCP metrics are absent");
53+
}
54+
}

0 commit comments

Comments
 (0)