Skip to content

Commit 112f70f

Browse files
fix: Address review comments — restore metric name and add Prometheus scrape test
- Restore gauge name to 'db.connections.total' so Micrometer's Prometheus naming convention exposes it as 'db_connections_total', preserving backward compatibility with existing dashboards. - Add prometheusScrapExposesDbConnectionsTotal() test that uses PrometheusMeterRegistry to verify the Prometheus scrape output contains 'db_connections_total' as a gauge type.
1 parent 58bb55e commit 112f70f

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public OpenMetadataMetrics(MeterRegistry meterRegistry) {
5050
.sla(LATENCY_SLA_BUCKETS)
5151
.register(meterRegistry);
5252

53-
Gauge.builder("db.connections", () -> poolTotalConnections())
53+
Gauge.builder("db.connections.total", () -> poolTotalConnections())
5454
.description("Total connections in the database connection pool")
5555
.register(meterRegistry);
5656

openmetadata-service/src/test/java/org/openmetadata/service/monitoring/OpenMetadataMetricsTest.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import io.micrometer.core.instrument.Gauge;
78
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
9+
import io.micrometer.prometheusmetrics.PrometheusConfig;
10+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
811
import java.util.concurrent.atomic.AtomicInteger;
912
import org.junit.jupiter.api.BeforeEach;
1013
import org.junit.jupiter.api.Test;
@@ -30,8 +33,8 @@ void dbConnectionsTotalReflectsHikariCPPoolSize() {
3033

3134
new OpenMetadataMetrics(registry);
3235

33-
Gauge total = registry.find("db.connections").gauge();
34-
assertNotNull(total, "db.connections gauge should be registered");
36+
Gauge total = registry.find("db.connections.total").gauge();
37+
assertNotNull(total, "db.connections.total gauge should be registered");
3538
assertEquals(20.0, total.value(), 0.01, "Should equal active + idle");
3639

3740
activeConnections.set(10);
@@ -47,8 +50,31 @@ void dbConnectionsTotalReflectsHikariCPPoolSize() {
4750
void dbConnectionsTotalReturnsZeroWithoutHikariCP() {
4851
new OpenMetadataMetrics(registry);
4952

50-
Gauge total = registry.find("db.connections").gauge();
51-
assertNotNull(total, "db.connections gauge should be registered");
53+
Gauge total = registry.find("db.connections.total").gauge();
54+
assertNotNull(total, "db.connections.total gauge should be registered");
5255
assertEquals(0.0, total.value(), 0.01, "Should be zero when HikariCP metrics are absent");
5356
}
57+
58+
@Test
59+
void prometheusScrapExposesDbConnectionsTotal() {
60+
PrometheusMeterRegistry promRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
61+
62+
AtomicInteger activeConnections = new AtomicInteger(3);
63+
AtomicInteger idleConnections = new AtomicInteger(7);
64+
65+
Gauge.builder("hikaricp.connections.active", activeConnections, AtomicInteger::doubleValue)
66+
.register(promRegistry);
67+
Gauge.builder("hikaricp.connections.idle", idleConnections, AtomicInteger::doubleValue)
68+
.register(promRegistry);
69+
70+
new OpenMetadataMetrics(promRegistry);
71+
72+
String scrape = promRegistry.scrape();
73+
assertTrue(
74+
scrape.contains("db_connections_total"),
75+
"Prometheus scrape should contain db_connections_total metric");
76+
assertTrue(
77+
scrape.contains("# TYPE db_connections_total gauge"),
78+
"db_connections_total should be exposed as a gauge type");
79+
}
5480
}

0 commit comments

Comments
 (0)