io.dropwizard.metrics5.jetty10.InstrumentedConnectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-jetty10 Show documentation
Show all versions of metrics-jetty10 Show documentation
A set of extensions for Jetty 10.x and higher which provide instrumentation of thread pools, connector
metrics, and application latency and utilization.
The newest version!
package io.dropwizard.metrics5.jetty10;
import io.dropwizard.metrics5.Counter;
import io.dropwizard.metrics5.Timer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import java.util.List;
public class InstrumentedConnectionFactory extends ContainerLifeCycle implements ConnectionFactory {
private final ConnectionFactory connectionFactory;
private final Timer timer;
private final Counter counter;
public InstrumentedConnectionFactory(ConnectionFactory connectionFactory, Timer timer) {
this(connectionFactory, timer, null);
}
public InstrumentedConnectionFactory(ConnectionFactory connectionFactory, Timer timer, Counter counter) {
this.connectionFactory = connectionFactory;
this.timer = timer;
this.counter = counter;
addBean(connectionFactory);
}
@Override
public String getProtocol() {
return connectionFactory.getProtocol();
}
@Override
public List getProtocols() {
return connectionFactory.getProtocols();
}
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
final Connection connection = connectionFactory.newConnection(connector, endPoint);
connection.addEventListener(new Connection.Listener() {
private Timer.Context context;
@Override
public void onOpened(Connection connection) {
this.context = timer.time();
if (counter != null) {
counter.inc();
}
}
@Override
public void onClosed(Connection connection) {
context.stop();
if (counter != null) {
counter.dec();
}
}
});
return connection;
}
}