-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fixes #26555: Fix db_connections_total Prometheus metric always zero #27022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
040fd0a
5c5accd
7ef92aa
90bd572
105c956
4620868
4f54819
74fb4ce
78300f4
0b0d41d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package org.openmetadata.service.monitoring; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.micrometer.core.instrument.Gauge; | ||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
| import io.micrometer.prometheusmetrics.PrometheusConfig; | ||
| import io.micrometer.prometheusmetrics.PrometheusMeterRegistry; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class OpenMetadataMetricsTest { | ||
|
|
||
| private SimpleMeterRegistry registry; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| registry = new SimpleMeterRegistry(); | ||
| } | ||
|
|
||
| @Test | ||
| void dbPoolConnectionsGaugeReflectsHikariCPPoolSize() { | ||
| AtomicInteger activeConnections = new AtomicInteger(5); | ||
| AtomicInteger idleConnections = new AtomicInteger(15); | ||
|
|
||
| Gauge.builder("hikaricp.connections.active", activeConnections, AtomicInteger::doubleValue) | ||
| .register(registry); | ||
| Gauge.builder("hikaricp.connections.idle", idleConnections, AtomicInteger::doubleValue) | ||
| .register(registry); | ||
|
|
||
| new OpenMetadataMetrics(registry); | ||
|
|
||
| Gauge total = registry.find("db.pool.connections").gauge(); | ||
| assertNotNull(total, "db.pool.connections gauge should be registered"); | ||
| assertEquals(20.0, total.value(), 0.01, "Should equal active + idle"); | ||
|
Comment on lines
+24
to
+38
|
||
|
|
||
| activeConnections.set(10); | ||
| idleConnections.set(10); | ||
| assertEquals(20.0, total.value(), 0.01, "Should reflect updated pool state"); | ||
|
|
||
| activeConnections.set(0); | ||
| idleConnections.set(0); | ||
| assertEquals(0.0, total.value(), 0.01, "Should be zero when pool is empty"); | ||
| } | ||
|
|
||
| @Test | ||
| void dbPoolConnectionsGaugeReturnsZeroWithoutHikariCP() { | ||
| new OpenMetadataMetrics(registry); | ||
|
|
||
| Gauge total = registry.find("db.pool.connections").gauge(); | ||
| assertNotNull(total, "db.pool.connections gauge should be registered"); | ||
| assertEquals(0.0, total.value(), 0.01, "Should be zero when HikariCP metrics are absent"); | ||
| } | ||
|
|
||
| @Test | ||
| void prometheusScrapeExposesDbPoolConnectionsGauge() { | ||
| PrometheusMeterRegistry promRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); | ||
|
|
||
| AtomicInteger activeConnections = new AtomicInteger(3); | ||
| AtomicInteger idleConnections = new AtomicInteger(7); | ||
|
|
||
| Gauge.builder("hikaricp.connections.active", activeConnections, AtomicInteger::doubleValue) | ||
| .register(promRegistry); | ||
| Gauge.builder("hikaricp.connections.idle", idleConnections, AtomicInteger::doubleValue) | ||
| .register(promRegistry); | ||
|
|
||
| new OpenMetadataMetrics(promRegistry); | ||
|
|
||
| String scrape = promRegistry.scrape(); | ||
| assertTrue( | ||
| scrape.contains("db_pool_connections"), | ||
| "Prometheus scrape should contain db_pool_connections metric"); | ||
| assertTrue( | ||
| scrape.contains("# TYPE db_pool_connections gauge"), | ||
| "db_pool_connections should be exposed as a gauge type"); | ||
|
Comment on lines
+58
to
+78
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The gauge callback performs two registry lookups (
meterRegistry.find(...).gauge()) on every sample/scrape. To reduce overhead, consider caching theGaugereferences once they’re discovered (e.g., initialize lazily on first successful lookup and reuse thereafter), while still supporting late registration of Hikari meters.