Skip to content

Commit

Permalink
Add a connection pool metric for connections in establishment. (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkokar authored Jun 18, 2019
1 parent 84ab718 commit f08f39b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.Closeable;
import java.util.EventListener;
import java.util.concurrent.CompletableFuture;

/**
* A connection to an origin.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,6 +85,13 @@ interface Stats {
* @return
*/
int terminatedConnections();

/**
* Number of connections being established at the moment.
*
* @return
*/
int connectionsInEstablishment();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/
public class SimpleConnectionPool implements ConnectionPool, Connection.Listener {
private static final Logger LOG = getLogger(SimpleConnectionPool.class);
private static final int MAX_ATTEMPTS = 3;

private final ConnectionPoolSettings poolSettings;
private final ConnectionSettings connectionSettings;
Expand Down Expand Up @@ -104,7 +105,7 @@ public Publisher<Connection> borrowConnection() {

private void newConnection() {
connectionAttempts.incrementAndGet();
newConnection(3)
newConnection(MAX_ATTEMPTS)
.doOnNext(it -> it.addConnectionListener(SimpleConnectionPool.this))
.subscribe(
connection -> {
Expand Down Expand Up @@ -224,6 +225,11 @@ public int terminatedConnections() {
return terminatedConnections.get();
}

@Override
public int connectionsInEstablishment() {
return connectionsInEstablishment.get();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ public void close() {
removeMetrics();
}

private void removeMetrics() {
MetricRegistry scopedRegistry = getMetricScope(connectionPool);
asList("busy-connections", "pending-connections", "available-connections", "ttfb",
"connection-attempts", "connection-failures", "connections-closed", "connections-terminated")
.forEach(scopedRegistry::deregister);
private static void registerMetrics(ConnectionPool hostConnectionPool, MetricRegistry scopedRegistry) {
ConnectionPool.Stats stats = hostConnectionPool.stats();
scopedRegistry.register("busy-connections", (Gauge<Integer>) stats::busyConnectionCount);
scopedRegistry.register("pending-connections", (Gauge<Integer>) stats::pendingConnectionCount);
scopedRegistry.register("available-connections", (Gauge<Integer>) stats::availableConnectionCount);
scopedRegistry.register("connection-attempts", (Gauge<Integer>) () -> (int) stats.connectionAttempts());
scopedRegistry.register("connection-failures", (Gauge<Integer>) () -> (int) stats.connectionFailures());
scopedRegistry.register("connections-closed", (Gauge<Integer>) () -> (int) stats.closedConnections());
scopedRegistry.register("connections-terminated", (Gauge<Integer>) () -> (int) stats.terminatedConnections());
scopedRegistry.register("in-establishment", (Gauge<Integer>) () -> (int) stats.connectionsInEstablishment());
}

private void registerMetrics() {
Expand All @@ -98,15 +103,12 @@ private void registerMetrics() {
}
}

private static void registerMetrics(ConnectionPool hostConnectionPool, MetricRegistry scopedRegistry) {
ConnectionPool.Stats stats = hostConnectionPool.stats();
scopedRegistry.register("busy-connections", (Gauge<Integer>) stats::busyConnectionCount);
scopedRegistry.register("pending-connections", (Gauge<Integer>) stats::pendingConnectionCount);
scopedRegistry.register("available-connections", (Gauge<Integer>) stats::availableConnectionCount);
scopedRegistry.register("connection-attempts", (Gauge<Integer>) () -> (int) stats.connectionAttempts());
scopedRegistry.register("connection-failures", (Gauge<Integer>) () -> (int) stats.connectionFailures());
scopedRegistry.register("connections-closed", (Gauge<Integer>) () -> (int) stats.closedConnections());
scopedRegistry.register("connections-terminated", (Gauge<Integer>) () -> (int) stats.terminatedConnections());
private void removeMetrics() {
MetricRegistry scopedRegistry = getMetricScope(connectionPool);
asList("busy-connections", "pending-connections", "available-connections", "ttfb",
"connection-attempts", "connection-failures", "connections-closed", "connections-terminated",
"in-establishment")
.forEach(scopedRegistry::deregister);
}

private MetricRegistry getMetricScope(ConnectionPool hostConnectionPool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void registersMetricsUnderOriginsScope() {
assertThat(metricRegistry.getGauges().keySet(), hasItems(
"origins.test-app.origin-X.connectionspool.pending-connections",
"origins.test-app.origin-X.connectionspool.available-connections",
"origins.test-app.origin-X.connectionspool.busy-connections"
"origins.test-app.origin-X.connectionspool.busy-connections",
"origins.test-app.origin-X.connectionspool.in-establishment"
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void removesRegisteredMetricsOnClose() {
"origins.generic-app.backend-01.connectionspool.busy-connections",
"origins.generic-app.backend-01.connectionspool.pending-connections",
"origins.generic-app.backend-01.connectionspool.connection-attempts",
"origins.generic-app.backend-01.connectionspool.in-establishment",
"origins.generic-app.backend-01.connectionspool.connection-failures",
"origins.generic-app.backend-01.connectionspool.connections-closed",
"origins.generic-app.backend-01.connectionspool.connections-terminated"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,6 +115,11 @@ public int closedConnections() {
public int terminatedConnections() {
return 0;
}

@Override
public int connectionsInEstablishment() {
return 0;
}
};
}

Expand Down

0 comments on commit f08f39b

Please sign in to comment.