Skip to content

Commit f41d8bc

Browse files
authored
Synchronize access of current connections in JettyConnectionMetrics (#6587)
* JettyConnectionMetrics: synchronize access to connectionSamples (#6578) Guard access to connectionSamples with connectionSamplesLock at all times. This guarantees the gauge polls observe the number of current connections, as intended. Signed-off-by: Sergey Savenko <[email protected]> * JettyConnectionMetrics: update maxConnections outside the critical section (#6578) Move the update of the maxConnections TimeWindowMax out of the critical section Signed-off-by: Sergey Savenko <[email protected]> --------- Signed-off-by: Sergey Savenko <[email protected]> Closes gh-6578
1 parent aea78d9 commit f41d8bc

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jetty/JettyConnectionMetrics.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public JettyConnectionMetrics(MeterRegistry registry, Iterable<Tag> tags) {
121121
.tags(tags)
122122
.register(registry);
123123

124-
Gauge.builder("jetty.connections.current", this, jcm -> jcm.connectionSamples.size())
124+
Gauge.builder("jetty.connections.current", this, JettyConnectionMetrics::currentConnections)
125125
.strongReference(true)
126126
.baseUnit(BaseUnits.CONNECTIONS)
127127
.description("The current number of open Jetty connections")
@@ -160,10 +160,12 @@ private static Tags getConnectorNameTag(Connector connector) {
160160
@Override
161161
public void onOpened(Connection connection) {
162162
Timer.Sample started = Timer.start(registry);
163+
int connections;
163164
synchronized (connectionSamplesLock) {
164165
connectionSamples.put(connection, started);
165-
maxConnections.record(connectionSamples.size());
166+
connections = connectionSamples.size();
166167
}
168+
maxConnections.record(connections);
167169
}
168170

169171
@Override
@@ -260,4 +262,10 @@ private static Method getNetworkTrafficListenerMethod(NetworkTrafficServerConnec
260262
return method;
261263
}
262264

265+
private int currentConnections() {
266+
synchronized (connectionSamplesLock) {
267+
return connectionSamples.size();
268+
}
269+
}
270+
263271
}

0 commit comments

Comments
 (0)