-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7099 from Postremus/mongo-connection-pool-metrics
Add Mongo Connection Pool Metrics
- Loading branch information
Showing
12 changed files
with
300 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ent/src/main/java/io/quarkus/mongodb/deployment/MongoConnectionPoolListenerBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.mongodb.deployment; | ||
|
||
import com.mongodb.event.ConnectionPoolListener; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
public final class MongoConnectionPoolListenerBuildItem extends MultiBuildItem { | ||
private ConnectionPoolListener connectionPoolListener; | ||
|
||
public MongoConnectionPoolListenerBuildItem(ConnectionPoolListener connectionPoolListener) { | ||
this.connectionPoolListener = connectionPoolListener; | ||
} | ||
|
||
public ConnectionPoolListener getConnectionPoolListener() { | ||
return connectionPoolListener; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
extensions/mongodb-client/deployment/src/test/java/io/quarkus/mongodb/MongoMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.quarkus.mongodb; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.metrics.Metric; | ||
import org.eclipse.microprofile.metrics.MetricID; | ||
import org.eclipse.microprofile.metrics.MetricRegistry; | ||
import org.eclipse.microprofile.metrics.Tag; | ||
import org.eclipse.microprofile.metrics.annotation.RegistryType; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.mongodb.client.MongoClient; | ||
|
||
import io.quarkus.mongodb.metrics.ConnectionPoolGauge; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class MongoMetricsTest extends MongoTestBase { | ||
|
||
@Inject | ||
MongoClient client; | ||
|
||
@Inject | ||
@RegistryType(type = MetricRegistry.Type.VENDOR) | ||
MetricRegistry registry; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(MongoTestBase.class)) | ||
.withConfigurationResource("application-metrics-mongo.properties"); | ||
|
||
@AfterEach | ||
void cleanup() { | ||
if (client != null) { | ||
client.close(); | ||
} | ||
} | ||
|
||
@Test | ||
void testMetricsInitialization() { | ||
assertNull(getGaugeValueOrNull("mongodb.connection-pool.size", getTags())); | ||
assertNull(getGaugeValueOrNull("mongodb.connection-pool.checked-out-count", getTags())); | ||
|
||
// Just need to execute something so that an connection is opened | ||
String name = client.listDatabaseNames().first(); | ||
|
||
assertEquals(1L, getGaugeValueOrNull("mongodb.connection-pool.size", getTags())); | ||
assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.checked-out-count", getTags())); | ||
|
||
client.close(); | ||
assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.size", getTags())); | ||
assertEquals(0L, getGaugeValueOrNull("mongodb.connection-pool.checked-out-count", getTags())); | ||
} | ||
|
||
private Long getGaugeValueOrNull(String metricName, Tag[] tags) { | ||
MetricID metricID = new MetricID(metricName, tags); | ||
Metric metric = registry.getMetrics().get(metricID); | ||
|
||
if (metric == null) { | ||
return null; | ||
} | ||
return ((ConnectionPoolGauge) metric).getValue(); | ||
} | ||
|
||
private Tag[] getTags() { | ||
return new Tag[] { | ||
new Tag("host", "localhost"), | ||
new Tag("port", "27018"), | ||
}; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
extensions/mongodb-client/deployment/src/test/resources/application-metrics-mongo.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
quarkus.mongodb.connection-string=mongodb://localhost:27018 | ||
quarkus.mongodb.metrics.enabled=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../mongodb-client/runtime/src/main/java/io/quarkus/mongodb/metrics/ConnectionPoolGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.mongodb.metrics; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import org.eclipse.microprofile.metrics.Gauge; | ||
|
||
public class ConnectionPoolGauge implements Gauge<Long> { | ||
private AtomicLong value = new AtomicLong(); | ||
|
||
public void decrement() { | ||
value.decrementAndGet(); | ||
} | ||
|
||
public void increment() { | ||
value.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public Long getValue() { | ||
return value.longValue(); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
.../runtime/src/main/java/io/quarkus/mongodb/metrics/MongoMetricsConnectionPoolListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package io.quarkus.mongodb.metrics; | ||
|
||
import org.eclipse.microprofile.metrics.Metadata; | ||
import org.eclipse.microprofile.metrics.Metric; | ||
import org.eclipse.microprofile.metrics.MetricID; | ||
import org.eclipse.microprofile.metrics.MetricRegistry; | ||
import org.eclipse.microprofile.metrics.MetricType; | ||
import org.eclipse.microprofile.metrics.Tag; | ||
|
||
import com.mongodb.connection.ServerId; | ||
import com.mongodb.event.ConnectionAddedEvent; | ||
import com.mongodb.event.ConnectionCheckedInEvent; | ||
import com.mongodb.event.ConnectionCheckedOutEvent; | ||
import com.mongodb.event.ConnectionPoolClosedEvent; | ||
import com.mongodb.event.ConnectionPoolListener; | ||
import com.mongodb.event.ConnectionPoolOpenedEvent; | ||
import com.mongodb.event.ConnectionPoolWaitQueueEnteredEvent; | ||
import com.mongodb.event.ConnectionPoolWaitQueueExitedEvent; | ||
import com.mongodb.event.ConnectionRemovedEvent; | ||
|
||
import io.smallrye.metrics.MetricRegistries; | ||
|
||
public class MongoMetricsConnectionPoolListener implements ConnectionPoolListener { | ||
private final static String SIZE_NAME = "mongodb.connection-pool.size"; | ||
private final static String CHECKED_OUT_COUNT_NAME = "mongodb.connection-pool.checked-out-count"; | ||
|
||
@Override | ||
public void connectionPoolOpened(ConnectionPoolOpenedEvent event) { | ||
Tag[] tags = createTags(event.getServerId()); | ||
|
||
registerGauge(SIZE_NAME, "the current size of the pool, including idle and and in-use members", tags); | ||
registerGauge(CHECKED_OUT_COUNT_NAME, "the current count of connections that are currently in use", tags); | ||
} | ||
|
||
@Override | ||
public void connectionPoolClosed(ConnectionPoolClosedEvent event) { | ||
} | ||
|
||
@Override | ||
public void connectionCheckedOut(ConnectionCheckedOutEvent event) { | ||
MetricID metricID = createMetricID(CHECKED_OUT_COUNT_NAME, event.getConnectionId().getServerId()); | ||
|
||
Metric metric = getMetricRegistry().getMetrics().get(metricID); | ||
|
||
if (metric != null) { | ||
((ConnectionPoolGauge) metric).increment(); | ||
} | ||
} | ||
|
||
@Override | ||
public void connectionCheckedIn(ConnectionCheckedInEvent event) { | ||
MetricID metricID = createMetricID(CHECKED_OUT_COUNT_NAME, event.getConnectionId().getServerId()); | ||
|
||
Metric metric = getMetricRegistry().getMetrics().get(metricID); | ||
|
||
if (metric != null) { | ||
((ConnectionPoolGauge) metric).decrement(); | ||
} | ||
} | ||
|
||
@Override | ||
public void waitQueueEntered(ConnectionPoolWaitQueueEnteredEvent connectionPoolWaitQueueEnteredEvent) { | ||
// Not supported, since the event is deprecated and will be removed anyway | ||
} | ||
|
||
@Override | ||
public void waitQueueExited(ConnectionPoolWaitQueueExitedEvent connectionPoolWaitQueueExitedEvent) { | ||
// Not supported, since the event is deprecated and will be removed anyway | ||
} | ||
|
||
@Override | ||
public void connectionAdded(ConnectionAddedEvent event) { | ||
|
||
MetricID metricID = createMetricID(SIZE_NAME, event.getConnectionId().getServerId()); | ||
|
||
Metric metric = getMetricRegistry().getMetrics().get(metricID); | ||
|
||
if (metric != null) { | ||
((ConnectionPoolGauge) metric).increment(); | ||
} | ||
} | ||
|
||
@Override | ||
public void connectionRemoved(ConnectionRemovedEvent event) { | ||
|
||
MetricID metricID = createMetricID(SIZE_NAME, event.getConnectionId().getServerId()); | ||
|
||
Metric metric = getMetricRegistry().getMetrics().get(metricID); | ||
|
||
if (metric != null) { | ||
((ConnectionPoolGauge) metric).decrement(); | ||
} | ||
} | ||
|
||
private void registerGauge(String metricName, String description, Tag[] tags) { | ||
getMetricRegistry().remove(new MetricID(metricName, tags)); | ||
|
||
Metadata metaData = Metadata.builder().withName(metricName).withType(MetricType.GAUGE) | ||
.withDescription(description).build(); | ||
getMetricRegistry().register(metaData, new ConnectionPoolGauge(), tags); | ||
} | ||
|
||
private MetricRegistry getMetricRegistry() { | ||
return MetricRegistries.get(MetricRegistry.Type.VENDOR); | ||
} | ||
|
||
private Tag[] createTags(ServerId server) { | ||
return new Tag[] { | ||
new Tag("host", server.getAddress().getHost()), | ||
new Tag("port", String.valueOf(server.getAddress().getPort())), | ||
}; | ||
} | ||
|
||
private MetricID createMetricID(String metricName, ServerId server) { | ||
return new MetricID(metricName, createTags(server)); | ||
} | ||
} |
Oops, something went wrong.